Skip to content

Commit bace240

Browse files
committed
[pbxProject] addResourceFile
1 parent 5d3ce93 commit bace240

File tree

3 files changed

+188
-14
lines changed

3 files changed

+188
-14
lines changed

lib/pbxFile.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ function detectLastType(path) {
2323
return 'unknown';
2424
}
2525

26+
function fileEncoding(file) {
27+
if (file.lastType != BUNDLE) {
28+
return DEFAULT_FILE_ENCODING;
29+
}
30+
}
31+
2632
function pbxFile(filepath, opt) {
2733
var opt = opt || {};
2834

@@ -37,7 +43,7 @@ function pbxFile(filepath, opt) {
3743
}
3844

3945
this.sourceTree = opt.sourceTree || DEFAULT_SOURCE_TREE;
40-
this.fileEncoding = opt.fileEncoding || DEFAULT_FILE_ENCODING;
46+
this.fileEncoding = opt.fileEncoding || fileEncoding(this);
4147
}
4248

4349
module.exports = pbxFile;

lib/pbxProject.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ pbxProject.prototype.addSourceFile = function (path, opt) {
7373
file.uuid = this.generateUuid();
7474
file.fileRef = this.generateUuid();
7575

76-
this.addToPbxBuildFileSection(file); // PBXBuildFile
77-
this.addToPbxFileReferenceSection(file); // PBXFileReference
78-
this.addToPluginsPbxGroup(file); // PBXGroup
79-
this.addToPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
76+
this.addToPbxBuildFileSection(file); // PBXBuildFile
77+
this.addToPbxFileReferenceSection(file); // PBXFileReference
78+
this.addToPluginsPbxGroup(file); // PBXGroup
79+
this.addToPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
8080

8181
return file;
8282
}
@@ -86,22 +86,31 @@ pbxProject.prototype.addHeaderFile = function (path, opt) {
8686

8787
file.fileRef = this.generateUuid();
8888

89-
this.addToPbxFileReferenceSection(file); // PBXFileReference
90-
this.addToPluginsPbxGroup(file); // PBXGroup
89+
this.addToPbxFileReferenceSection(file); // PBXFileReference
90+
this.addToPluginsPbxGroup(file); // PBXGroup
9191

9292
return file;
9393
}
9494

9595
pbxProject.prototype.addResourceFile = function (path, opt) {
96+
var file = new pbxFile(path, opt);
97+
98+
file.uuid = this.generateUuid();
99+
file.fileRef = this.generateUuid();
100+
101+
this.addToPbxBuildFileSection(file); // PBXBuildFile
102+
this.addToPbxFileReferenceSection(file); // PBXFileReference
103+
this.addToPluginsPbxGroup(file); // PBXGroup
104+
this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
105+
96106
/*
97-
* PBXBuildFile
98-
* PBXFileReference
99-
* PBXGroup (Plugins)
100107
* PBXResourcesBuildPhase
101108
*/
109+
110+
return file;
102111
}
103112

