-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.js
129 lines (107 loc) · 3.24 KB
/
manifest.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var parser = require('xml2json');
var fs = require('fs');
var pd = require('pretty-data').pd;
var log = require('./logger');
object2xml = function(manifestObject) {
var project;
var external = {};
external.project = [];
var length = manifestObject.manifest.project.length;
for (i = 0; i < length; i++) {
project = manifestObject.manifest.project[i];
if (project.external) {
delete project.external;
delete project.path;
external.project.push(project);
manifestObject.manifest.project[i] = undefined;
}
}
if (external.project.length > 0) {
manifestObject.manifest.external = external;
// Cleanup main project array
for (i = length; i > 0; i--) {
if (manifestObject.manifest.project[i] === undefined) {
manifestObject.manifest.project.splice(i, 1);
}
}
}
return pd.xml(parser.toXml(manifestObject));
};
exports.object2xml = object2xml;
xml2object = function(xml) {
// Send a method response with a value
var json = parser.toJson(xml, {reversible: true});
var manifestObject = JSON.parse(json);
var length = manifestObject.manifest.project.length;
for (i = 0; i < length; i++) {
var project = manifestObject.manifest.project[i];
delete project.groups;
}
return manifestObject;
};
exports.xml2object = xml2object;
exports.readObject = function(path, file, callback) {
fs.readFile(path + file, function(err, data){
if(err) {
log.error("Could not open file: " + err);
callback(err, undefined);
}
// Send a method response with a value
var json = parser.toJson(data.toString(), {reversible: true});
var manifestObject = JSON.parse(json);
if (manifestObject.manifest.external) {
while (manifestObject.manifest.external.project.length > 0) {
var project = manifestObject.manifest.external.project.shift();
project.external = true;
project.path = 'N/A';
manifestObject.manifest.project.push(project);
}
}
callback(err, manifestObject);
});
};
exports.writeObject = function(path, file, data, callback) {
fs.writeFile(path + file, object2xml(data), function(err){
callback(err);
});
};
exports.applyChanges = function (manifest, request) {
var projects = manifest.manifest.project;
// Modify
for (i in request.changes) {
change = request.changes[i];
switch (change.action) {
case "ADD":
// TODO: error checking, does it already exist?
project = {};
project.name = change.project.name;
project.path = change.project.path;
project.groups = change.project.groups;
project.revision = change.project.revision;
project.remote = change.project.remote;
projects.push(project);
break;
case "REMOVE":
// TODO: Error checking, is it really removing anything?
projects = projects.filter(function(v) { return v.name == change.project.name ? false : true;});
manifest.manifest.project = projects;
break;
case "MODIFY":
for (j in projects) {
project = projects[j];
if (change.project.name == project.name) {
project.path = change.project.path;
project.groups = change.project.groups;
project.revision = change.project.revision;
project.remote = change.project.remote;
}
}
break;
default:
log.error("Unknown change action: " + change.action);
return false;
break;
}
}
return true;
};