Skip to content

Commit

Permalink
feat(buildNumber): store or change build number in config.xml
Browse files Browse the repository at this point in the history
closes #18
  • Loading branch information
gligoran committed Apr 11, 2017
1 parent 630f318 commit eddec15
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

import setVersion from './set-version';
import setBuildNumber from './set-build-number';

export default {
setVersion
setVersion,
setBuildNumber
};
23 changes: 23 additions & 0 deletions src/set-build-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

import fs from 'mz/fs';
import xml2js from 'xml2js-es6-promise';
import { Builder } from 'xml2js';

const xmlBuilder = new Builder();

function setBuildNumber(configPath, buildNumber) {
return fs.readFile(configPath, { encoding: 'UTF-8' })
.then((xmlString) => xml2js(xmlString))
.then((xml) => writeBuildNumber(configPath, buildNumber, xml));
}

function writeBuildNumber(configPath, buildNumber, xml) {
xml.widget.$['android-versionCode'] = buildNumber;
xml.widget.$['ios-CFBundleVersion'] = buildNumber;
xml.widget.$['osx-CFBundleVersion'] = buildNumber;
let newConfigData = xmlBuilder.buildObject(xml);
return fs.writeFile(configPath, newConfigData, { encoding: 'UTF-8' });
}

export default setBuildNumber;
93 changes: 93 additions & 0 deletions test/set-build-number.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import fs from 'fs-promise';

import cordovaSetVersion from '../src/index';

const fileParams = { encoding: 'UTF-8' };
const configPaths = {
COPY: './config.xml',
GOOD: './set-build-number/configs/config.good.xml',
WITHOUT: './set-build-number/configs/config.good.xml',
MALFORMED: './set-build-number/configs/config.malformed.xml',
MISSING: './set-build-number/configs/config.missing.xml',
EXPECTED: './set-build-number/configs/config.expected.xml'
};

chai.use(chaiAsPromised);
chai.should();

describe('setBuildNumber', () => {
before(() => {
process.chdir(__dirname);
});

it('should exist', () => {
cordovaSetVersion.should.have.property('setBuildNumber');
});

it('should be a function', () => {
let type = typeof cordovaSetVersion.setBuildNumber;
type.should.equal('function');
});

it('should replace existing build number attribute values', () => {
fs.copySync(configPaths.GOOD, configPaths.COPY);

let promise = cordovaSetVersion.setBuildNumber(configPaths.COPY, 24)
.then(() => fs.readFile(configPaths.COPY, fileParams));

let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedConfig);
});
});


it('should create build number attributes, when missing', () => {
fs.copySync(configPaths.WITHOUT, configPaths.COPY);

let promise = cordovaSetVersion.setBuildNumber(configPaths.COPY, 24)
.then(() => fs.readFile(configPaths.COPY, fileParams));

let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams);

return promise.should.be.fulfilled
.then((result) => {
result.should.equal(expectedConfig);
});
});

it('should return an error about missing config.xml file', () => {
let promise = cordovaSetVersion.setBuildNumber(configPaths.MISSING, '1.0.0')
.then(() => fs.readFile(configPaths.COPY, fileParams));

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.should.have.property('message');
error.message.should.contain('no such file or directory');
error.message.should.contain(configPaths.MISSING);
});
});

it('should return an error about bad config.xml file', () => {
let promise = cordovaSetVersion.setVersion(configPaths.MALFORMED, '1.0.0')

return promise.should.be.rejected
.then((error) => {
error.should.be.instanceOf(Error);
error.message.should.not.contain('no such file or directory');
});
});

afterEach(function () {
if (fs.existsSync(configPaths.COPY)) {
fs.removeSync(configPaths.COPY);
}
});
});
39 changes: 39 additions & 0 deletions test/set-build-number/configs/config.expected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="0.0.0" android-versionCode="24" ios-CFBundleVersion="24" osx-CFBundleVersion="24" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">
<allow-intent href="market:*"/>
</platform>
<platform name="ios">
<allow-intent href="itms:*"/>
<allow-intent href="itms-apps:*"/>
</platform>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="FadeSplashScreenDuration" value="300"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
<plugin name="cordova-plugin-console" spec="1.0.5"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
<plugin name="cordova-plugin-device" spec="1.1.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
</widget>
39 changes: 39 additions & 0 deletions test/set-build-number/configs/config.good.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="0.0.0" android-versionCode="0" ios-CFBundleVersion="0" osx-CFBundleVersion="0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">
<allow-intent href="market:*"/>
</platform>
<platform name="ios">
<allow-intent href="itms:*"/>
<allow-intent href="itms-apps:*"/>
</platform>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="FadeSplashScreenDuration" value="300"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
<plugin name="cordova-plugin-console" spec="1.0.5"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
<plugin name="cordova-plugin-device" spec="1.1.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
</widget>
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions test/set-version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import cordovaSetVersion from '../src/index';
const fileParams = { encoding: 'UTF-8' };
const configPaths = {
COPY: './config.xml',
GOOD: './configs/config.good.xml',
MALFORMED: './configs/config.malformed.xml',
MISSING: './configs/config.missing.xml',
EXPECTED: './configs/config.expected.xml',
EXPECTED_JSON: './configs/config.expected-json.xml'
GOOD: './set-version/configs/config.good.xml',
MALFORMED: './set-version/configs/config.malformed.xml',
MISSING: './set-version/configs/config.missing.xml',
EXPECTED: './set-version/configs/config.expected.xml',
EXPECTED_JSON: './set-version/configs/config.expected-json.xml'
};
const packagePaths = {
COPY: './package.json',
GOOD: './packages/package.good.json',
MALFORMED: './packages/package.malformed.json',
NO_VERSION: './packages/package.no-version.json',
MISSING: './packages/package.missing.json'
GOOD: './set-version/packages/package.good.json',
MALFORMED: './set-version/packages/package.malformed.json',
NO_VERSION: './set-version/packages/package.no-version.json',
MISSING: './set-version/packages/package.missing.json'
};

chai.use(chaiAsPromised);
Expand Down
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions test/set-version/configs/config.good.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="0.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">
<allow-intent href="market:*"/>
</platform>
<platform name="ios">
<allow-intent href="itms:*"/>
<allow-intent href="itms-apps:*"/>
</platform>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="FadeSplashScreenDuration" value="300"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
<plugin name="cordova-plugin-console" spec="1.0.5"/>
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
<plugin name="cordova-plugin-device" spec="1.1.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
</widget>
16 changes: 16 additions & 0 deletions test/set-version/configs/config.malformed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.ionic2" version="0.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ionic2-rc6</name>
<description>An awesome Ionic/Cordova app.</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
<content src="index.html"/>
<access origin="*"/>
<allow-navigation href="http://ionic.local/*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
<platform name="android">

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit eddec15

Please sign in to comment.