Last active
August 29, 2015 14:26
-
-
Save balos1/8b9f7bfd64436dbeed15 to your computer and use it in GitHub Desktop.
Apache Cordova hook to automatically timestamp builds for iOS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Don't forget to install xml2js using npm | |
// `$ npm install xml2js` | |
var fs = require('fs'); | |
var xml2js = require('xml2js'); | |
module.exports = function(context) { | |
var timestamp = (function() { | |
var now = new Date(); | |
var year = now.getUTCFullYear().toString().substring(2, 4); | |
var month = ("0" + (now.getUTCMonth() + 1)).slice(-2); | |
var day = ("0" + now.getUTCDate()).slice(-2); | |
var hours = ("0" + now.getUTCHours()).slice(-2); | |
var min = ("0" + now.getUTCMinutes()).slice(-2); | |
return year.toString() + month.toString() + day.toString() + hours.toString() + min.toString(); | |
})(); | |
// Read config.xml | |
fs.readFile('config.xml', 'utf8', function(err, data) { | |
if(err) { | |
return console.log(err); | |
} | |
// Get XML | |
var xml = data; | |
// Parse XML to JS Obj | |
xml2js.parseString(xml, function (err, result) { | |
if(err) { | |
return console.log(err); | |
} | |
// Get JS Obj | |
var obj = result; | |
// ios-CFBundleVersion doen't exist in config.xml | |
if(typeof obj['widget']['$']['ios-CFBundleVersion'] === 'undefined') { | |
obj['widget']['$']['ios-CFBundleVersion'] = timestamp; | |
} | |
// android-versionCode doen't exist in config.xml | |
// if(typeof obj['widget']['$']['android-versionCode'] === 'undefined') { | |
// obj['widget']['$']['android-versionCode'] = timestamp; | |
// } | |
// Increment build numbers (separately for iOS and Android) | |
obj['widget']['$']['ios-CFBundleVersion'] = timestamp; | |
// obj['widget']['$']['android-versionCode'] = timestamp; | |
// Build XML from JS Obj | |
var builder = new xml2js.Builder(); | |
var xml = builder.buildObject(obj); | |
// Write config.xml | |
fs.writeFile('config.xml', xml, function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log('Build number successfully incremented to: ' + timestamp); | |
}); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<hook type="before_prepare" src="/path/to/automatic_timestamped_builds.js" /> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Android versionCode does not support timestamps in the versionCode, thus I am removing android support for now.