Skip to content

Commit 582bed5

Browse files
committed
[writer] handle multiple sections
1 parent 8ee07b6 commit 582bed5

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

lib/pbxWriter.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pbxWriter.prototype.write = function (str) {
4949
}
5050
}
5151

52+
pbxWriter.prototype.writeFlush = function (str) {
53+
var oldIndent = this.indentLevel;
54+
55+
this.indentLevel = 0;
56+
57+
this.write.apply(this, arguments)
58+
59+
this.indentLevel = oldIndent;
60+
}
61+
5262
pbxWriter.prototype.writeSync = function () {
5363
this.sync = true;
5464
this.buffer = "";
@@ -82,13 +92,7 @@ pbxWriter.prototype.writeProject = function () {
8292
obj = proj[key];
8393

8494
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");
95+
this.writeArray(obj, key)
9296
} else if (isObject(obj)) {
9397
this.write("%s = {\n", key);
9498
this.indentLevel++;
@@ -123,7 +127,9 @@ pbxWriter.prototype.writeObject = function (object) {
123127
cmt = comment(key, object);
124128
obj = object[key];
125129

126-
if (cmt) {
130+
if (isArray(obj)) {
131+
this.writeArray(obj, key)
132+
} else if (cmt) {
127133
this.write("%s = %s /* %s */;\n", key, obj, cmt)
128134
} else {
129135
this.write("%s = %s;\n", key, obj)
@@ -132,9 +138,16 @@ pbxWriter.prototype.writeObject = function (object) {
132138
}
133139

134140
pbxWriter.prototype.writeObjectsSections = function (objects) {
135-
var key, obj;
141+
var first = true,
142+
key, obj;
136143

137144
for (key in objects) {
145+
if (!first) {
146+
this.writeFlush("\n")
147+
} else {
148+
first = false;
149+
}
150+
138151
obj = objects[key];
139152

140153
if (isObject(obj)) {
@@ -147,9 +160,12 @@ pbxWriter.prototype.writeObjectsSections = function (objects) {
147160
}
148161
}
149162

150-
pbxWriter.prototype.writeArray = function (arr) {
163+
pbxWriter.prototype.writeArray = function (arr, name) {
151164
var i, entry;
152165

166+
this.write("%s = (\n", name);
167+
this.indentLevel++;
168+
153169
for (i=0; i < arr.length; i++) {
154170
entry = arr[i]
155171

@@ -159,20 +175,17 @@ pbxWriter.prototype.writeArray = function (arr) {
159175
this.write('%s,\n', entry);
160176
}
161177
}
178+
179+
this.indentLevel--;
180+
this.write(");\n");
162181
}
163182

164183
pbxWriter.prototype.writeSectionComment = function (name, begin) {
165-
var oldIndent = this.indentLevel;
166-
167-
this.indentLevel = 0;
168-
169184
if (begin) {
170-
this.write("/* Begin %s section */\n", name)
185+
this.writeFlush("/* Begin %s section */\n", name)
171186
} else { // end
172-
this.write("/* End %s section */\n", name)
187+
this.writeFlush("/* End %s section */\n", name)
173188
}
174-
175-
this.indentLevel = oldIndent;
176189
}
177190

178191
pbxWriter.prototype.writeSection = function (section) {

test/pbxWriter.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,32 @@ var pbx = require('../lib/pbxProject'),
22
fs = require('fs'),
33
myProj;
44

5-
function testProjectContents(filename) {
5+
function testProjectContents(filename, test) {
6+
var myProj = new pbx(filename),
7+
content = fs.readFileSync(filename, 'utf-8');
68

7-
}
9+
// normalize tabs vs strings
10+
content = content.replace(/\t/g, ' ');
811

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');
12+
myProj.parse(function (err, projHash) {
13+
var written = myProj.writeSync();
1414

15-
myProj.parse(function (err, projHash) {
16-
var written = myProj.writeSync();
15+
test.equal(content, written);
16+
test.done();
17+
});
18+
}
1719

18-
test.equal(written, contents);
19-
test.done();
20-
})
20+
exports.writeSync = {
21+
'should write out the "hash" test': function (test) {
22+
testProjectContents('test/parser/projects/hash.pbxproj', test);
2123
},
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-
})
24+
'should write out the "with_array" test': function (test) {
25+
testProjectContents('test/parser/projects/with_array.pbxproj', test);
3326
},
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-
})
27+
'should write out the "section" test': function (test) {
28+
testProjectContents('test/parser/projects/section.pbxproj', test);
29+
},
30+
'should write out the "two-sections" test': function (test) {
31+
testProjectContents('test/parser/projects/two-sections.pbxproj', test);
4532
}
4633
}

0 commit comments

Comments
 (0)