Skip to content

Commit b0b5db8

Browse files
committed
Refactored removeFromPbxFileReferenceSection to recognize file references
that have base file name as path (also minor improvements for readability and quality) Added removePbxGroup Added tests Added quote helper function
1 parent b51590a commit b0b5db8

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

lib/pbxProject.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
325325
return {uuid: pbxGroupUuid, pbxGroup: pbxGroup};
326326
}
327327

328+
pbxProject.prototype.removePbxGroup = function (groupName) {
329+
var section = this.hash.project.objects['PBXGroup'],
330+
key, itemKey;
331+
332+
for (key in section) {
333+
// only look for comments
334+
if (!COMMENT_KEY.test(key)) continue;
335+
336+
if (section[key] == groupName) {
337+
itemKey = key.split(COMMENT_KEY)[0];
338+
delete section[itemKey];
339+
}
340+
}
341+
}
342+
328343
pbxProject.prototype.addToPbxFileReferenceSection = function (file) {
329344
var commentKey = f("%s_comment", file.fileRef);
330345

@@ -333,22 +348,26 @@ pbxProject.prototype.addToPbxFileReferenceSection = function (file) {
333348
}
334349

335350
pbxProject.prototype.removeFromPbxFileReferenceSection = function (file) {
336-
337-
var i;
338351
var refObj = pbxFileReferenceObj(file);
339-
for(i in this.pbxFileReferenceSection()) {
340-
if(this.pbxFileReferenceSection()[i].name == refObj.name ||
341-
('"' + this.pbxFileReferenceSection()[i].name + '"') == refObj.name ||
342-
this.pbxFileReferenceSection()[i].path == refObj.path ||
343-
('"' + this.pbxFileReferenceSection()[i].path + '"') == refObj.path) {
352+
var pbxFileReferenceSection = this.pbxFileReferenceSection();
353+
for (var i in pbxFileReferenceSection) {
354+
var pbxFileReferenceSectionName = quote(pbxFileReferenceSection[i].name);
355+
var pbxFileReferenceSectionPath = quote(pbxFileReferenceSection[i].path);
356+
var refObjName = quote(refObj.name);
357+
var refObjPath = quote(refObj.path);
358+
if (pbxFileReferenceSectionName == refObjName ||
359+
pbxFileReferenceSectionName == refObjPath ||
360+
pbxFileReferenceSectionPath == refObjPath ||
361+
pbxFileReferenceSectionPath == refObjName) {
344362
file.fileRef = file.uuid = i;
345-
delete this.pbxFileReferenceSection()[i];
363+
delete pbxFileReferenceSection[i];
346364
break;
347365
}
348366
}
349-
var commentKey = f("%s_comment", file.fileRef);
350-
if(this.pbxFileReferenceSection()[commentKey] != undefined) {
351-
delete this.pbxFileReferenceSection()[commentKey];
367+
368+
if (typeof file.fileRef !== 'undefined') {
369+
var commentKey = f("%s_comment", file.fileRef);
370+
delete pbxFileReferenceSection[commentKey];
352371
}
353372

354373
return file;
@@ -376,11 +395,13 @@ pbxProject.prototype.addToResourcesPbxGroup = function (file) {
376395
}
377396

378397
pbxProject.prototype.removeFromResourcesPbxGroup = function (file) {
379-
var pluginsGroupChildren = this.pbxGroupByName('Resources').children, i;
380-
for(i in pluginsGroupChildren) {
381-
if(pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
382-
pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
383-
pluginsGroupChildren.splice(i, 1);
398+
var groupChild = pbxGroupChild(file);
399+
var resourcesGroupChildren = this.pbxGroupByName('Resources').children;
400+
for (var i in resourcesGroupChildren) {
401+
var resourcesGroupChild = resourcesGroupChildren[i];
402+
if ((typeof groupChild.value === 'undefined' || groupChild.value == resourcesGroupChild.value) &&
403+
groupChild.comment == resourcesGroupChild.comment) {
404+
resourcesGroupChildren.splice(i, 1);
384405
break;
385406
}
386407
}
@@ -1086,4 +1107,8 @@ function unquote(str) {
10861107
if (str) return str.replace(/^"(.*)"$/, "$1");
10871108
}
10881109

1110+
function quote(str) {
1111+
if (str) return '"' + unquote(str) + '"';
1112+
}
1113+
10891114
module.exports = pbxProject;

test/addPbxGroup.js renamed to test/addRemovePbxGroup.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports.setUp = function (callback) {
1212
callback();
1313
}
1414

15-
exports.addPbxGroup = {
15+
exports.addRemovePbxGroup = {
1616
'should return a pbxGroup': function (test) {
1717
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', 'Application', '"<group>"');
1818

@@ -145,5 +145,16 @@ exports.addPbxGroup = {
145145
// for each file added in the file reference section two keyes are added - one for the object and one for the comment
146146
test.equal(initialBuildFileSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 4);
147147
test.done();
148+
},
149+
'should remove a pbxGroup': function (test) {
150+
var groupName = 'MyGroup';
151+
proj.addPbxGroup(['file.m'], groupName, 'Application', 'Application', '"<group>"');
152+
proj.removePbxGroup(groupName);
153+
154+
var pbxGroupInPbx = proj.pbxGroupByName(groupName);
155+
console.log(pbxGroupInPbx);
156+
157+
test.ok(!pbxGroupInPbx);
158+
test.done()
148159
}
149160
}

0 commit comments

Comments
 (0)