-
-
Save mavenius/36278821b0e33e990ba6464a5eb664aa to your computer and use it in GitHub Desktop.
Build number hook
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 | |
// Based on the 'replace text' hook found here: http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/ | |
// This hook should be placed in the 'after_prepare' hook folder. | |
// The hook relies on a JSON file located at '<project_root>/resources/build_number.json' to track the build number. | |
// build.json content: | |
// { "major": 1, "minor": 2, "build": 3 } | |
// this example would generate build number "1.2.4" (because the build is incremented before, so build_number.json reflects the most recent build.) | |
// Add 'BUILD_NUMBER' to the version number in the '<project_root>/config.xml' file. | |
// this plugin replaces arbitrary text in arbitrary files | |
// | |
// Look for the string CONFIGURE HERE for areas that need configuration | |
// | |
console.log("Starting build number update"); | |
var fs = require('fs'); | |
var path = require('path'); | |
var rootDir = process.argv[2]; | |
function replace_string_in_file(filename, to_replace, replace_with) { | |
console.log('Replacing entries in file ' + filename); | |
var data = fs.readFileSync(filename, 'utf8'); | |
var result = data.replace(new RegExp(to_replace, "g"), replace_with); | |
fs.writeFileSync(filename, result, 'utf8'); | |
} | |
function escapeSpecialChars(jsonString){ | |
return jsonString.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t').replace(/\f/g, '\\f'); | |
} | |
if (rootDir) { | |
var buildFile = path.join(rootDir, "resources", "build_number.json"); | |
console.log('Parsing build_number.json'); | |
var buildObj = JSON.parse(escapeSpecialChars(fs.readFileSync(buildFile, 'utf8'))); | |
// CONFIGURE HERE | |
// with the names of the files that contain tokens you want replaced. Replace files that have been copied via the prepare step. | |
var buildsToReplace = [ | |
//ios | |
"platforms/ios/appName/config.xml", | |
"platforms/ios/appName/appName-info.plist", | |
//android | |
//"platforms/android/AndroidManifest.xml", | |
//"platforms/android/ant-build/AndroidManifest.cordova.xml", | |
//"platforms/android/res/xml/config.xml" | |
]; | |
buildObj.build++; | |
var buildNumberString = buildObj.major.toString() + '.' + buildObj.minor.toString() + '.' + buildObj.build.toString(); | |
buildsToReplace.forEach(function(val, index, array) { | |
"use strict"; | |
var fullFilename = path.join(rootDir, val); | |
if (fs.existsSync(fullFilename)) { | |
replace_string_in_file(fullFilename, "BUILD_NUMBER", buildNumberString); | |
// ... any other configuration | |
} else { | |
//console.log("missing: "+fullfilename); | |
} | |
}); | |
console.log('Build number updated to ' + buildNumberString); | |
fs.writeFileSync(buildFile, JSON.stringify(buildObj), ["utf8"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment