Skip to content

Commit 34f4d68

Browse files
author
Anis Kadri
committed
2 parents 15ec067 + 6af1ba8 commit 34f4d68

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

lib/pbxProject.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,19 +400,46 @@ pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function (file) {
400400

401401
pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) {
402402
var section = this.hash.project.objects[isa],
403+
fileReferenceSection = this.pbxFileReferenceSection(),
404+
buildFileSection = this.pbxBuildFileSection(),
403405
buildPhaseUuid = this.generateUuid(),
404406
commentKey = f("%s_comment", buildPhaseUuid),
405407
buildPhase = {
406408
isa: isa,
407409
buildActionMask: 2147483647,
408410
files: [],
409411
runOnlyForDeploymentPostprocessing: 0
410-
};
412+
},
413+
filePathToBuildFile = {};
414+
415+
for (var key in buildFileSection) {
416+
// only look for comments
417+
if (!COMMENT_KEY.test(key)) continue;
418+
419+
var buildFileKey = key.split(COMMENT_KEY)[0],
420+
buildFile = buildFileSection[buildFileKey];
421+
fileReference = fileReferenceSection[buildFile.fileRef];
422+
423+
if (!fileReference) continue;
424+
425+
var pbxFileObj = new pbxFile(fileReference.path);
426+
427+
filePathToBuildFile[fileReference.path] = {uuid: buildFileKey, basename: pbxFileObj.basename, group: pbxFileObj.group};
428+
}
411429

412430
for (var index = 0; index < filePathsArray.length; index++) {
413431
var filePath = filePathsArray[index],
432+
filePathQuoted = "\"" + filePath + "\"",
414433
file = new pbxFile(filePath);
415434

435+
if (filePathToBuildFile[filePath]) {
436+
buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePath]));
437+
continue;
438+
} else if (filePathToBuildFile[filePathQuoted]) {
439+
buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePathQuoted]));
440+
continue;
441+
}
442+
416443
file.uuid = this.generateUuid();
417444
file.fileRef = this.generateUuid();
418445
this.addToPbxFileReferenceSection(file); // PBXFileReference
@@ -425,8 +452,7 @@ pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) {
425452
section[commentKey] = comment;
426453
}
427454

428-
buildPhase.uuid = buildPhaseUuid;
429-
return buildPhase;
455+
return {uuid: buildPhaseUuid, buildPhase: buildPhase};
430456
}
431457

432458
// helper access functions

test/addBuildPhase.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exports.addBuildPhase = {
2626
test.done()
2727
},
2828
'should add all files to build phase': function (test) {
29-
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase');
29+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase;
3030
for (var index = 0; index < buildPhase.files.length; index++) {
3131
var file = buildPhase.files[index];
3232
test.ok(file.value);
@@ -35,7 +35,7 @@ exports.addBuildPhase = {
3535
test.done()
3636
},
3737
'should add the PBXBuildPhase object correctly': function (test) {
38-
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
38+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
3939
buildPhaseInPbx = proj.buildPhaseObject('PBXResourcesBuildPhase', 'My build phase');
4040

4141
test.equal(buildPhaseInPbx, buildPhase);
@@ -45,7 +45,7 @@ exports.addBuildPhase = {
4545
test.done();
4646
},
4747
'should add each of the files to PBXBuildFile section': function (test) {
48-
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
48+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
4949
buildFileSection = proj.pbxBuildFileSection();
5050

5151
for (var index = 0; index < buildPhase.files.length; index++) {
@@ -56,21 +56,53 @@ exports.addBuildPhase = {
5656
test.done();
5757
},
5858
'should add each of the files to PBXFileReference section': function (test) {
59-
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase'),
59+
var buildPhase = proj.addBuildPhase(['file.m', 'assets.bundle'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
6060
fileRefSection = proj.pbxFileReferenceSection(),
6161
buildFileSection = proj.pbxBuildFileSection(),
6262
fileRefs = [];
6363

6464
for (var index = 0; index < buildPhase.files.length; index++) {
65-
var file = buildPhase.files[index];
66-
fileRefs.push(buildFileSection[file.value].fileRef);
65+
var file = buildPhase.files[index],
66+
fileRef = buildFileSection[file.value].fileRef;
67+
68+
test.ok(fileRefSection[fileRef]);
6769
}
70+
71+
test.done();
72+
},
73+
'should not add files to PBXFileReference section if already added': function (test) {
74+
var fileRefSection = proj.pbxFileReferenceSection(),
75+
initialFileReferenceSectionItemsCount = Object.keys(fileRefSection),
76+
buildPhase = proj.addBuildPhase(['AppDelegate.m', 'main.m'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
77+
afterAdditionBuildFileSectionItemsCount = Object.keys(fileRefSection);
78+
79+
test.deepEqual(initialFileReferenceSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
80+
test.done();
81+
},
82+
'should not add files to PBXBuildFile section if already added': function (test) {
83+
var buildFileSection = proj.pbxBuildFileSection(),
84+
initialBuildFileSectionItemsCount = Object.keys(buildFileSection),
85+
buildPhase = proj.addBuildPhase(['AppDelegate.m', 'main.m'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
86+
afterAdditionBuildFileSectionItemsCount = Object.keys(buildFileSection);
6887

69-
for (var index = 0; index < fileRefs.length; index++) {
70-
var fileRef = fileRefs[index];
88+
test.deepEqual(initialBuildFileSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
89+
test.done();
90+
},
91+
'should add only missing files to PBXFileReference section': function (test) {
92+
var fileRefSection = proj.pbxFileReferenceSection(),
93+
buildFileSection = proj.pbxBuildFileSection(),
94+
initialFileReferenceSectionItemsCount = Object.keys(fileRefSection),
95+
buildPhase = proj.addBuildPhase(['file.m', 'AppDelegate.m'], 'PBXResourcesBuildPhase', 'My build phase').buildPhase,
96+
afterAdditionBuildFileSectionItemsCount = Object.keys(fileRefSection);
97+
98+
for (var index = 0; index < buildPhase.files.length; index++) {
99+
var file = buildPhase.files[index],
100+
fileRef = buildFileSection[file.value].fileRef;
101+
71102
test.ok(fileRefSection[fileRef]);
72103
}
73-
104+
105+
test.deepEqual(initialFileReferenceSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 2);
74106
test.done();
75107
}
76108
}

0 commit comments

Comments
 (0)