Skip to content

Commit 0922e36

Browse files
committed
[pbxWriter] write out build-config
involves a couple of changes to writing out quoted values with escaped quotes inside of them - uses regex, so may be brittle
1 parent 463c925 commit 0922e36

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

lib/pbxWriter.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var pbxProj = require('./pbxProject'),
33
f = util.format,
44
INDENT = '\t',
55
COMMENT_KEY = /_comment$/,
6+
QUOTED = /^"(.*)"$/,
67
EventEmitter = require('events').EventEmitter
78

89
// indentation
@@ -37,6 +38,12 @@ function pbxWriter(contents) {
3738
this.indentLevel = 0;
3839
}
3940

41+
function escapeInternals(str) {
42+
return str.replace(QUOTED, function (orig, middle) {
43+
return '"' + middle.replace(/"/g, '\\"') + '"';
44+
});
45+
}
46+
4047
util.inherits(pbxWriter, EventEmitter);
4148

4249
pbxWriter.prototype.write = function (str) {
@@ -129,10 +136,23 @@ pbxWriter.prototype.writeObject = function (object) {
129136

130137
if (isArray(obj)) {
131138
this.writeArray(obj, key)
132-
} else if (cmt) {
133-
this.write("%s = %s /* %s */;\n", key, obj, cmt)
139+
} else if (isObject(obj)) {
140+
this.write("%s = {\n", key);
141+
this.indentLevel++;
142+
143+
this.writeObject(obj)
144+
145+
this.indentLevel--;
146+
this.write("};\n");
134147
} else {
135-
this.write("%s = %s;\n", key, obj)
148+
if (QUOTED.test(obj))
149+
obj = escapeInternals(obj);
150+
151+
if (cmt) {
152+
this.write("%s = %s /* %s */;\n", key, obj, cmt)
153+
} else {
154+
this.write("%s = %s;\n", key, obj)
155+
}
136156
}
137157
}
138158
}

test/parser/projects/build-config.pbxproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
};
66
objectVersion = 45;
77
objects = {
8-
9-
108
/* Begin XCBuildConfiguration section */
119
1D6058940D05DD3E006BFB54 /* Debug */ = {
1210
isa = XCBuildConfiguration;
@@ -109,7 +107,6 @@
109107
name = Release;
110108
};
111109
/* End XCBuildConfiguration section */
112-
113110
};
114111
rootObject = 29B97313FDCFA39411CA2CEA /* Project object */;
115112
}

test/pbxWriter.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ function testProjectContents(filename, test) {
1717
});
1818
}
1919

20+
// for debugging failing tests
21+
function testContentsInDepth(filename, test) {
22+
var myProj = new pbx(filename),
23+
content = fs.readFileSync(filename, 'utf-8');
24+
25+
// normalize tabs vs strings
26+
content = content.replace(/ /g, '\t');
27+
28+
myProj.parse(function (err, projHash) {
29+
var written = myProj.writeSync(),
30+
writtenLines = written.split('\n')
31+
contentLines = content.split('\n')
32+
33+
test.equal(writtenLines.length, contentLines.length);
34+
35+
test.done();
36+
});
37+
}
38+
2039
exports.writeSync = {
2140
'should write out the "hash" test': function (test) {
2241
testProjectContents('test/parser/projects/hash.pbxproj', test);
@@ -29,5 +48,11 @@ exports.writeSync = {
2948
},
3049
'should write out the "two-sections" test': function (test) {
3150
testProjectContents('test/parser/projects/two-sections.pbxproj', test);
51+
},
52+
'should write out the "section-entries" test': function (test) {
53+
testProjectContents('test/parser/projects/section-entries.pbxproj', test);
54+
},
55+
'should write out the "build-config" test': function (test) {
56+
testProjectContents('test/parser/projects/build-config.pbxproj', test);
3257
}
3358
}

0 commit comments

Comments
 (0)