Skip to content

Commit b5f5764

Browse files
ogoguele2l3n
authored andcommitted
added multiple target support
1 parent 630d4a4 commit b5f5764

File tree

3 files changed

+221
-20
lines changed

3 files changed

+221
-20
lines changed

lib/pbxProject.js

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

112+
112113
pbxProject.prototype.addSourceFile = function (path, opt) {
114+
113115
var file = this.addPluginFile(path, opt);
114-
115116
if (!file) return false;
116117

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

119121
this.addToPbxBuildFileSection(file); // PBXBuildFile
@@ -122,8 +124,10 @@ pbxProject.prototype.addSourceFile = function (path, opt) {
122124
return file;
123125
}
124126

127+
125128
pbxProject.prototype.removeSourceFile = function (path, opt) {
126129
var file = this.removePluginFile(path, opt)
130+
file.target = opt ? opt.target : undefined;
127131
this.removeFromPbxBuildFileSection(file); // PBXBuildFile
128132
this.removeFromPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
129133

@@ -152,6 +156,7 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
152156
}
153157

154158
file.uuid = this.generateUuid();
159+
file.target = opt ? opt.target : undefined;
155160

156161
if (!opt.plugin) {
157162
correctForResourcesPath(file, this);
@@ -171,6 +176,7 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
171176

172177
pbxProject.prototype.removeResourceFile = function (path, opt) {
173178
var file = new pbxFile(path, opt);
179+
file.target = opt ? opt.target : undefined;
174180

175181
correctForResourcesPath(file, this);
176182

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

190196
file.uuid = this.generateUuid();
191-
file.fileRef = this.generateUuid();
197+
file.fileRef = this.generateUuid();
198+
file.target = opt ? opt.target : undefined;
199+
192200

193201
this.addToPbxBuildFileSection(file); // PBXBuildFile
194202
this.addToPbxFileReferenceSection(file); // PBXFileReference
@@ -209,6 +217,7 @@ pbxProject.prototype.addFramework = function (fpath, opt) {
209217

210218
pbxProject.prototype.removeFramework = function (fpath, opt) {
211219
var file = new pbxFile(fpath, opt);
220+
file.target = opt ? opt.target : undefined;
212221

213222
this.removeFromPbxBuildFileSection(file); // PBXBuildFile
214223
this.removeFromPbxFileReferenceSection(file); // PBXFileReference
@@ -241,6 +250,7 @@ pbxProject.prototype.addStaticLibrary = function (path, opt) {
241250
}
242251

243252
file.uuid = this.generateUuid();
253+
file.target = opt ? opt.target : undefined;
244254

245255
if (!opt.plugin) {
246256
file.fileRef = this.generateUuid();
@@ -353,14 +363,14 @@ pbxProject.prototype.removeFromFrameworksPbxGroup = function (file) {
353363
}
354364
}
355365
}
356-
357366
pbxProject.prototype.addToPbxSourcesBuildPhase = function (file) {
358-
var sources = this.pbxSourcesBuildPhaseObj();
367+
var sources = this.pbxSourcesBuildPhaseObj(file.target);
359368
sources.files.push(pbxBuildPhaseObj(file));
360369
}
361370

362371
pbxProject.prototype.removeFromPbxSourcesBuildPhase = function (file) {
363-
var sources = this.pbxSourcesBuildPhaseObj(), i;
372+
373+
var sources = this.pbxSourcesBuildPhaseObj(file.target), i;
364374
for(i in sources.files) {
365375
if(sources.files[i].comment == longComment(file)) {
366376
sources.files.splice(i, 1);
@@ -370,12 +380,12 @@ pbxProject.prototype.removeFromPbxSourcesBuildPhase = function (file) {
370380
}
371381

372382
pbxProject.prototype.addToPbxResourcesBuildPhase = function (file) {
373-
var sources = this.pbxResourcesBuildPhaseObj();
383+
var sources = this.pbxResourcesBuildPhaseObj(file.target);
374384
sources.files.push(pbxBuildPhaseObj(file));
375385
}
376386

377387
pbxProject.prototype.removeFromPbxResourcesBuildPhase = function (file) {
378-
var sources = this.pbxResourcesBuildPhaseObj(), i;
388+
var sources = this.pbxResourcesBuildPhaseObj(file.target), i;
379389

380390
for(i in sources.files) {
381391
if(sources.files[i].comment == longComment(file)) {
@@ -386,12 +396,12 @@ pbxProject.prototype.removeFromPbxResourcesBuildPhase = function (file) {
386396
}
387397

388398
pbxProject.prototype.addToPbxFrameworksBuildPhase = function (file) {
389-
var sources = this.pbxFrameworksBuildPhaseObj();
399+
var sources = this.pbxFrameworksBuildPhaseObj(file.target);
390400
sources.files.push(pbxBuildPhaseObj(file));
391401
}
392402

393403
pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) {
394-
var sources = this.pbxFrameworksBuildPhaseObj();
404+
var sources = this.pbxFrameworksBuildPhaseObj(file.target);
395405
for(i in sources.files) {
396406
if(sources.files[i].comment == longComment(file)) {
397407
sources.files.splice(i, 1);
@@ -441,6 +451,14 @@ pbxProject.prototype.pbxFileReferenceSection = function () {
441451
return this.hash.project.objects['PBXFileReference'];
442452
}
443453

454+
pbxProject.prototype.pbxNativeTarget = function () {
455+
return this.hash.project.objects['PBXNativeTarget'];
456+
}
457+
458+
pbxProject.prototype.pbxXCConfigurationList = function () {
459+
return this.hash.project.objects['XCConfigurationList'];
460+
}
461+
444462
pbxProject.prototype.pbxGroupByName = function (name) {
445463
var groups = this.hash.project.objects['PBXGroup'],
446464
key, groupKey;
@@ -458,39 +476,66 @@ pbxProject.prototype.pbxGroupByName = function (name) {
458476
return null;
459477
}
460478

461-
pbxProject.prototype.pbxSourcesBuildPhaseObj = function () {
462-
return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources');
479+
pbxProject.prototype.pbxSourcesBuildPhaseObj = function (target) {
480+
return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources', target);
463481
}
464482

465-
pbxProject.prototype.pbxResourcesBuildPhaseObj = function () {
466-
return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources');
483+
pbxProject.prototype.pbxResourcesBuildPhaseObj = function (target) {
484+
return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources',target);
467485
}
468486

469-
pbxProject.prototype.pbxFrameworksBuildPhaseObj = function () {
470-
return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks');
487+
pbxProject.prototype.pbxFrameworksBuildPhaseObj = function (target) {
488+
return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks',target);
471489
}
472490

473491
pbxProject.prototype.pbxEmbedFrameworksBuildPhaseObj = function () {
474492
return this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Frameworks');
475493
}
476494

477-
pbxProject.prototype.buildPhaseObject = function (name, group) {
495+
// Find Build Phase from group/target
496+
pbxProject.prototype.buildPhase = function (group,target) {
497+
498+
if (!target)
499+
return undefined;
500+
501+
var nativeTargets = this.pbxNativeTarget();
502+
if (typeof nativeTargets[target] == "undefined")
503+
throw new Error("Invalid target: "+target);
504+
505+
var nativeTarget= nativeTargets[target];
506+
var buildPhases = nativeTarget.buildPhases;
507+
for(var i in buildPhases)
508+
{
509+
var buildPhase = buildPhases[i];
510+
if (buildPhase.comment==group)
511+
return buildPhase.value+"_comment";
512+
}
513+
}
514+
515+
pbxProject.prototype.buildPhaseObject = function (name, group, target) {
478516
var section = this.hash.project.objects[name],
479517
obj, sectionKey, key;
480518

519+
var buildPhase = this.buildPhase(group,target);
520+
481521
for (key in section) {
522+
482523
// only look for comments
483524
if (!COMMENT_KEY.test(key)) continue;
484-
525+
526+
// select the proper buildPhase
527+
if (buildPhase && buildPhase!=key)
528+
continue;
529+
485530
if (section[key] == group) {
486-
sectionKey = key.split(COMMENT_KEY)[0];
531+
sectionKey = key.split(COMMENT_KEY)[0];
487532
return section[sectionKey];
488533
}
489-
}
490-
534+
}
491535
return null;
492536
}
493537

538+
494539
pbxProject.prototype.updateBuildProperty = function(prop, value) {
495540
var config = this.pbxXCBuildConfigurationSection();
496541
propReplace(config, prop, value);
@@ -625,6 +670,7 @@ pbxProject.prototype.removeFromHeaderSearchPaths = function (file) {
625670

626671
}
627672
}
673+
628674
pbxProject.prototype.addToHeaderSearchPaths = function (file) {
629675
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
630676
INHERITED = '"$(inherited)"',
@@ -647,6 +693,7 @@ pbxProject.prototype.addToHeaderSearchPaths = function (file) {
647693
}
648694
}
649695
}
696+
650697
// a JS getter. hmmm
651698
pbxProject.prototype.__defineGetter__("productName", function () {
652699
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),

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)