-
Notifications
You must be signed in to change notification settings - Fork 98
/
pbxWriter.js
314 lines (246 loc) · 7.58 KB
/
pbxWriter.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
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 pbxProj = require('./pbxProject'),
util = require('util'),
f = util.format,
INDENT = '\t',
COMMENT_KEY = /_comment$/,
QUOTED = /^"(.*)"$/,
EventEmitter = require('events').EventEmitter
// indentation
function i(x) {
if (x <=0)
return '';
else
return INDENT + i(x-1);
}
function comment(key, parent) {
var text = parent[key + '_comment'];
if (text)
return text;
else
return null;
}
// copied from underscore
function isObject(obj) {
return obj === Object(obj)
}
function isArray(obj) {
return Array.isArray(obj)
}
function pbxWriter(contents, options) {
if (!options) {
options = {}
}
if (options.omitEmptyValues === undefined) {
options.omitEmptyValues = false
}
this.contents = contents;
this.sync = false;
this.indentLevel = 0;
this.omitEmptyValues = options.omitEmptyValues
}
util.inherits(pbxWriter, EventEmitter);
pbxWriter.prototype.write = function (str) {
var fmt = f.apply(null, arguments);
if (this.sync) {
this.buffer += f("%s%s", i(this.indentLevel), fmt);
} else {
// do stream write
}
}
pbxWriter.prototype.writeFlush = function (str) {
var oldIndent = this.indentLevel;
this.indentLevel = 0;
this.write.apply(this, arguments)
this.indentLevel = oldIndent;
}
pbxWriter.prototype.writeSync = function () {
this.sync = true;
this.buffer = "";
this.writeHeadComment();
this.writeProject();
return this.buffer;
}
pbxWriter.prototype.writeHeadComment = function () {
if (this.contents.headComment) {
this.write("// %s\n", this.contents.headComment)
}
}
pbxWriter.prototype.writeProject = function () {
var proj = this.contents.project,
key, cmt, obj;
this.write("{\n")
if (proj) {
this.indentLevel++;
for (key in proj) {
// skip comments
if (COMMENT_KEY.test(key)) continue;
cmt = comment(key, proj);
obj = proj[key];
if (isArray(obj)) {
this.writeArray(obj, key)
} else if (isObject(obj)) {
this.write("%s = {\n", key);
this.indentLevel++;
if (key === 'objects') {
this.writeObjectsSections(obj)
} else {
this.writeObject(obj)
}
this.indentLevel--;
this.write("};\n");
} else if (this.omitEmptyValues && (obj === undefined || obj === null)) {
continue;
} else if (cmt) {
this.write("%s = %s /* %s */;\n", key, obj, cmt)
} else {
this.write("%s = %s;\n", key, obj)
}
}
this.indentLevel--;
}
this.write("}\n")
}
pbxWriter.prototype.writeObject = function (object) {
var key, obj, cmt;
for (key in object) {
if (COMMENT_KEY.test(key)) continue;
cmt = comment(key, object);
obj = object[key];
if (isArray(obj)) {
this.writeArray(obj, key)
} else if (isObject(obj)) {
this.write("%s = {\n", key);
this.indentLevel++;
this.writeObject(obj)
this.indentLevel--;
this.write("};\n");
} else {
if (this.omitEmptyValues && (obj === undefined || obj === null)) {
continue;
} else if (cmt) {
this.write("%s = %s /* %s */;\n", key, obj, cmt)
} else {
this.write("%s = %s;\n", key, obj)
}
}
}
}
pbxWriter.prototype.writeObjectsSections = function (objects) {
var first = true,
key, obj;
for (key in objects) {
if (!first) {
this.writeFlush("\n")
} else {
first = false;
}
obj = objects[key];
if (isObject(obj)) {
this.writeSectionComment(key, true);
this.writeSection(obj);
this.writeSectionComment(key, false);
}
}
}
pbxWriter.prototype.writeArray = function (arr, name) {
var i, entry;
this.write("%s = (\n", name);
this.indentLevel++;
for (i=0; i < arr.length; i++) {
entry = arr[i]
if (entry.value && entry.comment) {
this.write('%s /* %s */,\n', entry.value, entry.comment);
} else if (isObject(entry)) {
this.write('{\n');
this.indentLevel++;
this.writeObject(entry);
this.indentLevel--;
this.write('},\n');
} else {
this.write('%s,\n', entry);
}
}
this.indentLevel--;
this.write(");\n");
}
pbxWriter.prototype.writeSectionComment = function (name, begin) {
if (begin) {
this.writeFlush("/* Begin %s section */\n", name)
} else { // end
this.writeFlush("/* End %s section */\n", name)
}
}
pbxWriter.prototype.writeSection = function (section) {
var key, obj, cmt;
// section should only contain objects
for (key in section) {
if (COMMENT_KEY.test(key)) continue;
cmt = comment(key, section);
obj = section[key]
if (obj.isa == 'PBXBuildFile' || obj.isa == 'PBXFileReference') {
this.writeInlineObject(key, cmt, obj);
} else {
if (cmt) {
this.write("%s /* %s */ = {\n", key, cmt);
} else {
this.write("%s = {\n", key);
}
this.indentLevel++
this.writeObject(obj)
this.indentLevel--
this.write("};\n");
}
}
}
pbxWriter.prototype.writeInlineObject = function (n, d, r) {
var output = [];
var self = this
var inlineObjectHelper = function (name, desc, ref) {
var key, cmt, obj;
if (desc) {
output.push(f("%s /* %s */ = {", name, desc));
} else {
output.push(f("%s = {", name));
}
for (key in ref) {
if (COMMENT_KEY.test(key)) continue;
cmt = comment(key, ref);
obj = ref[key];
if (isArray(obj)) {
output.push(f("%s = (", key));
for (var i=0; i < obj.length; i++) {
output.push(f("%s, ", obj[i]))
}
output.push("); ");
} else if (isObject(obj)) {
inlineObjectHelper(key, cmt, obj)
} else if (self.omitEmptyValues && (obj === undefined || obj === null)) {
continue;
} else if (cmt) {
output.push(f("%s = %s /* %s */; ", key, obj, cmt))
} else {
output.push(f("%s = %s; ", key, obj))
}
}
output.push("}; ");
}
inlineObjectHelper(n, d, r);
this.write("%s\n", output.join('').trim());
}
module.exports = pbxWriter;