Skip to content

Commit 8ee07b6

Browse files
committed
pbxWriter object
1 parent 42fd1ec commit 8ee07b6

File tree

3 files changed

+247
-1
lines changed

3 files changed

+247
-1
lines changed

lib/pbxProject.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var util = require('util'),
22
EventEmitter = require('events').EventEmitter,
33
path = require('path'),
4-
fork = require('child_process').fork
4+
fork = require('child_process').fork,
5+
pbxWriter = require('./pbxWriter')
56

67
function pbxProject(filename) {
78
this.filepath = path.resolve(filename)
@@ -27,4 +28,9 @@ pbxProject.prototype.parse = function (cb) {
2728
return this;
2829
}
2930

31+
pbxProject.prototype.writeSync = function () {
32+
this.writer = new pbxWriter(this.hash);
33+
return this.writer.writeSync();
34+
}
35+
3036
module.exports = pbxProject;

lib/pbxWriter.js

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
var pbxProj = require('./pbxProject'),
2+
util = require('util'),
3+
f = util.format,
4+
INDENT = ' ',
5+
COMMENT_KEY = /_comment$/,
6+
EventEmitter = require('events').EventEmitter
7+
8+
// indentation
9+
function i(x) {
10+
if (x <=0)
11+
return '';
12+
else
13+
return INDENT + i(x-1);
14+
}
15+
16+
function comment(key, parent) {
17+
var text = parent[key + '_comment'];
18+
19+
if (text)
20+
return text;
21+
else
22+
return null;
23+
}
24+
25+
// copied from underscore
26+
function isObject(obj) {
27+
return obj === Object(obj)
28+
}
29+
30+
function isArray(obj) {
31+
return Array.isArray(obj)
32+
}
33+
34+
function pbxWriter(contents) {
35+
this.contents = contents;
36+
this.sync = false;
37+
this.indentLevel = 0;
38+
}
39+
40+
util.inherits(pbxWriter, EventEmitter);
41+
42+
pbxWriter.prototype.write = function (str) {
43+
var fmt = f.apply(null, arguments);
44+
45+
if (this.sync) {
46+
this.buffer += f("%s%s", i(this.indentLevel), fmt);
47+
} else {
48+
// do stream write
49+
}
50+
}
51+
52+
pbxWriter.prototype.writeSync = function () {
53+
this.sync = true;
54+
this.buffer = "";
55+
56+
this.writeHeadComment();
57+
this.writeProject();
58+
59+
return this.buffer;
60+
}
61+
62+
pbxWriter.prototype.writeHeadComment = function () {
63+
if (this.contents.headComment) {
64+
this.write("// %s\n", this.contents.headComment)
65+
}
66+
}
67+
68+
pbxWriter.prototype.writeProject = function () {
69+
var proj = this.contents.project,
70+
key, cmt, obj;
71+
72+
this.write("{\n")
73+
74+
if (proj) {
75+
this.indentLevel++;
76+
77+
for (key in proj) {
78+
// skip comments
79+
if (COMMENT_KEY.test(key)) continue;
80+
81+
cmt = comment(key, proj);
82+
obj = proj[key];
83+
84+
if (isArray(obj)) {
85+
this.write("%s = (\n", key);
86+
this.indentLevel++;
87+
88+
this.writeArray(obj)
89+
90+
this.indentLevel--;
91+
this.write(");\n");
92+
} else if (isObject(obj)) {
93+
this.write("%s = {\n", key);
94+
this.indentLevel++;
95+
96+
if (key === 'objects') {
97+
this.writeObjectsSections(obj)
98+
} else {
99+
this.writeObject(obj)
100+
}
101+
102+
this.indentLevel--;
103+
this.write("};\n");
104+
} else if (cmt) {
105+
this.write("%s = %s /* %s */;\n", key, obj, cmt)
106+
} else {
107+
this.write("%s = %s;\n", key, obj)
108+
}
109+
}
110+
111+
this.indentLevel--;
112+
}
113+
114+
this.write("}\n")
115+
}
116+
117+
pbxWriter.prototype.writeObject = function (object) {
118+
var key, obj, cmt;
119+
120+
for (key in object) {
121+
if (COMMENT_KEY.test(key)) continue;
122+
123+
cmt = comment(key, object);
124+
obj = object[key];
125+
126+
if (cmt) {
127+
this.write("%s = %s /* %s */;\n", key, obj, cmt)
128+
} else {
129+
this.write("%s = %s;\n", key, obj)
130+
}
131+
}
132+
}
133+
134+
pbxWriter.prototype.writeObjectsSections = function (objects) {
135+
var key, obj;
136+
137+
for (key in objects) {
138+
obj = objects[key];
139+
140+
if (isObject(obj)) {
141+
this.writeSectionComment(key, true);
142+
143+
this.writeSection(obj);
144+
145+
this.writeSectionComment(key, false);
146+
}
147+
}
148+
}
149+
150+
pbxWriter.prototype.writeArray = function (arr) {
151+
var i, entry;
152+
153+
for (i=0; i < arr.length; i++) {
154+
entry = arr[i]
155+
156+
if (entry.value && entry.comment) {
157+
this.write('%s /* %s */,\n', entry.value, entry.comment);
158+
} else {
159+
this.write('%s,\n', entry);
160+
}
161+
}
162+
}
163+
164+
pbxWriter.prototype.writeSectionComment = function (name, begin) {
165+
var oldIndent = this.indentLevel;
166+
167+
this.indentLevel = 0;
168+
169+
if (begin) {
170+
this.write("/* Begin %s section */\n", name)
171+
} else { // end
172+
this.write("/* End %s section */\n", name)
173+
}
174+
175+
this.indentLevel = oldIndent;
176+
}
177+
178+
pbxWriter.prototype.writeSection = function (section) {
179+
var id = section.id,
180+
comment = section[id + '_comment'],
181+
object = section[id];
182+
183+
this.write("%s /* %s */ = {\n", id, comment);
184+
185+
this.indentLevel++
186+
187+
this.writeObject(object)
188+
189+
this.indentLevel--
190+
191+
this.write("};\n");
192+
}
193+
194+
module.exports = pbxWriter;

test/pbxWriter.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var pbx = require('../lib/pbxProject'),
2+
fs = require('fs'),
3+
myProj;
4+
5+
function testProjectContents(filename) {
6+
7+
}
8+
9+
exports.writeSync = {
10+
'should write out the simple "hash" test': function (test) {
11+
var file = 'test/parser/projects/hash.pbxproj',
12+
myProj = new pbx(file),
13+
contents = fs.readFileSync(file, 'utf-8');
14+
15+
myProj.parse(function (err, projHash) {
16+
var written = myProj.writeSync();
17+
18+
test.equal(written, contents);
19+
test.done();
20+
})
21+
},
22+
'should write out the simple "with_array" test': function (test) {
23+
var file = 'test/parser/projects/with_array.pbxproj',
24+
myProj = new pbx(file),
25+
contents = fs.readFileSync(file, 'utf-8');
26+
27+
myProj.parse(function (err, projHash) {
28+
var written = myProj.writeSync();
29+
30+
test.equal(written, contents);
31+
test.done();
32+
})
33+
},
34+
'should write out the simple "section" test': function (test) {
35+
var file = 'test/parser/projects/section.pbxproj',
36+
myProj = new pbx(file),
37+
contents = fs.readFileSync(file, 'utf-8');
38+
39+
myProj.parse(function (err, projHash) {
40+
var written = myProj.writeSync();
41+
42+
test.equal(written, contents);
43+
test.done();
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)