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
diff --git a/lib/pbxProject.js b/lib/pbxProject.js
index 0449ebb..4d9ecb4 100644
--- a/lib/pbxProject.js
+++ b/lib/pbxProject.js
@@ -325,6 +325,21 @@
return {uuid: pbxGroupUuid, pbxGroup: pbxGroup};
}
+pbxProject.prototype.removePbxGroup = function (groupName) {
+ var section = this.hash.project.objects['PBXGroup'],
+ key, itemKey;
+
+ for (key in section) {
+ // only look for comments
+ if (!COMMENT_KEY.test(key)) continue;
+
+ if (section[key] == groupName) {
+ itemKey = key.split(COMMENT_KEY)[0];
+ delete section[itemKey];
+ }
+ }
+}
+
pbxProject.prototype.addToPbxFileReferenceSection = function (file) {
var commentKey = f("%s_comment", file.fileRef);
@@ -333,22 +348,26 @@
}
pbxProject.prototype.removeFromPbxFileReferenceSection = function (file) {
-
- var i;
var refObj = pbxFileReferenceObj(file);
- for(i in this.pbxFileReferenceSection()) {
- if(this.pbxFileReferenceSection()[i].name == refObj.name ||
- ('"' + this.pbxFileReferenceSection()[i].name + '"') == refObj.name ||
- this.pbxFileReferenceSection()[i].path == refObj.path ||
- ('"' + this.pbxFileReferenceSection()[i].path + '"') == refObj.path) {
+ var pbxFileReferenceSection = this.pbxFileReferenceSection();
+ for (var i in pbxFileReferenceSection) {
+ var pbxFileReferenceSectionName = quote(pbxFileReferenceSection[i].name);
+ var pbxFileReferenceSectionPath = quote(pbxFileReferenceSection[i].path);
+ var refObjName = quote(refObj.name);
+ var refObjPath = quote(refObj.path);
+ if (pbxFileReferenceSectionName == refObjName ||
+ pbxFileReferenceSectionName == refObjPath ||
+ pbxFileReferenceSectionPath == refObjPath ||
+ pbxFileReferenceSectionPath == refObjName) {
file.fileRef = file.uuid = i;
- delete this.pbxFileReferenceSection()[i];
+ delete pbxFileReferenceSection[i];
break;
}
}
- var commentKey = f("%s_comment", file.fileRef);
- if(this.pbxFileReferenceSection()[commentKey] != undefined) {
- delete this.pbxFileReferenceSection()[commentKey];
+
+ if (typeof file.fileRef !== 'undefined') {
+ var commentKey = f("%s_comment", file.fileRef);
+ delete pbxFileReferenceSection[commentKey];
}
return file;
@@ -376,11 +395,13 @@
}
pbxProject.prototype.removeFromResourcesPbxGroup = function (file) {
- var pluginsGroupChildren = this.pbxGroupByName('Resources').children, i;
- for(i in pluginsGroupChildren) {
- if(pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
- pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
- pluginsGroupChildren.splice(i, 1);
+ var groupChild = pbxGroupChild(file);
+ var resourcesGroupChildren = this.pbxGroupByName('Resources').children;
+ for (var i in resourcesGroupChildren) {
+ var resourcesGroupChild = resourcesGroupChildren[i];
+ if ((typeof groupChild.value === 'undefined' || groupChild.value == resourcesGroupChild.value) &&
+ groupChild.comment == resourcesGroupChild.comment) {
+ resourcesGroupChildren.splice(i, 1);
break;
}
}
@@ -1086,4 +1107,8 @@
if (str) return str.replace(/^"(.*)"$/, "$1");
}
+function quote(str) {
+ if (str) return '"' + unquote(str) + '"';
+}
+
module.exports = pbxProject;
diff --git a/test/addPbxGroup.js b/test/addRemovePbxGroup.js
similarity index 94%
rename from test/addPbxGroup.js
rename to test/addRemovePbxGroup.js
index 433d30e..7c6e978 100644
--- a/test/addPbxGroup.js
+++ b/test/addRemovePbxGroup.js
@@ -12,7 +12,7 @@
callback();
}
-exports.addPbxGroup = {
+exports.addRemovePbxGroup = {
'should return a pbxGroup': function (test) {
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', 'Application', '"<group>"');
@@ -145,5 +145,16 @@
// for each file added in the file reference section two keyes are added - one for the object and one for the comment
test.equal(initialBuildFileSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 4);
test.done();
+ },
+ 'should remove a pbxGroup': function (test) {
+ var groupName = 'MyGroup';
+ proj.addPbxGroup(['file.m'], groupName, 'Application', 'Application', '"<group>"');
+ proj.removePbxGroup(groupName);
+
+ var pbxGroupInPbx = proj.pbxGroupByName(groupName);
+ console.log(pbxGroupInPbx);
+
+ test.ok(!pbxGroupInPbx);
+ test.done()
}
}