104-
// helper access functions
113+
// helper addition functions
105114
pbxProject.prototype.addToPbxBuildFileSection = function (file) {
106115
var commentKey = f("%s_comment", file.uuid);
107116

@@ -123,9 +132,15 @@ pbxProject.prototype.addToPluginsPbxGroup = function (file) {
123132

124133
pbxProject.prototype.addToPbxSourcesBuildPhase = function (file) {
125134
var sources = this.pbxSourcesBuildPhaseObj();
126-
sources.files.push(pbxSourceFileObj(file));
135+
sources.files.push(pbxBuildPhaseObj(file));
136+
}
137+
138+
pbxProject.prototype.addToPbxResourcesBuildPhase = function (file) {
139+
var sources = this.pbxResourcesBuildPhaseObj();
140+
sources.files.push(pbxBuildPhaseObj(file));
127141
}
128142

143+
// helper access functions
129144
pbxProject.prototype.pbxBuildFileSection = function () {
130145
return this.hash.project.objects['PBXBuildFile'];
131146
}
@@ -172,6 +187,27 @@ pbxProject.prototype.pbxSourcesBuildPhaseObj = function () {
172187
return null;
173188
}
174189

190+
pbxProject.prototype.pbxResourcesBuildPhaseSection = function () {
191+
return this.hash.project.objects['PBXResourcesBuildPhase'];
192+
}
193+
194+
pbxProject.prototype.pbxResourcesBuildPhaseObj = function () {
195+
var section = this.pbxResourcesBuildPhaseSection(),
196+
obj, sectionKey;
197+
198+
for (key in section) {
199+
// only look for comments
200+
if (!COMMENT_KEY.test(key)) continue;
201+
202+
if (section[key] == 'Resources') {
203+
sectionKey = key.split(COMMENT_KEY)[0];
204+
return section[sectionKey];
205+
}
206+
}
207+
208+
return null;
209+
}
210+
175211
// helper object creation functions
176212
function pbxBuildFileObj(file) {
177213
var obj = Object.create(null);
@@ -187,12 +223,14 @@ function pbxFileReferenceObj(file) {
187223
var obj = Object.create(null);
188224

189225
obj.isa = 'PBXFileReference';
190-
obj.fileEncoding = file.fileEncoding;
191226
obj.lastKnownFileType = file.lastType;
192227
obj.name = file.basename;
193228
obj.path = file.path;
194229
obj.sourceTree = file.sourceTree;
195230

231+
if (file.fileEncoding)
232+
obj.fileEncoding = file.fileEncoding;
233+
196234
return obj;
197235
}
198236

@@ -205,7 +243,7 @@ function pbxGroupChild(file) {
205243
return obj;
206244
}
207245

208-
function pbxSourceFileObj(file) {
246+
function pbxBuildPhaseObj(file) {
209247
var obj = Object.create(null);
210248

211249
obj.value = file.uuid;

test/addResourceFile.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.addResourceFile = {
17+
'should return a pbxFile': function (test) {
18+
var newFile = proj.addResourceFile('assets.bundle');
19+
20+
test.equal(newFile.constructor, pbxFile);
21+
test.done()
22+
},
23+
'should set a uuid on the pbxFile': function (test) {
24+
var newFile = proj.addResourceFile('assets.bundle');
25+
26+
test.ok(newFile.uuid);
27+
test.done()
28+
},
29+
'should set a fileRef on the pbxFile': function (test) {
30+
var newFile = proj.addResourceFile('assets.bundle');
31+
32+
test.ok(newFile.fileRef);
33+
test.done()
34+
},
35+
'should populate the PBXBuildFile section with 2 fields': function (test) {
36+
var newFile = proj.addResourceFile('assets.bundle'),
37+
buildFileSection = proj.pbxBuildFileSection(),
38+
bfsLength = Object.keys(buildFileSection).length;
39+
40+
test.equal(60, bfsLength);
41+
test.ok(buildFileSection[newFile.uuid]);
42+
test.ok(buildFileSection[newFile.uuid + '_comment']);
43+
44+
test.done();
45+
},
46+
'should add the PBXBuildFile comment correctly': function (test) {
47+
var newFile = proj.addResourceFile('assets.bundle'),
48+
commentKey = newFile.uuid + '_comment',
49+
buildFileSection = proj.pbxBuildFileSection();
50+
51+
test.equal(buildFileSection[commentKey], 'assets.bundle in Resources');
52+
test.done();
53+
},
54+
'should add the PBXBuildFile object correctly': function (test) {
55+
var newFile = proj.addResourceFile('assets.bundle'),
56+
buildFileSection = proj.pbxBuildFileSection(),
57+
buildFileEntry = buildFileSection[newFile.uuid];
58+
59+
test.equal(buildFileEntry.isa, 'PBXBuildFile');
60+
test.equal(buildFileEntry.fileRef, newFile.fileRef);
61+
test.equal(buildFileEntry.fileRef_comment, 'assets.bundle');
62+
63+
test.done();
64+
},
65+
'should populate the PBXFileReference section with 2 fields': function (test) {
66+
var newFile = proj.addResourceFile('assets.bundle'),
67+
fileRefSection = proj.pbxFileReferenceSection(),
68+
frsLength = Object.keys(fileRefSection).length;
69+
70+
test.equal(68, frsLength);
71+
test.ok(fileRefSection[newFile.fileRef]);
72+
test.ok(fileRefSection[newFile.fileRef + '_comment']);
73+
74+
test.done();
75+
},
76+
'should populate the PBXFileReference comment correctly': function (test) {
77+
var newFile = proj.addResourceFile('assets.bundle'),
78+
fileRefSection = proj.pbxFileReferenceSection(),
79+
commentKey = newFile.fileRef + '_comment';
80+
81+
test.equal(fileRefSection[commentKey], 'assets.bundle');
82+
test.done();
83+
},
84+
'should add the PBXFileReference object correctly': function (test) {
85+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
86+
fileRefSection = proj.pbxFileReferenceSection(),
87+
fileRefEntry = fileRefSection[newFile.fileRef];
88+
89+
test.equal(fileRefEntry.isa, 'PBXFileReference');
90+
test.equal(fileRefEntry.fileEncoding, undefined);
91+
test.equal(fileRefEntry.lastKnownFileType, '"wrapper.plug-in"');
92+
test.equal(fileRefEntry.name, 'assets.bundle');
93+
test.equal(fileRefEntry.path, 'Plugins/assets.bundle');
94+
test.equal(fileRefEntry.sourceTree, '"<group>"');
95+
96+
test.done();
97+
},
98+
'should add to the Plugins PBXGroup group': function (test) {
99+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
100+
plugins = proj.pbxGroupByName('Plugins');
101+
102+
test.equal(plugins.children.length, 1);
103+
test.done();
104+
},
105+
'should have the right values for the PBXGroup entry': function (test) {
106+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
107+
plugins = proj.pbxGroupByName('Plugins'),
108+
pluginObj = plugins.children[0];
109+
110+
test.equal(pluginObj.comment, 'assets.bundle');
111+
test.equal(pluginObj.value, newFile.fileRef);
112+
test.done();
113+
},
114+
'should add to the PBXSourcesBuildPhase': function (test) {
115+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
116+
sources = proj.pbxResourcesBuildPhaseObj();
117+
118+
test.equal(sources.files.length, 13);
119+
test.done();
120+
},
121+
'should have the right values for the Sources entry': function (test) {
122+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
123+
sources = proj.pbxResourcesBuildPhaseObj(),
124+
sourceObj = sources.files[12];
125+
126+
test.equal(sourceObj.comment, 'assets.bundle in Resources');
127+
test.equal(sourceObj.value, newFile.uuid);
128+
test.done();
129+
}
130+
}

0 commit comments

Comments
 (0)