Skip to content

Commit 57768ea

Browse files
author
Anis Kadri
committed
Merge branch 'add-get-target-by-name' of https://github.com/Mitko-Kerezov/node-xcode into Mitko-Kerezov-add-get-target-by-name
2 parents be19b0d + 6a3108a commit 57768ea

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
@@ -558,16 +558,24 @@ pbxProject.prototype.pbxXCConfigurationList = function () {
558558
}
559559

560560
pbxProject.prototype.pbxGroupByName = function (name) {
561-
var groups = this.hash.project.objects['PBXGroup'],
562-
key, groupKey;
561+
return this.pbxItemByComment(name, 'PBXGroup');
562+
}
563+
564+
pbxProject.prototype.pbxTargetByName = function (name) {
565+
return this.pbxItemByComment(name, 'PBXNativeTarget');
566+
}
567+
568+
pbxProject.prototype.pbxItemByComment = function (name, pbxSectionName) {
569+
var section = this.hash.project.objects[pbxSectionName],
570+
key, itemKey;
563571

564-
for (key in groups) {
572+
for (key in section) {
565573
// only look for comments
566574
if (!COMMENT_KEY.test(key)) continue;
567575

568-
if (groups[key] == name) {
569-
groupKey = key.split(COMMENT_KEY)[0];
570-
return groups[groupKey];
576+
if (section[key] == name) {
577+
itemKey = key.split(COMMENT_KEY)[0];
578+
return section[itemKey];
571579
}
572580
}
573581

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)