Skip to content

Commit 5d3ce93

Browse files
committed
[pbxProject] addHeaderFile
plus some refactoring
1 parent 920b7c6 commit 5d3ce93

File tree

2 files changed

+114
-24
lines changed

2 files changed

+114
-24
lines changed

lib/pbxProject.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,42 +68,30 @@ pbxProject.prototype.generateUuid = function () {
6868
}
6969

7070
pbxProject.prototype.addSourceFile = function (path, opt) {
71-
var file = new pbxFile(path, opt),
72-
commentKey, pluginsGroup, sources;
71+
var file = new pbxFile(path, opt);
7372

7473
file.uuid = this.generateUuid();
7574
file.fileRef = this.generateUuid();
7675

77-
// PBXBuildFile
78-
commentKey = f("%s_comment", file.uuid);
76+
this.addToPbxBuildFileSection(file); // PBXBuildFile
77+
this.addToPbxFileReferenceSection(file); // PBXFileReference
78+
this.addToPluginsPbxGroup(file); // PBXGroup
79+
this.addToPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
7980

80-
this.pbxBuildFileSection()[file.uuid] = pbxBuildFileObj(file);
81-
this.pbxBuildFileSection()[commentKey] = pbxBuildFileComment(file);
82-
83-
// PBXFileReference
84-
commentKey = f("%s_comment", file.fileRef);
81+
return file;
82+
}
8583

86-
this.pbxFileReferenceSection()[file.fileRef] = pbxFileReferenceObj(file);
87-
this.pbxFileReferenceSection()[commentKey] = pbxFileReferenceComment(file);
84+
pbxProject.prototype.addHeaderFile = function (path, opt) {
85+
var file = new pbxFile(path, opt);
8886

89-
// PBXGroup
90-
pluginsGroup = this.pbxGroupByName('Plugins');
91-
pluginsGroup.children.push(pbxGroupChild(file));
87+
file.fileRef = this.generateUuid();
9288

93-
// PBXSourcesBuildPhase
94-
sources = this.pbxSourcesBuildPhaseObj();
95-
sources.files.push(pbxSourceFileObj(file));
89+
this.addToPbxFileReferenceSection(file); // PBXFileReference
90+
this.addToPluginsPbxGroup(file); // PBXGroup
9691

9792
return file;
9893
}
9994

100-
pbxProject.prototype.addHeaderFile = function (path, opt) {
101-
/*
102-
* PBXFileReference
103-
* PBXGroup (Plugins)
104-
*/
105-
}
106-
10795
pbxProject.prototype.addResourceFile = function (path, opt) {
10896
/*
10997
* PBXBuildFile
@@ -114,6 +102,30 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
114102
}
115103

116104
// helper access functions
105+
pbxProject.prototype.addToPbxBuildFileSection = function (file) {
106+
var commentKey = f("%s_comment", file.uuid);
107+
108+
this.pbxBuildFileSection()[file.uuid] = pbxBuildFileObj(file);
109+
this.pbxBuildFileSection()[commentKey] = pbxBuildFileComment(file);
110+
}
111+
112+
pbxProject.prototype.addToPbxFileReferenceSection = function (file) {
113+
var commentKey = f("%s_comment", file.fileRef);
114+
115+
this.pbxFileReferenceSection()[file.fileRef] = pbxFileReferenceObj(file);
116+
this.pbxFileReferenceSection()[commentKey] = pbxFileReferenceComment(file);
117+
}
118+
119+
pbxProject.prototype.addToPluginsPbxGroup = function (file) {
120+
var pluginsGroup = this.pbxGroupByName('Plugins');
121+
pluginsGroup.children.push(pbxGroupChild(file));
122+
}
123+
124+
pbxProject.prototype.addToPbxSourcesBuildPhase = function (file) {
125+
var sources = this.pbxSourcesBuildPhaseObj();
126+
sources.files.push(pbxSourceFileObj(file));
127+
}
128+
117129
pbxProject.prototype.pbxBuildFileSection = function () {
118130
return this.hash.project.objects['PBXBuildFile'];
119131
}

test/addHeaderFile.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var fullProject = require('./fixtures/full-project')
2+
fullProjectStr = JSON.stringify(fullProject),
3+
pbx = require('../lib/pbxProject'),
4+
pbxFile = require('../lib/pbxFile'),
5+
proj = new pbx('.');
6+
7+
function cleanHash() {
8+
return JSON.parse(fullProjectStr);
9+
}
10+
11+
exports.setUp = function (callback) {
12+
proj.hash = cleanHash();
13+
callback();
14+
}
15+
16+
exports.addHeaderFile = {
17+
'should return a pbxFile': function (test) {
18+
var newFile = proj.addHeaderFile('file.h');
19+
20+
test.equal(newFile.constructor, pbxFile);
21+
test.done()
22+
},
23+
'should set a fileRef on the pbxFile': function (test) {
24+
var newFile = proj.addHeaderFile('file.h');
25+
26+
test.ok(newFile.fileRef);
27+
test.done()
28+
},
29+
'should populate the PBXFileReference section with 2 fields': function (test) {
30+
var newFile = proj.addHeaderFile('file.h'),
31+
fileRefSection = proj.pbxFileReferenceSection(),
32+
frsLength = Object.keys(fileRefSection).length;
33+
34+
test.equal(68, frsLength);
35+
test.ok(fileRefSection[newFile.fileRef]);
36+
test.ok(fileRefSection[newFile.fileRef + '_comment']);
37+
38+
test.done();
39+
},
40+
'should populate the PBXFileReference comment correctly': function (test) {
41+
var newFile = proj.addHeaderFile('file.h'),
42+
fileRefSection = proj.pbxFileReferenceSection(),
43+
commentKey = newFile.fileRef + '_comment';
44+
45+
test.equal(fileRefSection[commentKey], 'file.h');
46+
test.done();
47+
},
48+
'should add the PBXFileReference object correctly': function (test) {
49+
var newFile = proj.addHeaderFile('Plugins/file.h'),
50+
fileRefSection = proj.pbxFileReferenceSection(),
51+
fileRefEntry = fileRefSection[newFile.fileRef];
52+
53+
test.equal(fileRefEntry.isa, 'PBXFileReference');
54+
test.equal(fileRefEntry.fileEncoding, 4);
55+
test.equal(fileRefEntry.lastKnownFileType, 'sourcecode.c.h');
56+
test.equal(fileRefEntry.name, 'file.h');
57+
test.equal(fileRefEntry.path, 'Plugins/file.h');
58+
test.equal(fileRefEntry.sourceTree, '"<group>"');
59+
60+
test.done();
61+
},
62+
'should add to the Plugins PBXGroup group': function (test) {
63+
var newFile = proj.addHeaderFile('Plugins/file.h'),
64+
plugins = proj.pbxGroupByName('Plugins');
65+
66+
test.equal(plugins.children.length, 1);
67+
test.done();
68+
},
69+
'should have the right values for the PBXGroup entry': function (test) {
70+
var newFile = proj.addHeaderFile('Plugins/file.h'),
71+
plugins = proj.pbxGroupByName('Plugins'),
72+
pluginObj = plugins.children[0];
73+
74+
test.equal(pluginObj.comment, 'file.h');
75+
test.equal(pluginObj.value, newFile.fileRef);
76+
test.done();
77+
}
78+
}

0 commit comments

Comments
 (0)