Skip to content

Commit f2eb6ef

Browse files
author
Anis Kadri
committed
Merge branch 'Mitko-Kerezov-master'
2 parents c61762d + 3085e40 commit f2eb6ef

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

lib/pbxProject.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,37 @@ pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) {
398398
}
399399
}
400400

401+
pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) {
402+
var section = this.hash.project.objects[isa],
403+
buildPhaseUuid = this.generateUuid(),
404+
commentKey = f("%s_comment", buildPhaseUuid),
405+
buildPhase = {
406+
isa: isa,
407+
buildActionMask: 2147483647,
408+
files: [],
409+
runOnlyForDeploymentPostprocessing: 0
410+
};
411+
412+
for (var index = 0; index < filePathsArray.length; index++) {
413+
var filePath = filePathsArray[index],
414+
file = new pbxFile(filePath);
415+
416+
file.uuid = this.generateUuid();
417+
file.fileRef = this.generateUuid();
418+
this.addToPbxFileReferenceSection(file); // PBXFileReference
419+
this.addToPbxBuildFileSection(file); // PBXBuildFile
420+
buildPhase.files.push(pbxBuildPhaseObj(file));
421+
}
422+
423+
if (section) {
424+
section[buildPhaseUuid] = buildPhase;
425+
section[commentKey] = comment;
426+
}
427+
428+
buildPhase.uuid = buildPhaseUuid;
429+
return buildPhase;
430+
}
431+
401432
// helper access functions
402433
pbxProject.prototype.pbxBuildFileSection = function () {
403434
return this.hash.project.objects['PBXBuildFile'];
@@ -471,18 +502,16 @@ pbxProject.prototype.buildPhase = function (group,target) {
471502
pbxProject.prototype.buildPhaseObject = function (name, group,target) {
472503
var section = this.hash.project.objects[name],
473504
obj, sectionKey, key;
474-
475505
var buildPhase = this.buildPhase(group,target);
476506

477507
for (key in section) {
478-
508+
479509
// only look for comments
480510
if (!COMMENT_KEY.test(key)) continue;
481511

482512
// select the proper buildPhase
483513
if (buildPhase && buildPhase!=key)
484514
continue;
485-
486515
if (section[key] == group) {
487516
sectionKey = key.split(COMMENT_KEY)[0];
488517
return section[sectionKey];

test/addBuildPhase.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var fullProject = require('./fixtures/full-project')
2+
fullProjectStr = JSON.stringify(fullProject),
3+
pbx = require('../lib/pbxProject'),
4+
proj = new pbx('.');
5+
6+
function cleanHash() {
7+
return JSON.parse(fullProjectStr);
8+
}
9+
10+
exports.setUp = function (callback) {
11+
proj.hash = cleanHash();
12+
callback();
13+
}
14+
15+
exports.addBuildPhase = {
16+
'should return a pbxBuildPhase': function (test) {
17+
var buildPhase = proj.addBuildPhase(['file.m'], 'PBXSourcesBuildPhase', 'My build phase');
18+
19+
test.ok(typeof buildPhase === 'object');
20+
test.done()
21+
},
22+
'should set a uuid on the pbxBuildPhase': function (test) {
23+
var buildPhase = proj.addBuildPhase(['file.m'], 'PBXSourcesBuildPhase', 'My build phase');
24+
25+
test.ok(buildPhase.uuid);
26+
test.done()
27+
},
28+
'should add all files to build phase': function (test) {
29+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase');
30+
for (var index = 0; index < buildPhase.files.length; index++) {
31+
var file = buildPhase.files[index];
32+
test.ok(file.value);
33+
}
34+
35+
test.done()
36+
},
37+
'should add the PBXBuildPhase object correctly': function (test) {
38+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
39+
buildPhaseInPbx = proj.buildPhaseObject('PBXResourcesBuildPhase', 'My build phase');
40+
41+
test.equal(buildPhaseInPbx, buildPhase);
42+
test.equal(buildPhaseInPbx.isa, 'PBXResourcesBuildPhase');
43+
test.equal(buildPhaseInPbx.buildActionMask, 2147483647);
44+
test.equal(buildPhaseInPbx.runOnlyForDeploymentPostprocessing, 0);
45+
test.done();
46+
},
47+
'should add each of the files to PBXBuildFile section': function (test) {
48+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
49+
buildFileSection = proj.pbxBuildFileSection();
50+
51+
for (var index = 0; index < buildPhase.files.length; index++) {
52+
var file = buildPhase.files[index];
53+
test.ok(buildFileSection[file.value]);
54+
}
55+
56+
test.done();
57+
},
58+
'should add each of the files to PBXFileReference section': function (test) {
59+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
60+
fileRefSection = proj.pbxFileReferenceSection(),
61+
buildFileSection = proj.pbxBuildFileSection(),
62+
fileRefs = [];
63+
64+
for (var index = 0; index < buildPhase.files.length; index++) {
65+
var file = buildPhase.files[index];
66+
fileRefs.push(buildFileSection[file.value].fileRef);
67+
}
68+
69+
for (var index = 0; index < fileRefs.length; index++) {
70+
var fileRef = fileRefs[index];
71+
test.ok(fileRefSection[fileRef]);
72+
}
73+
74+
test.done();
75+
}
76+
}

0 commit comments

Comments
 (0)