Skip to content

Commit 8bb44fe

Browse files
Dimitar Kerezove2l3n
Dimitar Kerezov
authored andcommitted
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 0932434 commit 8bb44fe

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

lib/pbxProject.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -521,19 +521,46 @@ pbxProject.prototype.addXCConfigurationList = function (configurationObjectsArra
521521

522522
pbxProject.prototype.addBuildPhase = function (filePathsArray, isa, comment) {
523523
var section = this.hash.project.objects[isa],
524+
fileReferenceSection = this.pbxFileReferenceSection(),
525+
buildFileSection = this.pbxBuildFileSection(),
524526
buildPhaseUuid = this.generateUuid(),
525527
commentKey = f("%s_comment", buildPhaseUuid),
526528
buildPhase = {
527529
isa: isa,
528530
buildActionMask: 2147483647,
529531
files: [],
530532
runOnlyForDeploymentPostprocessing: 0
531-
};
533+
},
534+
filePathToBuildFile = {};
535+
536+
for (var key in buildFileSection) {
537+
// only look for comments
538+
if (!COMMENT_KEY.test(key)) continue;
539+
540+
var buildFileKey = key.split(COMMENT_KEY)[0],
541+
buildFile = buildFileSection[buildFileKey];
542+
fileReference = fileReferenceSection[buildFile.fileRef];
543+
544+
if (!fileReference) continue;
545+
546+
var pbxFileObj = new pbxFile(fileReference.path);
547+
548+
filePathToBuildFile[fileReference.path] = {uuid: buildFileKey, basename: pbxFileObj.basename, group: pbxFileObj.group};
549+
}
532550

533551
for (var index = 0; index < filePathsArray.length; index++) {
534552
var filePath = filePathsArray[index],
553+
filePathQuoted = "\"" + filePath + "\"",
535554
file = new pbxFile(filePath);
536555

556+
if (filePathToBuildFile[filePath]) {
557+
buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePath]));
558+
continue;
559+
} else if (filePathToBuildFile[filePathQuoted]) {
560+
buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePathQuoted]));
561+
continue;
562+
}
563+
537564
file.uuid = this.generateUuid();
538565
file.fileRef = this.generateUuid();
539566
this.addToPbxFileReferenceSection(file); // PBXFileReference

test/addBuildPhase.js

+37-5
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)