Skip to content

Commit 347651f

Browse files
committed
[project] check if file exists before adding
1 parent cd937af commit 347651f

6 files changed

+116
-6
lines changed

lib/pbxProject.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ pbxProject.prototype.addPluginFile = function (path, opt) {
8888
correctForPluginsPath(file, this);
8989

9090
// null is better for early errors
91-
if (this.hasFile(file.path)) {
92-
return null;
93-
}
91+
if (this.hasFile(file.path)) return null;
9492

9593
file.fileRef = this.generateUuid();
9694

@@ -103,15 +101,17 @@ pbxProject.prototype.addPluginFile = function (path, opt) {
103101
pbxProject.prototype.removePluginFile = function (path, opt) {
104102
var file = new pbxFile(path, opt);
105103
correctForPluginsPath(file, this);
106-
104+
107105
this.removeFromPbxFileReferenceSection(file); // PBXFileReference
108106
this.removeFromPluginsPbxGroup(file); // PBXGroup
109107

110108
return file;
111109
}
112110

113111
pbxProject.prototype.addSourceFile = function (path, opt) {
114-
var file = this.addPluginFile(path, opt)
112+
var file = this.addPluginFile(path, opt);
113+
114+
if (!file) return false;
115115

116116
file.uuid = this.generateUuid();
117117

@@ -144,8 +144,10 @@ pbxProject.prototype.addResourceFile = function (path, opt) {
144144

145145
if (opt.plugin) {
146146
file = this.addPluginFile(path, opt);
147+
if (!file) return false;
147148
} else {
148149
file = new pbxFile(path, opt);
150+
if (this.hasFile(file.path)) return false;
149151
}
150152

151153
file.uuid = this.generateUuid();
@@ -182,6 +184,9 @@ pbxProject.prototype.removeResourceFile = function (path, opt) {
182184
pbxProject.prototype.addFramework = function (path, opt) {
183185
var file = new pbxFile(path, opt);
184186

187+
// catch duplicates
188+
if (this.hasFile(file.path)) return false;
189+
185190
file.uuid = this.generateUuid();
186191
file.fileRef = this.generateUuid();
187192

@@ -211,8 +216,10 @@ pbxProject.prototype.addStaticLibrary = function (path, opt) {
211216

212217
if (opt.plugin) {
213218
file = this.addPluginFile(path, opt);
219+
if (!file) return false;
214220
} else {
215221
file = new pbxFile(path, opt);
222+
if (this.hasFile(file.path)) return false;
216223
}
217224

218225
file.uuid = this.generateUuid();
@@ -309,7 +316,6 @@ pbxProject.prototype.removeFromResourcesPbxGroup = function (file) {
309316
}
310317
}
311318

312-
313319
pbxProject.prototype.addToFrameworksPbxGroup = function (file) {
314320
var pluginsGroup = this.pbxGroupByName('Frameworks');
315321
pluginsGroup.children.push(pbxGroupChild(file));

test/addFramework.js

+8
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,13 @@ exports.addFramework = {
133133
test.equal(framework.comment, 'libsqlite3.dylib in Frameworks');
134134
test.equal(framework.value, newFile.uuid);
135135
test.done();
136+
},
137+
'duplicate entries': {
138+
'should return false': function (test) {
139+
var newFile = proj.addFramework('libsqlite3.dylib');
140+
141+
test.ok(!proj.addFramework('libsqlite3.dylib'));
142+
test.done();
143+
}
136144
}
137145
}

test/addHeaderFile.js

+20
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,25 @@ exports.addHeaderFile = {
7474
test.equal(pluginObj.comment, 'file.h');
7575
test.equal(pluginObj.value, newFile.fileRef);
7676
test.done();
77+
},
78+
'duplicate entries': {
79+
'should return false': function (test) {
80+
var newFile = proj.addHeaderFile('Plugins/file.h');
81+
82+
test.ok(!proj.addHeaderFile('Plugins/file.h'));
83+
test.done();
84+
},
85+
'should not add another entry anywhere': function (test) {
86+
var newFile = proj.addHeaderFile('Plugins/file.h'),
87+
fileRefSection = proj.pbxFileReferenceSection(),
88+
frsLength = Object.keys(fileRefSection).length,
89+
plugins = proj.pbxGroupByName('Plugins');
90+
91+
proj.addHeaderFile('Plugins/file.h');
92+
93+
test.equal(68, frsLength);
94+
test.equal(plugins.children.length, 1);
95+
test.done();
96+
}
7797
}
7898
}

test/addResourceFile.js

+34
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,40 @@ exports.addResourceFile = {
210210
test.done();
211211
}
212212
},
213+
'duplicate entries': {
214+
'should return false': function (test) {
215+
var newFile = proj.addResourceFile('Plugins/assets.bundle');
216+
217+
test.ok(!proj.addResourceFile('Plugins/assets.bundle'));
218+
test.done();
219+
},
220+
'should return false (plugin entries)': function (test) {
221+
var newFile = proj.addResourceFile('Plugins/assets.bundle',
222+
{ plugin: true });
223+
224+
test.ok(!proj.addResourceFile('Plugins/assets.bundle',
225+
{ plugin: true }));
226+
test.done();
227+
},
228+
'should not add another entry anywhere': function (test) {
229+
var newFile = proj.addResourceFile('Plugins/assets.bundle'),
230+
buildFileSection = proj.pbxBuildFileSection(),
231+
bfsLength = Object.keys(buildFileSection).length,
232+
fileRefSection = proj.pbxFileReferenceSection(),
233+
frsLength = Object.keys(fileRefSection).length,
234+
resources = proj.pbxGroupByName('Resources'),
235+
sources = proj.pbxResourcesBuildPhaseObj();
236+
237+
proj.addResourceFile('Plugins/assets.bundle');
238+
239+
// check lengths
240+
test.equal(60, bfsLength);
241+
test.equal(68, frsLength);
242+
test.equal(resources.children.length, 10);
243+
test.equal(sources.files.length, 13);
244+
test.done();
245+
}
246+
},
213247
tearDown: function (callback) {
214248
delete proj.pbxGroupByName('Resources').path;
215249
callback();

test/addSourceFile.js

+26
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ exports.addSourceFile = {
126126
test.equal(sourceObj.comment, 'file.m in Sources');
127127
test.equal(sourceObj.value, newFile.uuid);
128128
test.done();
129+
},
130+
'duplicate entries': {
131+
'should return false': function (test) {
132+
var newFile = proj.addSourceFile('Plugins/file.m');
133+
134+
test.ok(!proj.addSourceFile('Plugins/file.m'));
135+
test.done();
136+
},
137+
'should not add another entry anywhere': function (test) {
138+
var newFile = proj.addSourceFile('Plugins/file.m'),
139+
buildFileSection = proj.pbxBuildFileSection(),
140+
bfsLength = Object.keys(buildFileSection).length,
141+
fileRefSection = proj.pbxFileReferenceSection(),
142+
frsLength = Object.keys(fileRefSection).length,
143+
plugins = proj.pbxGroupByName('Plugins'),
144+
sources = proj.pbxSourcesBuildPhaseObj();
145+
146+
// duplicate!
147+
proj.addSourceFile('Plugins/file.m');
148+
149+
test.equal(60, bfsLength); // BuildFileSection
150+
test.equal(68, frsLength); // FileReferenceSection
151+
test.equal(plugins.children.length, 1); // Plugins pbxGroup
152+
test.equal(sources.files.length, 3); // SourcesBuildPhhase
153+
test.done();
154+
}
129155
}
130156
}
131157

test/addStaticLibrary.js

+16
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,21 @@ exports.addStaticLibrary = {
231231

232232
test.done();
233233
}
234+
},
235+
'duplicate entries': {
236+
'should return false': function (test) {
237+
var newFile = proj.addStaticLibrary('libGoogleAnalytics.a');
238+
239+
test.ok(!proj.addStaticLibrary('libGoogleAnalytics.a'));
240+
test.done();
241+
},
242+
'should return false (plugin entries)': function (test) {
243+
var newFile = proj.addStaticLibrary('Plugins/libGoogleAnalytics.a',
244+
{ plugin: true });
245+
246+
test.ok(!proj.addStaticLibrary('Plugins/libGoogleAnalytics.a',
247+
{ plugin: true }));
248+
test.done();
249+
},
234250
}
235251
}

0 commit comments

Comments
 (0)