git - Expand variables on config.xml PhoneGap -


some plugins need se values api secret , more on config.xml file dont want commits on repo, instead want place them on different file , expand on config example

<widget ...> ...     <plugin name="cordova-fabric-plugin" spec="~1.0.8">         <variable name="fabric_api_key" value="blablabla" />         <variable name="fabric_api_secret" value="blablabla_secret" />     </plugin> </widget> 

how can

<widget ...> ...     <plugin name="cordova-fabric-plugin" spec="~1.0.8">         <variable name="fabric_api_key" value="${fabric_api_key}" />         <variable name="fabric_api_secret" value="${fabric_api_secret}" />     </plugin> </widget> 

it awesome if there such simple feature, alas option know @ point use hooks.

you can utilize before_build , after_build events edit config before compiling, , change once process complete.

this untested code, this:

/scripts/before_build/config_variables.js

#!/usr/bin/env node  var fs = require('fs'); var variables = require('./../../.variables');  module.exports = function(context) {   var configpath = 'config.xml';   var configbackuppath = 'config.xml.backup';    var config = fs.readfilesync(configpath, 'ascii');   fs.writefilesync(configbackuppath, config, 'ascii');    var tempconfig = config.replace(/\${(.*?)}/g, function (match, variablename) {     if (variables.hasownproperty(variablename)) {       return variables[variablename];     } else {       return match;     }   });    fs.writefilesync(configpath, tempconfig, 'ascii'); } 

/scripts/after_build/config_variables.js

#!/usr/bin/env node  var fs = require('fs');  module.exports = function(context) {   var configpath = 'config.xml';   var configbackuppath = 'config.xml.backup';    var realconfig = fs.readfilesync(configbackuppath, 'ascii');    fs.writefilesync(configpath, realconfig, 'ascii');   fs.unlink(configbackuppath); } 

/.variables.js

module.exports = {   fabric_api_key: 'blablabla',   fabric_api_secret: 'blablabla_secret', }; 

and finally, add these hooks config.xml file itself:

<hook type="before_build" src="scripts/before_build/config_variables.js" /> <hook type="after_build" src="scripts/after_build/config_variables.js" /> 

now when run $ phonegap build {platform} pull object export in .variables.js, overwrite config, build/compile, , replace edited config backup.

i went step further in own usage , used command line arguments pull in different variables multiple app flavors :)

one last tip, make sure hooks executable.


Comments