summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/cordova/lib/PodsJson.js
blob: b13a1afe2a9b917d89ba0c953b008e6f72101f31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
*/

var fs = require('fs'),
    path = require('path'),
    util = require('util'),
    events = require('cordova-common').events,
    CordovaError = require('cordova-common').CordovaError;

PodsJson.FILENAME = 'pods.json';

function PodsJson(podsJsonPath) {
    this.path = podsJsonPath;
    this.contents = null;
    this.__dirty = false;

    var filename = this.path.split(path.sep).pop();
    if (filename !== PodsJson.FILENAME) {
        throw new CordovaError(util.format('PodsJson: The file at %s is not `%s`.', this.path, PodsJson.FILENAME));
    }

    if (!fs.existsSync(this.path)) {
        events.emit('verbose', util.format('pods.json: The file at %s does not exist.', this.path));
        events.emit('verbose', 'Creating new pods.json in platforms/ios');
        this.clear();
        this.write();
    } else {
        events.emit('verbose', 'pods.json found in platforms/ios');  
        // load contents
        this.contents = fs.readFileSync(this.path, 'utf8');
        this.contents = JSON.parse(this.contents);
    }
}

PodsJson.prototype.get = function(name) {
    return this.contents[name];
};

PodsJson.prototype.remove = function(name) {
    if (this.contents[name]) {
        delete this.contents[name];
        this.__dirty = true;
        events.emit('verbose', util.format('Remove from pods.json for `%s`', name));
    }
};

PodsJson.prototype.clear = function() {
    this.contents = {};
    this.__dirty = true;
};

PodsJson.prototype.destroy = function() {
    fs.unlinkSync(this.path);
    events.emit('verbose', util.format('Deleted `%s`', this.path));
};

PodsJson.prototype.write = function() {
    if (this.contents) {
        fs.writeFileSync(this.path, JSON.stringify(this.contents, null, 4));
        this.__dirty = false;
        events.emit('verbose', 'Wrote to pods.json.');
    }
};

PodsJson.prototype.set = function(name, type, spec, count) {
    this.setJson(name, { name: name, type: type, spec: spec, count: count });
};

PodsJson.prototype.increment = function(name) {
    var val = this.get(name);
    if (val) {
        val.count++;
        this.setJson(val);
    }
};

PodsJson.prototype.decrement = function(name) {
    var val = this.get(name);
    if (val) {
        val.count--;
        if (val.count <= 0) {
            this.remove(name);
        } else {
            this.setJson(val);
        }
    }
};

PodsJson.prototype.setJson = function(name, json) {
    this.contents[name] = json;
    this.__dirty = true;
    events.emit('verbose', util.format('Set pods.json for `%s`', name));
};

PodsJson.prototype.isDirty = function() {
    return this.__dirty;
};

module.exports.PodsJson = PodsJson;