Skip to content

Commit 6af1ba8

Browse files
author
Dimitar Kerezov
committed
Fix AddBuildPhase able to add duplicate files
Add a check - if a file exists in the PBXBuildFile and the PBXFileReference sections do not add it
1 parent 4982ced commit 6af1ba8

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

lib/pbxProject.js

Lines changed: 28 additions & 1 deletion
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

test/addBuildPhase.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,47 @@ exports.addBuildPhase = {
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)