Skip to content

Instantly share code, notes, and snippets.

@Delagen
Last active September 24, 2016 02:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Delagen/47d74d9604f5b8e9af45 to your computer and use it in GitHub Desktop.
Save Delagen/47d74d9604f5b8e9af45 to your computer and use it in GitHub Desktop.
"use strict";
/*use in config.xml <hook type="after_platform_add" src="../path/to/cordova-ios-disable-push.js"/>*/
var fs = require("fs");
var path = require("path");
var COMMENT_KEY = /_comment$/;
function nonComments(obj) {
var newObj = {};
Object.keys(obj).forEach(function(key) {
if (!COMMENT_KEY.test(key)) {
newObj[key] = obj[key];
}
});
return newObj;
}
module.exports = function(ctx) {
var GCC_PREPROCESSOR_DEFINITIONS = '"$(inherited) DISABLE_PUSH_NOTIFICATIONS=1"';
var q = ctx.requireCordovaModule("q");
var deferred = q.defer();
var cordovaConfigPath = path.join(ctx.opts.projectRoot, "config.xml");
fs.readFile(cordovaConfigPath, {encoding: "utf-8"}, function(errConfigRead, configContent) {
if (errConfigRead) {
return deferred.reject(errConfigRead);
}
var projectName = /<name[^>]*>([\s\S]*)<\/name>/mi.exec(configContent)[1].trim();
var xcodeProjectName = [projectName, ".xcodeproj"].join("");
var xcodeProjectPath = path.join(ctx.opts.projectRoot, "platforms", "ios", xcodeProjectName, "project.pbxproj");
var xcode = ctx.requireCordovaModule("xcode");
var xcodeProject = xcode.project(xcodeProjectPath);
xcodeProject.parse(function(parseError) {
if (parseError) {
return deferred.reject(parseError);
}
var configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection());
Object.keys(configurations).forEach(function(config) {
var buildSettings = configurations[config].buildSettings;
buildSettings.GCC_PREPROCESSOR_DEFINITIONS = GCC_PREPROCESSOR_DEFINITIONS;
});
fs.writeFile(xcodeProjectPath, /*eslint-disable no-sync*/xcodeProject.writeSync()/*eslint-enable*/, {encoding: "utf-8"}, function(projectWriteError) {
if (projectWriteError) {
return deferred.reject(projectWriteError);
}
deferred.resolve();
});
});
});
return deferred.promise;
};
@MrAntix
Copy link

MrAntix commented Sep 9, 2015

Hi, thanks for this, where in config.xml do you add it? perhaps ...

<platform name="ios">
...
    <hook type="after_platform_add" src="../path/to/cordova-ios-disable-push.js"/>
</platform>

ok, yes, see here (for others I know you know XD)

https://cordova.apache.org/docs/en/edge/guide_appdev_hooks_index.md.html#Hooks%20Guide

@Delagen
Copy link
Author

Delagen commented Sep 14, 2015

Fucking project "xcode". Rewrited this into simply Regexp replace.
One this part of code make me sad (((

/ parsing is slow and blocking right now
// so we do it in a separate process
var fs = require('fs'),
    parser = require('./parser/pbxproj'),
    path = process.argv[2],
    fileContents, obj;

try {
    fileContents = fs.readFileSync(path, 'utf-8'),
    obj = parser.parse(fileContents)
    process.send(obj)
    process.exit()
} catch (e) {
    process.send(e)
    process.exit(1)
}

But I'm glad if this code works for someone. In my situation it simply close process on project.parse.

@jmatthiesen
Copy link

Trying this in a project, I (and another dev) were finding that the xcode module couldn't be found. I made another version of this script which uses ctx.requireCordovaModule("xcode") and that fixed the issue. See my gist and feel free to merge the changes in here, I'd rather have one "source of truth" for this hook, than too many forks.

@Delagen
Copy link
Author

Delagen commented Sep 17, 2015

I just installed xcode globally, but this module working bad, and I simply rewrite code to regexp replace, which do work more predictable
May be such problem can be solved by using older version of nodejs

@njleonzhang
Copy link

@Delagen, if I change

var GCC_PREPROCESSOR_DEFINITIONS = '"$(inherited) DISABLE_PUSH_NOTIFICATIONS=1"';
to
var GCC_PREPROCESSOR_DEFINITIONS = '"$(inherited) DISABLE_PUSH_NOTIFICATIONS=0"';

Can I make the push enable? I need to enable push

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