Skip to content

Commit 1792611

Browse files
committed
added multiple target support
1 parent c9a3ee5 commit 1792611

File tree

3 files changed

+219
-20
lines changed

3 files changed

+219
-20
lines changed

lib/pbxProject.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ pbxProject.prototype.removePluginFile = function (path, opt) {
108108
return file;
109109
}
110110

111+
111112
pbxProject.prototype.addSourceFile = function (path, opt) {
113+
112114
var file = this.addPluginFile(path, opt);
113-
114115
if (!file) return false;
115116

117+
file.target = opt ? opt.target : undefined;
116118
file.uuid = this.generateUuid();
117119

118120
this.addToPbxBuildFileSection(file); // PBXBuildFile
@@ -121,8 +123,10 @@ pbxProject.prototype.addSourceFile = function (path, opt) {
121123
return file;
122124
}
123125

126+
124127
pbxProject.prototype.removeSourceFile = function (path, opt) {
125128
var file = this.removePluginFile(path, opt)
129+
file.target = opt ? opt.target : undefined;
126130
this.removeFromPbxBuildFileSection(file); // PBXBuildFile
127131
this.removeFromPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
128132

@@ -151,6 +155,7 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
151155
}
152156

153157
file.uuid = this.generateUuid();
158+
file.target = opt ? opt.target : undefined;
154159

155160
if (!opt.plugin) {
156161
correctForResourcesPath(file, this);
@@ -170,6 +175,7 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
170175

171176
pbxProject.prototype.removeResourceFile = function (path, opt) {
172177
var file = new pbxFile(path, opt);
178+
file.target = opt ? opt.target : undefined;
173179

174180
correctForResourcesPath(file, this);
175181

@@ -187,7 +193,9 @@ pbxProject.prototype.addFramework = function (fpath, opt) {
187193
if (this.hasFile(file.path)) return false;
188194

189195
file.uuid = this.generateUuid();
190-
file.fileRef = this.generateUuid();
196+
file.fileRef = this.generateUuid();
197+
file.target = opt ? opt.target : undefined;
198+
191199

192200
this.addToPbxBuildFileSection(file); // PBXBuildFile
193201
this.addToPbxFileReferenceSection(file); // PBXFileReference
@@ -203,6 +211,7 @@ pbxProject.prototype.addFramework = function (fpath, opt) {
203211

204212
pbxProject.prototype.removeFramework = function (fpath, opt) {
205213
var file = new pbxFile(fpath, opt);
214+
file.target = opt ? opt.target : undefined;
206215

207216
this.removeFromPbxBuildFileSection(file); // PBXBuildFile
208217
this.removeFromPbxFileReferenceSection(file); // PBXFileReference
@@ -230,6 +239,7 @@ pbxProject.prototype.addStaticLibrary = function (path, opt) {
230239
}
231240

232241
file.uuid = this.generateUuid();
242+
file.target = opt ? opt.target : undefined;
233243

234244
if (!opt.plugin) {
235245
file.fileRef = this.generateUuid();
@@ -341,14 +351,14 @@ pbxProject.prototype.removeFromFrameworksPbxGroup = function (file) {
341351
}
342352
}
343353
}
344-
345354
pbxProject.prototype.addToPbxSourcesBuildPhase = function (file) {
346-
var sources = this.pbxSourcesBuildPhaseObj();
355+
var sources = this.pbxSourcesBuildPhaseObj(file.target);
347356
sources.files.push(pbxBuildPhaseObj(file));
348357
}
349358

350359
pbxProject.prototype.removeFromPbxSourcesBuildPhase = function (file) {
351-
var sources = this.pbxSourcesBuildPhaseObj(), i;
360+
361+
var sources = this.pbxSourcesBuildPhaseObj(file.target), i;
352362
for(i in sources.files) {
353363
if(sources.files[i].comment == longComment(file)) {
354364
sources.files.splice(i, 1);
@@ -358,12 +368,12 @@ pbxProject.prototype.removeFromPbxSourcesBuildPhase = function (file) {
358368
}
359369

360370
pbxProject.prototype.addToPbxResourcesBuildPhase = function (file) {
361-
var sources = this.pbxResourcesBuildPhaseObj();
371+
var sources = this.pbxResourcesBuildPhaseObj(file.target);
362372
sources.files.push(pbxBuildPhaseObj(file));
363373
}
364374

365375
pbxProject.prototype.removeFromPbxResourcesBuildPhase = function (file) {
366-
var sources = this.pbxResourcesBuildPhaseObj(), i;
376+
var sources = this.pbxResourcesBuildPhaseObj(file.target), i;
367377

368378
for(i in sources.files) {
369379
if(sources.files[i].comment == longComment(file)) {
@@ -374,12 +384,12 @@ pbxProject.prototype.removeFromPbxResourcesBuildPhase = function (file) {
374384
}
375385

376386
pbxProject.prototype.addToPbxFrameworksBuildPhase = function (file) {
377-
var sources = this.pbxFrameworksBuildPhaseObj();
387+
var sources = this.pbxFrameworksBuildPhaseObj(file.target);
378388
sources.files.push(pbxBuildPhaseObj(file));
379389
}
380390

381391
pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) {
382-
var sources = this.pbxFrameworksBuildPhaseObj();
392+
var sources = this.pbxFrameworksBuildPhaseObj(file.target);
383393
for(i in sources.files) {
384394
if(sources.files[i].comment == longComment(file)) {
385395
sources.files.splice(i, 1);
@@ -401,6 +411,14 @@ pbxProject.prototype.pbxFileReferenceSection = function () {
401411
return this.hash.project.objects['PBXFileReference'];
402412
}
403413

414+
pbxProject.prototype.pbxNativeTarget = function () {
415+
return this.hash.project.objects['PBXNativeTarget'];
416+
}
417+
418+
pbxProject.prototype.pbxXCConfigurationList = function () {
419+
return this.hash.project.objects['XCConfigurationList'];
420+
}
421+
404422
pbxProject.prototype.pbxGroupByName = function (name) {
405423
var groups = this.hash.project.objects['PBXGroup'],
406424
key, groupKey;
@@ -418,35 +436,62 @@ pbxProject.prototype.pbxGroupByName = function (name) {
418436
return null;
419437
}
420438

421-
pbxProject.prototype.pbxSourcesBuildPhaseObj = function () {
422-
return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources');
439+
pbxProject.prototype.pbxSourcesBuildPhaseObj = function (target) {
440+
return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources', target);
423441
}
424442

425-
pbxProject.prototype.pbxResourcesBuildPhaseObj = function () {
426-
return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources');
443+
pbxProject.prototype.pbxResourcesBuildPhaseObj = function (target) {
444+
return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources',target);
427445
}
428446

429-
pbxProject.prototype.pbxFrameworksBuildPhaseObj = function () {
430-
return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks');
447+
pbxProject.prototype.pbxFrameworksBuildPhaseObj = function (target) {
448+
return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks',target);
431449
}
432450

433-
pbxProject.prototype.buildPhaseObject = function (name, group) {
451+
// Find Build Phase from group/target
452+
pbxProject.prototype.buildPhase = function (group,target) {
453+
454+
if (!target)
455+
return undefined;
456+
457+
var nativeTargets = this.pbxNativeTarget();
458+
if (typeof nativeTargets[target] == "undefined")
459+
throw new Error("Invalid target: "+target);
460+
461+
var nativeTarget= nativeTargets[target];
462+
var buildPhases = nativeTarget.buildPhases;
463+
for(var i in buildPhases)
464+
{
465+
var buildPhase = buildPhases[i];
466+
if (buildPhase.comment==group)
467+
return buildPhase.value+"_comment";
468+
}
469+
}
470+
471+
pbxProject.prototype.buildPhaseObject = function (name, group,target) {
434472
var section = this.hash.project.objects[name],
435473
obj, sectionKey, key;
436474

475+
var buildPhase = this.buildPhase(group,target);
476+
437477
for (key in section) {
478+
438479
// only look for comments
439480
if (!COMMENT_KEY.test(key)) continue;
440-
481+
482+
// select the proper buildPhase
483+
if (buildPhase && buildPhase!=key)
484+
continue;
485+
441486
if (section[key] == group) {
442-
sectionKey = key.split(COMMENT_KEY)[0];
487+
sectionKey = key.split(COMMENT_KEY)[0];
443488
return section[sectionKey];
444489
}
445-
}
446-
490+
}
447491
return null;
448492
}
449493

494+
450495
pbxProject.prototype.updateBuildProperty = function(prop, value) {
451496
var config = this.pbxXCBuildConfigurationSection();
452497
propReplace(config, prop, value);

test/fixtures/multiple-targets.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/multipleTargets.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
var fullProject = require('./fixtures/multiple-targets')
2+
fullProjectStr = JSON.stringify(fullProject),
3+
pbx = require('../lib/pbxProject'),
4+
pbxFile = require('../lib/pbxFile'),
5+
proj = new pbx('.');
6+
7+
function cleanHash() {
8+
return JSON.parse(fullProjectStr);
9+
}
10+
11+
exports.setUp = function (callback) {
12+
proj.hash = cleanHash();
13+
callback();
14+
}
15+
16+
exports.addFilesToTarget = {
17+
'should add the file to a proper target': function (test) {
18+
19+
var target = "1D6058900D05DD3D006BFB54";
20+
var filename = "file.m";
21+
22+
var opt = { target : target };
23+
var newFile = proj.addSourceFile(filename,opt);
24+
25+
test.equal(newFile.constructor, pbxFile);
26+
27+
var sources = proj.pbxSourcesBuildPhaseObj(target);
28+
test.equal(sources.files[5].comment, filename+" in Sources");
29+
30+
test.done();
31+
},
32+
'should remove the file from the proper target': function (test) {
33+
34+
var target = "1D6058900D05DD3D006BFB54";
35+
var filename = "file.m";
36+
37+
var opt = { target : target };
38+
var newFile = proj.addSourceFile(filename,opt);
39+
40+
test.equal(newFile.constructor, pbxFile);
41+
42+
var sources = proj.pbxSourcesBuildPhaseObj(target);
43+
test.equal(sources.files[5].comment, filename+" in Sources");
44+
var l = sources.files.length;
45+
46+
proj.removeSourceFile(filename,opt);
47+
var sources = proj.pbxSourcesBuildPhaseObj(target);
48+
test.equal(sources.files.length,l-1);
49+
50+
test.done();
51+
},
52+
'should fail when specifying an invalid target': function (test) {
53+
54+
var target = "XXXXX";
55+
var filename = "file.m";
56+
57+
var opt = { target : target };
58+
test.throws(function(){
59+
proj.addSourceFile(filename,opt);
60+
});
61+
62+
63+
test.done();
64+
},
65+
'should add the library to a proper target': function (test) {
66+
67+
var target = "1D6058900D05DD3D006BFB54";
68+
var filename = "library.lib";
69+
70+
var opt = { target : target };
71+
var newFile = proj.addStaticLibrary(filename,opt);
72+
73+
test.equal(newFile.constructor, pbxFile);
74+
75+
var libraries = proj.pbxFrameworksBuildPhaseObj(target);
76+
test.equal(libraries.files[4].comment, filename+" in Resources");
77+
78+
test.done();
79+
},
80+
'should remove the library to a proper target': function (test) {
81+
82+
var target = "1D6058900D05DD3D006BFB54";
83+
var filename = "library.lib";
84+
85+
var opt = { target : target };
86+
var newFile = proj.addStaticLibrary(filename,opt);
87+
88+
test.equal(newFile.constructor, pbxFile);
89+
90+
var libraries = proj.pbxFrameworksBuildPhaseObj(target);
91+
test.equal(libraries.files[4].comment, filename+" in Resources");
92+
var l = libraries.files.length;
93+
94+
proj.removeFramework(filename,opt);
95+
var libraries = proj.pbxFrameworksBuildPhaseObj(target);
96+
test.equal(libraries.files.length,l-1);
97+
98+
test.done();
99+
100+
},
101+
'should add the framework to a proper target': function (test) {
102+
103+
var target = "1D6058900D05DD3D006BFB54";
104+
var filename = "delta.framework";
105+
106+
var opt = { target : target };
107+
var newFile = proj.addFramework(filename,opt);
108+
109+
test.equal(newFile.constructor, pbxFile);
110+
111+
var frameworks = proj.pbxFrameworksBuildPhaseObj(target);
112+
test.equal(frameworks.files[4].comment, filename+" in Frameworks");
113+
114+
test.done();
115+
},
116+
'should add a ressource fileto a proper target': function (test) {
117+
118+
var target = "1D6058900D05DD3D006BFB54";
119+
var filename = "delta.png";
120+
121+
var opt = { target : target };
122+
var newFile = proj.addResourceFile(filename,opt);
123+
124+
test.equal(newFile.constructor, pbxFile);
125+
126+
var resources = proj.pbxResourcesBuildPhaseObj(target);
127+
test.equal(resources.files[26].comment, filename+" in Resources");
128+
129+
test.done();
130+
},
131+
'should remove a ressource file from a proper target': function (test) {
132+
133+
var target = "1D6058900D05DD3D006BFB54";
134+
var filename = "delta.png";
135+
136+
var opt = { target : target };
137+
var newFile = proj.addResourceFile(filename,opt);
138+
139+
test.equal(newFile.constructor, pbxFile);
140+
141+
var resources = proj.pbxResourcesBuildPhaseObj(target);
142+
test.equal(resources.files[26].comment, filename+" in Resources");
143+
144+
var l = resources.files.length;
145+
146+
proj.removeResourceFile(filename,opt);
147+
var resources = proj.pbxResourcesBuildPhaseObj(target);
148+
test.equal(resources.files.length,l-1);
149+
150+
test.done();
151+
},
152+
}
153+

0 commit comments

Comments
 (0)