Skip to content

Instantly share code, notes, and snippets.

@boboldehampsink
Created March 17, 2015 15:11
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save boboldehampsink/aac789c2864e16db3a14 to your computer and use it in GitHub Desktop.
Save boboldehampsink/aac789c2864e16db3a14 to your computer and use it in GitHub Desktop.
Cordova - after_platform_add hook to disable push notifications code
#!/usr/bin/env node
var GCC_PREPROCESSOR_DEFINITIONS = '"$(inherited) DISABLE_PUSH_NOTIFICATIONS=1"';
var fs = require("fs");
var path = require("path");
var xcode = require('xcode');
var projectRoot = process.argv[2];
function getProjectName(protoPath) {
var cordovaConfigPath = path.join(protoPath, 'config.xml');
var content = fs.readFileSync(cordovaConfigPath, 'utf-8');
return /<name>([\s\S]*)<\/name>/mi.exec(content)[1].trim();
}
function run(projectRoot) {
var projectName = getProjectName(projectRoot);
var xcodeProjectName = projectName + '.xcodeproj';
var xcodeProjectPath = path.join(projectRoot, 'platforms', 'ios', xcodeProjectName, 'project.pbxproj');
var xcodeProject;
if (!fs.existsSync(xcodeProjectPath)) {
return;
}
xcodeProject = xcode.project(xcodeProjectPath);
console.log('Setting GCC Preprocessor Definitions for ' + projectName + ' to: [' + GCC_PREPROCESSOR_DEFINITIONS + '] ...');
xcodeProject.parse(function(error){
if(error){
console.log('An error occured during parsing of [' + xcodeProjectPath + ']: ' + JSON.stringify(error));
} else {
var configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection());
for (var config in configurations) {
var buildSettings = configurations[config].buildSettings;
buildSettings.GCC_PREPROCESSOR_DEFINITIONS = GCC_PREPROCESSOR_DEFINITIONS;
}
fs.writeFileSync(xcodeProjectPath, xcodeProject.writeSync(), 'utf-8');
console.log('[' + xcodeProjectPath + '] now has GCC Preprocessor Definitions set to:[' + GCC_PREPROCESSOR_DEFINITIONS + '] ...');
}
});
}
var COMMENT_KEY = /_comment$/;
function nonComments(obj) {
var keys = Object.keys(obj),
newObj = {}, i = 0;
for (i; i < keys.length; i++) {
if (!COMMENT_KEY.test(keys[i])) {
newObj[keys[i]] = obj[keys[i]];
}
}
return newObj;
}
run(projectRoot);
@mpoisot
Copy link

mpoisot commented Apr 17, 2015

Thanks for the useful script. I had to npm install xcode before it worked. Also, I temporarily put it into hooks/before_prepare so that I could apply it to my existing platforms/ios folder without having to drop/add the platform.

@Delagen
Copy link

Delagen commented Aug 31, 2015

this is deprecated behavior due config.xml hooks implemented in cordova 5.x
rewrited as gist

Copy link

ghost commented Sep 7, 2015

Didn't work DISABLE_PUSH_NOTIFICATIONS=1 for me, but DISABLE_PUSH_NOTIFICATIONS=true did

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment