Skip to content

Commit 0d2873d

Browse files
authored
fix: allow adding resource, source... to a target when it was already added to another target (#21)
1 parent f0bf52d commit 0d2873d

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

lib/pbxProject.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ pbxProject.prototype.addPluginFile = function(path, opt) {
119119
file.plugin = true; // durr
120120
correctForPluginsPath(file, this);
121121

122-
// null is better for early errors
123-
if (this.hasFile(file.path)) return null;
122+
const existingFile = this.hasFile(file.path);
123+
if (existingFile) return {...file, fileRef: existingFile.fileRef};
124124

125125
file.fileRef = this.generateUuid();
126126

@@ -256,19 +256,25 @@ pbxProject.prototype.addResourceFile = function(path, opt, group) {
256256
opt = opt || {};
257257

258258
var file;
259+
var fileIsAlreadyAdded = false;
259260

260261
if (opt.plugin) {
261262
file = this.addPluginFile(path, opt);
262263
if (!file) return false;
263264
} else {
264265
file = new pbxFile(path, opt);
265-
if (this.hasFile(file.path)) return false;
266+
const existingFile = this.hasFile(file.path);
267+
fileIsAlreadyAdded = !!existingFile;
268+
if (existingFile) {
269+
// use existing fileRef
270+
file.fileRef = existingFile.fileRef;
271+
}
266272
}
267273

268274
file.uuid = this.generateUuid();
269275
file.target = opt ? opt.target : undefined;
270276

271-
if (!opt.plugin) {
277+
if (!opt.plugin && !fileIsAlreadyAdded) {
272278
correctForResourcesPath(file, this);
273279
file.fileRef = this.generateUuid();
274280
}
@@ -278,7 +284,7 @@ pbxProject.prototype.addResourceFile = function(path, opt, group) {
278284
this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
279285
}
280286

281-
if (!opt.plugin) {
287+
if (!opt.plugin && !fileIsAlreadyAdded) {
282288
this.addToPbxFileReferenceSection(file); // PBXFileReference
283289
if (group) {
284290
if (this.getPBXGroupByKey(group)) {
@@ -345,8 +351,7 @@ pbxProject.prototype.addFramework = function(fpath, opt) {
345351

346352
var fileReference = this.hasFile(file.path);
347353
if (fileReference) {
348-
var key = this.getFileKey(file.path);
349-
file.fileRef = key;
354+
file.fileRef = fileReference.fileRef;
350355
} else {
351356
this.addToPbxFileReferenceSection(file); // PBXFileReference
352357
this.addToFrameworksPbxGroup(file); // PBXGroup
@@ -424,13 +429,14 @@ pbxProject.prototype.addCopyfile = function(fpath, opt) {
424429
// catch duplicates
425430
if (this.hasFile(file.path)) {
426431
file = this.hasFile(file.path);
432+
} else {
433+
file.fileRef = this.generateUuid();
434+
this.addToPbxFileReferenceSection(file); // PBXFileReference
427435
}
428-
429-
file.fileRef = file.uuid = this.generateUuid();
436+
file.uuid = this.generateUuid();
430437
file.target = opt ? opt.target : undefined;
431438

432439
this.addToPbxBuildFileSection(file); // PBXBuildFile
433-
this.addToPbxFileReferenceSection(file); // PBXFileReference
434440
this.addToPbxCopyfilesBuildPhase(file); // PBXCopyFilesBuildPhase
435441

436442
return file;
@@ -470,19 +476,24 @@ pbxProject.prototype.addStaticLibrary = function(path, opt) {
470476
opt = opt || {};
471477

472478
var file;
479+
var fileIsAlreadyAdded = false;
473480

474481
if (opt.plugin) {
475482
file = this.addPluginFile(path, opt);
476483
if (!file) return false;
477484
} else {
478485
file = new pbxFile(path, opt);
479-
if (this.hasFile(file.path)) return false;
486+
const existingFile = this.hasFile(file.path);
487+
fileIsAlreadyAdded = !!existingFile;
488+
if (existingFile) {
489+
file.fileRef = existingFile.fileRef;
490+
}
480491
}
481492

482493
file.uuid = this.generateUuid();
483494
file.target = opt ? opt.target : undefined;
484495

485-
if (!opt.plugin) {
496+
if (!opt.plugin && !fileIsAlreadyAdded) {
486497
file.fileRef = this.generateUuid();
487498
this.addToPbxFileReferenceSection(file); // PBXFileReference
488499
}
@@ -859,20 +870,20 @@ pbxProject.prototype.removeFromPluginsPbxGroup = function(file) {
859870
}
860871
}
861872

862-
pbxProject.prototype.addToResourcesPbxGroup = function(file) {
863-
var pluginsGroup = this.pbxGroupByName('Resources');
873+
pbxProject.prototype.addToResourcesPbxGroup = function(file, groupName = 'Resources') {
874+
var pluginsGroup = this.pbxGroupByName(groupName);
864875
if (!pluginsGroup) {
865-
this.addPbxGroup([file.path], 'Resources');
876+
this.addPbxGroup([file.path], groupName);
866877
} else {
867878
pluginsGroup.children.push(pbxGroupChild(file));
868879
}
869880
}
870881

871-
pbxProject.prototype.removeFromResourcesPbxGroup = function(file) {
872-
if (!this.pbxGroupByName('Resources')) {
882+
pbxProject.prototype.removeFromResourcesPbxGroup = function(file, groupName = 'Resources') {
883+
if (!this.pbxGroupByName(groupName)) {
873884
return null;
874885
}
875-
var pluginsGroupChildren = this.pbxGroupByName('Resources').children, i;
886+
var pluginsGroupChildren = this.pbxGroupByName(groupName).children, i;
876887
for (i in pluginsGroupChildren) {
877888
if (pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
878889
pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
@@ -1680,7 +1691,7 @@ pbxProject.prototype.hasFile = function(filePath) {
16801691
for (id in files) {
16811692
file = files[id];
16821693
if (file.path == filePath || file.path == ('"' + filePath + '"')) {
1683-
return file;
1694+
return {...file, fileRef: id};
16841695
}
16851696
}
16861697

@@ -2058,7 +2069,7 @@ function pbxFileReferenceObj(file) {
20582069
fileObject.name = "\"" + fileObject.name + "\"";
20592070
}
20602071

2061-
if(!file.path.match(NO_SPECIAL_SYMBOLS)) {
2072+
if(file.path && !file.path.match(NO_SPECIAL_SYMBOLS)) {
20622073
fileObject.path = "\"" + fileObject.path + "\"";
20632074
}
20642075

@@ -2525,10 +2536,11 @@ pbxProject.prototype.getPBXObject = function(name) {
25252536
pbxProject.prototype.addFile = function (path, group, opt) {
25262537
var file = new pbxFile(path, opt);
25272538

2528-
// null is better for early errors
2529-
if (this.hasFile(file.path)) return null;
2539+
const existingFile = this.hasFile(file.path);
2540+
if (existingFile) return {...file, fileRef: existingFile.fileRef};
25302541

25312542
file.fileRef = this.generateUuid();
2543+
file.target = opt ? opt.target : undefined;
25322544

25332545
this.addToPbxFileReferenceSection(file); // PBXFileReference
25342546

0 commit comments

Comments
 (0)