fikkatra
fikkatra

Reputation: 5822

Ionic/Cordova: programmatically set app version

The version number of a Cordova app can be set in the <widget> element of config.xml, but is there a way to set it programmatically?

    <?xml version="1.0" encoding="utf-8"?>
    <widget defaultLocale="en-US" id="com.example.myapp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
      <name>MyApp</name>
...

I would like to set the version number during the build, using the build number as the version number. It would be nice if we could change config.xml programmatically at build time, e.g. by running a Cordova cli command.

Upvotes: 3

Views: 787

Answers (1)

fikkatra
fikkatra

Reputation: 5822

If you're using Gulp, it can be done by using a plugin like gulp-xmlpoke.

You can pass the build version to gulp via commandline arguments (e.g. gulp --buildVersion=1.2.3) and extract them with yargs.

Example gulp task:

var gulp = require("gulp");
var argv = require("yargs").argv;    
var xmlpoke = require("gulp-xmlpoke");

gulp.task("setAppVersion", function () {
        return gulp.src("config.xml")
            .pipe(xmlpoke({
                replacements: [{
                    xpath: "/w:widget/@version",
                    value: argv.buildVersion,
                    namespaces: { "w": "http://www.w3.org/ns/widgets", "cdv": "http://cordova.apache.org/ns/1.0" }
                }]
            }))
            .pipe(gulp.dest("./"));
    });

Upvotes: 2

Related Questions