Skip to content

Instantly share code, notes, and snippets.

@ezodude
Last active August 29, 2015 14:01
Show Gist options
  • Save ezodude/36a28cf2a43fd6e1c5d1 to your computer and use it in GitHub Desktop.
Save ezodude/36a28cf2a43fd6e1c5d1 to your computer and use it in GitHub Desktop.
This is a Cordova CLI hook that resets an iOS deployment target in platform generated project file.
#!/usr/bin/env node
var IOS_DEPLOYMENT_TARGET = '7.0';
var fs = require("fs"),
path = require("path"),
shell = require("shelljs"),
xcode = require('xcode'),
projectRoot = process.argv[2];
function propReplace(obj, prop, value) {
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
if (typeof obj[p] === 'object') {
propReplace(obj[p], prop, value);
} else if (p === prop) {
obj[p] = value;
}
}
}
}
function updateDeploymentTarget(xcodeProject, xcodeProjectPath, targetVersion){
var buildConfig = xcodeProject.pbxXCBuildConfigurationSection();
propReplace(buildConfig, 'IPHONEOS_DEPLOYMENT_TARGET', targetVersion);
fs.writeFileSync(xcodeProjectPath, xcodeProject.writeSync(), 'utf-8');
}
function getProjectName(protoPath){
var cordovaConfigPath = path.join(protoPath, '.cordova', 'config.json'),
content = fs.readFileSync(cordovaConfigPath, 'utf-8'),
json = JSON.parse(content);
return json.name;
}
/*
This is our runner function. It sets up the project paths,
parses the project file using xcode and delegates to our updateDeploymentTarget
that does the actual work.
*/
function run(projectRoot){
var projectName = getProjectName(projectRoot),
xcodeProjectName = projectName + '.xcodeproj',
xcodeProjectPath = path.join(projectRoot, 'platforms', 'ios', xcodeProjectName, 'project.pbxproj'),
xcodeProject;
if(!fs.existsSync(xcodeProjectPath)) { return; }
xcodeProject = xcode.project(xcodeProjectPath);
shell.echo("Adjusting iOS deployment target for " + projectName + " to: [" + IOS_DEPLOYMENT_TARGET + "] ...");
xcodeProject.parse(function(err){
if(err){
shell.echo('An error occured during parsing of [' + xcodeProjectPath + ']: ' + JSON.stringify(err));
}else{
updateDeploymentTarget(xcodeProject, xcodeProjectPath, IOS_DEPLOYMENT_TARGET);
shell.echo('[' + xcodeProjectPath + '] now has deployment target set as:[' + IOS_DEPLOYMENT_TARGET + '] ...');
}
});
}
run(projectRoot);
@mirko77
Copy link

mirko77 commented May 20, 2015

This fails with Error: ENOENT, no such file or directory 'platform/.cordova/config.json'

@mpoisot
Copy link

mpoisot commented Jun 23, 2015

I ran into the same issue. I fixed it by using the getProjectName() method used by this other after_install hook.

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();
}

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