Skip to content

Commit 4888306

Browse files
Dimitar Kerezove2l3n
Dimitar Kerezov
authored andcommitted
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 8bb44fe commit 4888306

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

lib/pbxProject.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -601,16 +601,24 @@ pbxProject.prototype.pbxXCConfigurationList = function () {
601601
}
602602

603603
pbxProject.prototype.pbxGroupByName = function (name) {
604-
var groups = this.hash.project.objects['PBXGroup'],
605-
key, groupKey;
604+
return this.pbxItemByComment(name, 'PBXGroup');
605+
}
606+
607+
pbxProject.prototype.pbxTargetByName = function (name) {
608+
return this.pbxItemByComment(name, 'PBXNativeTarget');
609+
}
610+
611+
pbxProject.prototype.pbxItemByComment = function (name, pbxSectionName) {
612+
var section = this.hash.project.objects[pbxSectionName],
613+
key, itemKey;
606614

607-
for (key in groups) {
615+
for (key in section) {
608616
// only look for comments
609617
if (!COMMENT_KEY.test(key)) continue;
610618

611-
if (groups[key] == name) {
612-
groupKey = key.split(COMMENT_KEY)[0];
613-
return groups[groupKey];
619+
if (section[key] == name) {
620+
itemKey = key.split(COMMENT_KEY)[0];
621+
return section[itemKey];
614622
}
615623
}
616624

test/pbxItemByComment.js

+50
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

+29
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)