Skip to content

Commit 88a027e

Browse files
author
Richard Wong
committed
Add addBuildProperty and removeBuildProperty
into pbxProject
1 parent dadf03c commit 88a027e

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

lib/pbxProject.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,31 @@ pbxProject.prototype.buildPhaseObject = function(name, group, target) {
823823
return null;
824824
}
825825

826+
pbxProject.prototype.addBuildProperty = function(prop, value, build_name) {
827+
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
828+
key, configuration;
829+
830+
for (key in configurations){
831+
configuration = configurations[key];
832+
if (!build_name || configuration.name === build_name){
833+
configuration.buildSettings[prop] = value;
834+
}
835+
}
836+
}
837+
838+
pbxProject.prototype.removeBuildProperty = function(prop, build_name) {
839+
var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
840+
key, configuration;
841+
842+
for (key in configurations){
843+
configuration = configurations[key];
844+
console.error(configuration);
845+
if (configuration.buildSettings[prop] &&
846+
!build_name || configuration.name === build_name){
847+
delete configuration.buildSettings[prop];
848+
}
849+
}
850+
}
826851

827852
pbxProject.prototype.updateBuildProperty = function(prop, value) {
828853
var config = this.pbxXCBuildConfigurationSection();

test/pbxProject.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,90 @@ exports['updateBuildProperty function'] = {
177177
}
178178
}
179179

180+
exports['addBuildProperty function'] = {
181+
setUp:function(callback) {
182+
callback();
183+
},
184+
tearDown:function(callback) {
185+
fs.writeFileSync(bcpbx, original_pbx, 'utf-8');
186+
callback();
187+
},
188+
'should add 4 build properties in the .pbxproj file': function (test) {
189+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
190+
myProj.parse(function(err, hash) {
191+
myProj.addBuildProperty('ENABLE_BITCODE', 'NO');
192+
var newContents = myProj.writeSync();
193+
test.equal(newContents.match(/ENABLE_BITCODE\s*=\s*NO/g).length, 4);
194+
test.done();
195+
});
196+
},
197+
'should add 2 build properties in the .pbxproj file for specific build': function (test) {
198+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
199+
myProj.parse(function(err, hash) {
200+
myProj.addBuildProperty('ENABLE_BITCODE', 'NO', 'Release');
201+
var newContents = myProj.writeSync();
202+
test.equal(newContents.match(/ENABLE_BITCODE\s*=\s*NO/g).length, 2);
203+
test.done();
204+
});
205+
},
206+
'should not add build properties in the .pbxproj file for nonexist build': function (test) {
207+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
208+
myProj.parse(function(err, hash) {
209+
myProj.addBuildProperty('ENABLE_BITCODE', 'NO', 'nonexist');
210+
var newContents = myProj.writeSync();
211+
test.ok(!newContents.match(/ENABLE_BITCODE\s*=\s*NO/g));
212+
test.done();
213+
});
214+
}
215+
}
216+
217+
exports['removeBuildProperty function'] = {
218+
setUp:function(callback) {
219+
callback();
220+
},
221+
tearDown:function(callback) {
222+
fs.writeFileSync(bcpbx, original_pbx, 'utf-8');
223+
callback();
224+
},
225+
'should remove all build properties in the .pbxproj file': function (test) {
226+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
227+
myProj.parse(function(err, hash) {
228+
myProj.removeBuildProperty('IPHONEOS_DEPLOYMENT_TARGET');
229+
var newContents = myProj.writeSync();
230+
test.ok(!newContents.match(/IPHONEOS_DEPLOYMENT_TARGET/));
231+
test.done();
232+
});
233+
},
234+
'should remove specific build properties in the .pbxproj file': function (test) {
235+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
236+
myProj.parse(function(err, hash) {
237+
myProj.removeBuildProperty('IPHONEOS_DEPLOYMENT_TARGET', 'Debug');
238+
var newContents = myProj.writeSync();
239+
test.equal(newContents.match(/IPHONEOS_DEPLOYMENT_TARGET/g).length, 2);
240+
test.done();
241+
});
242+
},
243+
'should not remove any build properties in the .pbxproj file': function (test) {
244+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
245+
myProj.parse(function(err, hash) {
246+
myProj.removeBuildProperty('IPHONEOS_DEPLOYMENT_TARGET', 'notexist');
247+
var newContents = myProj.writeSync();
248+
test.equal(newContents.match(/IPHONEOS_DEPLOYMENT_TARGET/g).length, 4);
249+
test.done();
250+
});
251+
},
252+
'should fine with remove inexist build properties in the .pbxproj file': function (test) {
253+
var myProj = new pbx('test/parser/projects/build-config.pbxproj');
254+
myProj.parse(function(err, hash) {
255+
myProj.removeBuildProperty('ENABLE_BITCODE');
256+
var newContents = myProj.writeSync();
257+
test.ok(!newContents.match(/ENABLE_BITCODE/));
258+
test.done();
259+
});
260+
}
261+
262+
}
263+
180264
exports['productName field'] = {
181265
'should return the product name': function (test) {
182266
var newProj = new pbx('.');

0 commit comments

Comments
 (0)