Last active
December 28, 2020 00:05
-
-
Save DavidFrahm/2ce16cb6aec90b2c428f to your computer and use it in GitHub Desktop.
Cordova build hook, to use build version from config.xml in your hybrid app
This file contains hidden or 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 | |
// This plugin replaces text in a file with the app version from config.xml. | |
var wwwFileToReplace = "js/build.js"; | |
var fs = require('fs'); | |
var path = require('path'); | |
var rootdir = process.argv[2]; | |
function loadConfigXMLDoc(filePath) { | |
var fs = require('fs'); | |
var xml2js = require('xml2js'); | |
var json = ""; | |
try { | |
var fileData = fs.readFileSync(filePath, 'ascii'); | |
var parser = new xml2js.Parser(); | |
parser.parseString(fileData.substring(0, fileData.length), function (err, result) { | |
//console.log("config.xml as JSON", JSON.stringify(result, null, 2)); | |
json = result; | |
}); | |
console.log("File '" + filePath + "' was successfully read."); | |
return json; | |
} catch (ex) { | |
console.log(ex) | |
} | |
} | |
function replace_string_in_file(filename, to_replace, replace_with) { | |
var data = fs.readFileSync(filename, 'utf8'); | |
var result = data.replace(new RegExp(to_replace, "g"), replace_with); | |
fs.writeFileSync(filename, result, 'utf8'); | |
} | |
var configXMLPath = "config.xml"; | |
var rawJSON = loadConfigXMLDoc(configXMLPath); | |
var version = rawJSON.widget.$.version; | |
console.log("Version:", version); | |
var rootdir = process.argv[2]; | |
var currentBuildPlatforms = process.env.CORDOVA_PLATFORMS.split(","); | |
console.log("Current build platforms: ", currentBuildPlatforms); | |
if (rootdir) { | |
currentBuildPlatforms.forEach(function(val, index, array) { | |
var wwwPath = ""; | |
switch(val) { | |
case "ios": | |
wwwPath = "platforms/ios/www/"; | |
break; | |
case "android": | |
wwwPath = "platforms/android/assets/www/"; | |
break; | |
default: | |
console.log("Unknown build platform: " + val); | |
} | |
var fullfilename = path.join(rootdir, wwwPath + wwwFileToReplace); | |
if (fs.existsSync(fullfilename)) { | |
replace_string_in_file(fullfilename, "%%VERSION%%", version); | |
console.log("Replaced version in file: " + fullfilename); | |
} | |
}); | |
} |
This file contains hidden or 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
angular.module('equipmentShare').controller('AppController', function ($scope, BUILD) { | |
$scope.appVersion = BUILD.VERSION; | |
}); |
This file contains hidden or 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
/* | |
* Cordova build hook will replace this version with the actual version from config.xml. | |
*/ | |
angular.module('equipmentShare').constant('BUILD', { | |
VERSION: "%%VERSION%%" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there, I made a new functional hook script stored at
hooks/setVersion.js
. I just tested it now and it works (just in Android, for iOS you just need to replicate thewwwDir
)You have also to add in config.xml