Skip to content

Instantly share code, notes, and snippets.

@balos1
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balos1/8b9f7bfd64436dbeed15 to your computer and use it in GitHub Desktop.
Save balos1/8b9f7bfd64436dbeed15 to your computer and use it in GitHub Desktop.
Apache Cordova hook to automatically timestamp builds for iOS.
#!/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);
});
});
});
};
...
<hook type="before_prepare" src="/path/to/automatic_timestamped_builds.js" />
...
@balos1
Copy link
Author

balos1 commented Aug 7, 2015

Android versionCode does not support timestamps in the versionCode, thus I am removing android support for now.

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