Skip to content

Commit 0c3bf19

Browse files
author
Dimitar Kerezov
committed
Introduce a way to add PBXGroups
For given file paths add them to Build and File Reference sections if they're not already added and create a PBXGroup with given name, path and optional sourceTree. sourceTree defaults to <group> if no other specified
1 parent 3085e40 commit 0c3bf19

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

lib/pbxProject.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,57 @@ pbxProject.prototype.removeFromPbxBuildFileSection = function (file) {
274274
delete this.pbxBuildFileSection()[commentKey];
275275
}
276276

277+
pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceTree) {
278+
var groups = this.hash.project.objects['PBXGroup'],
279+
pbxGroupUuid = this.generateUuid(),
280+
commentKey = f("%s_comment", pbxGroupUuid),
281+
pbxGroup = {
282+
isa: 'PBXGroup',
283+
children: [],
284+
name: name,
285+
path: path,
286+
sourceTree: sourceTree ? sourceTree : '"<group>"'
287+
},
288+
fileReferenceSection = this.pbxFileReferenceSection(),
289+
filePathToReference = {};
290+
291+
for (var key in fileReferenceSection) {
292+
// only look for comments
293+
if (!COMMENT_KEY.test(key)) continue;
294+
295+
var fileReferenceKey = key.split(COMMENT_KEY)[0],
296+
fileReference = fileReferenceSection[fileReferenceKey];
297+
298+
filePathToReference[fileReference.path] = {fileRef: fileReferenceKey, basename: fileReferenceSection[key]};
299+
}
300+
301+
for (var index = 0; index < filePathsArray.length; index++) {
302+
var filePath = filePathsArray[index],
303+
filePathQuoted = "\"" + filePath + "\"";
304+
if (filePathToReference[filePath]) {
305+
pbxGroup.children.push(pbxGroupChild(filePathToReference[filePath]));
306+
continue;
307+
} else if (filePathToReference[filePathQuoted]) {
308+
pbxGroup.children.push(pbxGroupChild(filePathToReference[filePathQuoted]));
309+
continue;
310+
}
311+
312+
var file = new pbxFile(filePath);
313+
file.uuid = this.generateUuid();
314+
file.fileRef = this.generateUuid();
315+
this.addToPbxFileReferenceSection(file); // PBXFileReference
316+
this.addToPbxBuildFileSection(file); // PBXBuildFile
317+
pbxGroup.children.push(pbxGroupChild(file));
318+
}
319+
320+
if (groups) {
321+
groups[pbxGroupUuid] = pbxGroup;
322+
groups[commentKey] = name;
323+
}
324+
325+
return {uuid: pbxGroupUuid, pbxGroup: pbxGroup};
326+
}
327+
277328
pbxProject.prototype.addToPbxFileReferenceSection = function (file) {
278329
var commentKey = f("%s_comment", file.fileRef);
279330

test/addPbxGroup.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
var fullProject = require('./fixtures/full-project')
2+
fullProjectStr = JSON.stringify(fullProject),
3+
pbx = require('../lib/pbxProject'),
4+
proj = new pbx('.');
5+
6+
function cleanHash() {
7+
return JSON.parse(fullProjectStr);
8+
}
9+
10+
exports.setUp = function (callback) {
11+
proj.hash = cleanHash();
12+
callback();
13+
}
14+
15+
exports.addPbxGroup = {
16+
'should return a pbxGroup': function (test) {
17+
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', 'Application', '"<group>"');
18+
19+
test.ok(typeof pbxGroup === 'object');
20+
test.done()
21+
},
22+
'should set a uuid on the pbxGroup': function (test) {
23+
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', 'Application', '"<group>"');
24+
25+
test.ok(pbxGroup.uuid);
26+
test.done()
27+
},
28+
'should add all files to pbxGroup': function (test) {
29+
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', 'Application', '"<group>"');
30+
for (var index = 0; index < pbxGroup.pbxGroup.children.length; index++) {
31+
var file = pbxGroup.pbxGroup.children[index];
32+
test.ok(file.value);
33+
}
34+
35+
test.done()
36+
},
37+
'should add the PBXGroup object correctly': function (test) {
38+
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application', '"<group>"');
39+
pbxGroupInPbx = proj.pbxGroupByName('MyGroup');
40+
41+
test.equal(pbxGroupInPbx.children, pbxGroup.pbxGroup.children);
42+
test.equal(pbxGroupInPbx.isa, 'PBXGroup');
43+
test.equal(pbxGroupInPbx.path, 'Application');
44+
test.equal(pbxGroupInPbx.sourceTree, '"<group>"');
45+
test.done();
46+
},
47+
'should add <group> sourceTree if no other specified': function (test) {
48+
var pbxGroup = proj.addPbxGroup(['file.m'], 'MyGroup', 'Application');
49+
pbxGroupInPbx = proj.pbxGroupByName('MyGroup');
50+
51+
test.equal(pbxGroupInPbx.sourceTree, '"<group>"');
52+
test.done();
53+
},
54+
'should add each of the files to PBXBuildFile section': function (test) {
55+
var buildFileSection = proj.pbxBuildFileSection();
56+
for (var key in buildFileSection) {
57+
test.notEqual(buildFileSection[key].fileRef_comment, 'file.m');
58+
test.notEqual(buildFileSection[key].fileRef_comment, 'assets.bundle');
59+
}
60+
61+
var initialBuildFileSectionItemsCount = Object.keys(buildFileSection),
62+
pbxGroup = proj.addPbxGroup(['file.m', 'assets.bundle'], 'MyGroup', 'Application', '"<group>"'),
63+
afterAdditionBuildFileSectionItemsCount = Object.keys(buildFileSection);
64+
65+
// for each file added in the build file section two keyes are added - one for the object and one for the comment
66+
test.equal(initialBuildFileSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 4);
67+
test.done();
68+
},
69+
'should not add any of the files to PBXBuildFile section if already added': function (test) {
70+
var buildFileSection = proj.pbxBuildFileSection(),
71+
initialBuildFileSectionItemsCount = Object.keys(buildFileSection),
72+
pbxGroup = proj.addPbxGroup(['AppDelegate.m', 'AppDelegate.h'], 'MyGroup', 'Application', '"<group>"'),
73+
afterAdditionBuildFileSectionItemsCount = Object.keys(buildFileSection);
74+
75+
test.deepEqual(initialBuildFileSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
76+
test.done();
77+
},
78+
'should not add any of the files to PBXBuildFile section when they contain special symbols and are already added': function (test) {
79+
var buildFileSection = proj.pbxBuildFileSection(),
80+
initialBuildFileSectionItemsCount = Object.keys(buildFileSection),
81+
pbxGroup = proj.addPbxGroup(['KitchenSinktablet.app'], 'MyGroup', 'Application', '"<group>"'),
82+
afterAdditionBuildFileSectionItemsCount = Object.keys(buildFileSection);
83+
84+
test.deepEqual(initialBuildFileSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
85+
test.done();
86+
},
87+
'should add all files which are not added and not add files already added to PBXBuildFile section': function (test) {
88+
var buildFileSection = proj.pbxBuildFileSection();
89+
for (var key in buildFileSection) {
90+
test.notEqual(buildFileSection[key].fileRef_comment, 'file.m');
91+
test.notEqual(buildFileSection[key].fileRef_comment, 'assets.bundle');
92+
}
93+
94+
var initialBuildFileSectionItemsCount = Object.keys(buildFileSection),
95+
pbxGroup = proj.addPbxGroup(['AppDelegate.m', 'AppDelegate.h', 'file.m', 'assets.bundle'], 'MyGroup', 'Application', '"<group>"'),
96+
afterAdditionBuildFileSectionItemsCount = Object.keys(buildFileSection);
97+
98+
// for each file added in the build file section two keyes are added - one for the object and one for the comment
99+
test.equal(initialBuildFileSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 4);
100+
test.done();
101+
},
102+
'should add each of the files to PBXFileReference section': function (test) {
103+
var fileReference = proj.pbxFileReferenceSection();
104+
for (var key in fileReference) {
105+
test.notEqual(fileReference[key].fileRef_comment, 'file.m');
106+
test.notEqual(fileReference[key].fileRef_comment, 'assets.bundle');
107+
}
108+
var pbxGroup = proj.addPbxGroup(['file.m', 'assets.bundle'], 'MyGroup', 'Application', '"<group>"');
109+
for (var index = 0; index < pbxGroup.pbxGroup.children.length; index++) {
110+
var file = pbxGroup.pbxGroup.children[index];
111+
test.ok(fileReference[file.value]);
112+
}
113+
114+
test.done();
115+
},
116+
'should not add any of the files to PBXFileReference section if already added': function (test) {
117+
var fileReference = proj.pbxFileReferenceSection (),
118+
initialBuildFileSectionItemsCount = Object.keys(fileReference),
119+
pbxGroup = proj.addPbxGroup(['AppDelegate.m', 'AppDelegate.h'], 'MyGroup', 'Application', '"<group>"'),
120+
afterAdditionBuildFileSectionItemsCount = Object.keys(fileReference);
121+
122+
test.deepEqual(initialBuildFileSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
123+
test.done();
124+
},
125+
'should not add any of the files to PBXFileReference section when they contain special symbols and are already added': function (test) {
126+
var fileReference = proj.pbxFileReferenceSection (),
127+
initialBuildFileSectionItemsCount = Object.keys(fileReference),
128+
pbxGroup = proj.addPbxGroup(['KitchenSinktablet.app'], 'MyGroup', 'Application', '"<group>"'),
129+
afterAdditionBuildFileSectionItemsCount = Object.keys(fileReference);
130+
131+
test.deepEqual(initialBuildFileSectionItemsCount, afterAdditionBuildFileSectionItemsCount);
132+
test.done();
133+
},
134+
'should add all files which are not added and not add files already added to PBXFileReference section': function (test) {
135+
var fileReference = proj.pbxFileReferenceSection ();
136+
for (var key in fileReference) {
137+
test.notEqual(fileReference[key].fileRef_comment, 'file.m');
138+
test.notEqual(fileReference[key].fileRef_comment, 'assets.bundle');
139+
}
140+
141+
var initialBuildFileSectionItemsCount = Object.keys(fileReference),
142+
pbxGroup = proj.addPbxGroup(['AppDelegate.m', 'AppDelegate.h', 'file.m', 'assets.bundle'], 'MyGroup', 'Application', '"<group>"'),
143+
afterAdditionBuildFileSectionItemsCount = Object.keys(fileReference);
144+
145+
// for each file added in the file reference section two keyes are added - one for the object and one for the comment
146+
test.equal(initialBuildFileSectionItemsCount.length, afterAdditionBuildFileSectionItemsCount.length - 4);
147+
test.done();
148+
}
149+
}

0 commit comments

Comments
 (0)