Skip to content

Commit 6a3108a

Browse files
author
Dimitar Kerezov
committed
Implement pbxTargetByName and pbxItemByComment
Includes a bit of refactoring and extracting common logic between pbxGroupByName and pbxTargetByName into the more powerful function pbxItemByComment.
1 parent 3085e40 commit 6a3108a

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

lib/pbxProject.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,24 @@ pbxProject.prototype.pbxXCConfigurationList = function () {
451451
}
452452

453453
pbxProject.prototype.pbxGroupByName = function (name) {
454-
var groups = this.hash.project.objects['PBXGroup'],
455-
key, groupKey;
454+
return this.pbxItemByComment(name, 'PBXGroup');
455+
}
456+
457+
pbxProject.prototype.pbxTargetByName = function (name) {
458+
return this.pbxItemByComment(name, 'PBXNativeTarget');
459+
}
456460

457-
for (key in groups) {
461+
pbxProject.prototype.pbxItemByComment = function (name, pbxSectionName) {
462+
var section = this.hash.project.objects[pbxSectionName],
463+
key, itemKey;
464+
465+
for (key in section) {
458466
// only look for comments
459467
if (!COMMENT_KEY.test(key)) continue;
460468

461-
if (groups[key] == name) {
462-
groupKey = key.split(COMMENT_KEY)[0];
463-
return groups[groupKey];
469+
if (section[key] == name) {
470+
itemKey = key.split(COMMENT_KEY)[0];
471+
return section[itemKey];
464472
}
465473
}
466474

test/pbxItemByComment.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.pbxItemByComment = {
16+
'should return PBXTargetDependency': function (test) {
17+
var pbxItem = proj.pbxItemByComment('PBXTargetDependency', 'PBXTargetDependency');
18+
19+
test.ok(pbxItem);
20+
test.equals(pbxItem.isa, 'PBXTargetDependency');
21+
test.done()
22+
},
23+
'should return PBXContainerItemProxy': function (test) {
24+
var pbxItem = proj.pbxItemByComment('libPhoneGap.a', 'PBXReferenceProxy');
25+
26+
test.ok(pbxItem);
27+
test.equals(pbxItem.isa, 'PBXReferenceProxy');
28+
test.done()
29+
},
30+
'should return PBXResourcesBuildPhase': function (test) {
31+
var pbxItem = proj.pbxItemByComment('Resources', 'PBXResourcesBuildPhase');
32+
33+
test.ok(pbxItem);
34+
test.equals(pbxItem.isa, 'PBXResourcesBuildPhase');
35+
test.done()
36+
},
37+
'should return PBXShellScriptBuildPhase': function (test) {
38+
var pbxItem = proj.pbxItemByComment('Touch www folder', 'PBXShellScriptBuildPhase');
39+
40+
test.ok(pbxItem);
41+
test.equals(pbxItem.isa, 'PBXShellScriptBuildPhase');
42+
test.done()
43+
},
44+
'should return null when PBXNativeTarget not found': function (test) {
45+
var pbxItem = proj.pbxItemByComment('Invalid', 'PBXTargetDependency');
46+
47+
test.equal(pbxItem, null);
48+
test.done()
49+
}
50+
}

test/pbxTargetByName.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.pbxTargetByName = {
16+
'should return PBXNativeTarget': function (test) {
17+
var pbxTarget = proj.pbxTargetByName('KitchenSinktablet');
18+
19+
test.ok(pbxTarget);
20+
test.equals(pbxTarget.isa, 'PBXNativeTarget');
21+
test.done()
22+
},
23+
'should return null when PBXNativeTarget not found': function (test) {
24+
var pbxTarget = proj.pbxTargetByName('Invalid');
25+
26+
test.equal(pbxTarget, null);
27+
test.done()
28+
}
29+
}

0 commit comments

Comments
 (0)