1 | var pbxProj = require('./pbxProject'),
|
2 | util = require('util'),
|
3 | f = util.format,
|
4 | INDENT = '\t',
|
5 | COMMENT_KEY = /_comment$/,
|
6 | QUOTED = /^"(.*)"$/,
|
7 | EventEmitter = require('events').EventEmitter
|
8 |
|
9 |
|
10 | function i(x) {
|
11 | if (x <=0)
|
12 | return '';
|
13 | else
|
14 | return INDENT + i(x-1);
|
15 | }
|
16 |
|
17 | function comment(key, parent) {
|
18 | var text = parent[key + '_comment'];
|
19 |
|
20 | if (text)
|
21 | return text;
|
22 | else
|
23 | return null;
|
24 | }
|
25 |
|
26 |
|
27 | function isObject(obj) {
|
28 | return obj === Object(obj)
|
29 | }
|
30 |
|
31 | function isArray(obj) {
|
32 | return Array.isArray(obj)
|
33 | }
|
34 |
|
35 | function pbxWriter(contents) {
|
36 | this.contents = contents;
|
37 | this.sync = false;
|
38 | this.indentLevel = 0;
|
39 | }
|
40 |
|
41 | util.inherits(pbxWriter, EventEmitter);
|
42 |
|
43 | pbxWriter.prototype.write = function (str) {
|
44 | var fmt = f.apply(null, arguments);
|
45 |
|
46 | if (this.sync) {
|
47 | this.buffer += f("%s%s", i(this.indentLevel), fmt);
|
48 | } else {
|
49 |
|
50 | }
|
51 | }
|
52 |
|
53 | pbxWriter.prototype.writeFlush = function (str) {
|
54 | var oldIndent = this.indentLevel;
|
55 |
|
56 | this.indentLevel = 0;
|
57 |
|
58 | this.write.apply(this, arguments)
|
59 |
|
60 | this.indentLevel = oldIndent;
|
61 | }
|
62 |
|
63 | pbxWriter.prototype.writeSync = function () {
|
64 | this.sync = true;
|
65 | this.buffer = "";
|
66 |
|
67 | this.writeHeadComment();
|
68 | this.writeProject();
|
69 |
|
70 | return this.buffer;
|
71 | }
|
72 |
|
73 | pbxWriter.prototype.writeHeadComment = function () {
|
74 | if (this.contents.headComment) {
|
75 | this.write("// %s\n", this.contents.headComment)
|
76 | }
|
77 | }
|
78 |
|
79 | pbxWriter.prototype.writeProject = function () {
|
80 | var proj = this.contents.project,
|
81 | key, cmt, obj;
|
82 |
|
83 | this.write("{\n")
|
84 |
|
85 | if (proj) {
|
86 | this.indentLevel++;
|
87 |
|
88 | for (key in proj) {
|
89 |
|
90 | if (COMMENT_KEY.test(key)) continue;
|
91 |
|
92 | cmt = comment(key, proj);
|
93 | obj = proj[key];
|
94 |
|
95 | if (isArray(obj)) {
|
96 | this.writeArray(obj, key)
|
97 | } else if (isObject(obj)) {
|
98 | this.write("%s = {\n", key);
|
99 | this.indentLevel++;
|
100 |
|
101 | if (key === 'objects') {
|
102 | this.writeObjectsSections(obj)
|
103 | } else {
|
104 | this.writeObject(obj)
|
105 | }
|
106 |
|
107 | this.indentLevel--;
|
108 | this.write("};\n");
|
109 | } else if (cmt) {
|
110 | this.write("%s = %s /* %s */;\n", key, obj, cmt)
|
111 | } else {
|
112 | this.write("%s = %s;\n", key, obj)
|
113 | }
|
114 | }
|
115 |
|
116 | this.indentLevel--;
|
117 | }
|
118 |
|
119 | this.write("}\n")
|
120 | }
|
121 |
|
122 | pbxWriter.prototype.writeObject = function (object) {
|
123 | var key, obj, cmt;
|
124 |
|
125 | for (key in object) {
|
126 | if (COMMENT_KEY.test(key)) continue;
|
127 |
|
128 | cmt = comment(key, object);
|
129 | obj = object[key];
|
130 |
|
131 | if (isArray(obj)) {
|
132 | this.writeArray(obj, key)
|
133 | } else if (isObject(obj)) {
|
134 | this.write("%s = {\n", key);
|
135 | this.indentLevel++;
|
136 |
|
137 | this.writeObject(obj)
|
138 |
|
139 | this.indentLevel--;
|
140 | this.write("};\n");
|
141 | } else {
|
142 | if (cmt) {
|
143 | this.write("%s = %s /* %s */;\n", key, obj, cmt)
|
144 | } else {
|
145 | this.write("%s = %s;\n", key, obj)
|
146 | }
|
147 | }
|
148 | }
|
149 | }
|
150 |
|
151 | pbxWriter.prototype.writeObjectsSections = function (objects) {
|
152 | var first = true,
|
153 | key, obj;
|
154 |
|
155 | for (key in objects) {
|
156 | if (!first) {
|
157 | this.writeFlush("\n")
|
158 | } else {
|
159 | first = false;
|
160 | }
|
161 |
|
162 | obj = objects[key];
|
163 |
|
164 | if (isObject(obj)) {
|
165 | this.writeSectionComment(key, true);
|
166 |
|
167 | this.writeSection(obj);
|
168 |
|
169 | this.writeSectionComment(key, false);
|
170 | }
|
171 | }
|
172 | }
|
173 |
|
174 | pbxWriter.prototype.writeArray = function (arr, name) {
|
175 | var i, entry;
|
176 |
|
177 | this.write("%s = (\n", name);
|
178 | this.indentLevel++;
|
179 |
|
180 | for (i=0; i < arr.length; i++) {
|
181 | entry = arr[i]
|
182 |
|
183 | if (entry.value && entry.comment) {
|
184 | this.write('%s /* %s */,\n', entry.value, entry.comment);
|
185 | } else if (isObject(entry)) {
|
186 | this.write('{\n');
|
187 | this.indentLevel++;
|
188 |
|
189 | this.writeObject(entry);
|
190 |
|
191 | this.indentLevel--;
|
192 | this.write('},\n');
|
193 | } else {
|
194 | this.write('%s,\n', entry);
|
195 | }
|
196 | }
|
197 |
|
198 | this.indentLevel--;
|
199 | this.write(");\n");
|
200 | }
|
201 |
|
202 | pbxWriter.prototype.writeSectionComment = function (name, begin) {
|
203 | if (begin) {
|
204 | this.writeFlush("/* Begin %s section */\n", name)
|
205 | } else {
|
206 | this.writeFlush("/* End %s section */\n", name)
|
207 | }
|
208 | }
|
209 |
|
210 | pbxWriter.prototype.writeSection = function (section) {
|
211 | var key, obj, cmt;
|
212 |
|
213 |
|
214 | for (key in section) {
|
215 | if (COMMENT_KEY.test(key)) continue;
|
216 |
|
217 | cmt = comment(key, section);
|
218 | obj = section[key]
|
219 |
|
220 | if (obj.isa == 'PBXBuildFile' || obj.isa == 'PBXFileReference') {
|
221 | this.writeInlineObject(key, cmt, obj);
|
222 | } else {
|
223 | if (cmt) {
|
224 | this.write("%s /* %s */ = {\n", key, cmt);
|
225 | } else {
|
226 | this.write("%s = {\n", key);
|
227 | }
|
228 |
|
229 | this.indentLevel++
|
230 |
|
231 | this.writeObject(obj)
|
232 |
|
233 | this.indentLevel--
|
234 | this.write("};\n");
|
235 | }
|
236 | }
|
237 | }
|
238 |
|
239 | pbxWriter.prototype.writeInlineObject = function (n, d, r) {
|
240 | var output = [];
|
241 |
|
242 | var inlineObjectHelper = function (name, desc, ref) {
|
243 | var key, cmt, obj;
|
244 |
|
245 | if (desc) {
|
246 | output.push(f("%s /* %s */ = {", name, desc));
|
247 | } else {
|
248 | output.push(f("%s = {", name));
|
249 | }
|
250 |
|
251 | for (key in ref) {
|
252 | if (COMMENT_KEY.test(key)) continue;
|
253 |
|
254 | cmt = comment(key, ref);
|
255 | obj = ref[key];
|
256 |
|
257 | if (isArray(obj)) {
|
258 | output.push(f("%s = (", key));
|
259 |
|
260 | for (var i=0; i < obj.length; i++) {
|
261 | output.push(f("%s, ", obj[i]))
|
262 | }
|
263 |
|
264 | output.push("); ");
|
265 | } else if (isObject(obj)) {
|
266 | inlineObjectHelper(key, cmt, obj)
|
267 | } else if (cmt) {
|
268 | output.push(f("%s = %s /* %s */; ", key, obj, cmt))
|
269 | } else {
|
270 | output.push(f("%s = %s; ", key, obj))
|
271 | }
|
272 | }
|
273 |
|
274 | output.push("}; ");
|
275 | }
|
276 |
|
277 | inlineObjectHelper(n, d, r);
|
278 |
|
279 | this.write("%s\n", output.join('').trim());
|
280 | }
|
281 |
|
282 | module.exports = pbxWriter;
|