Skip to content

Commit

Permalink
Use gradle build instead of android manifest (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssideleau authored Sep 18, 2019
1 parent 42f6539 commit 0bdaf44
Show file tree
Hide file tree
Showing 4 changed files with 1,059 additions and 1,114 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ The **version** attribute in `package.json` will be updated with the specified v

It will update the **version name** and the **version code** in both `build.gradle` and `AndroidManifest.xml`.

#### About AndroidManifest.xml

Version information should not be in the `AndroidManifest.xml` since this information is overridden by `build.gradle`.

See https://developer.android.com/studio/publish/versioning for further informations.

For that reason `react-native-set-version` will only write in the `AndroidManifest.xml` if `android:versionCode` and/or `android:versionName` are already in the file.

### Update iOS Project Version

It will update the **CFBundleShortVersionString** and the **CFBundleVersion** in `Info.plist`.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"chalk": "^2.4.2",
"gradle-to-js": "^2.0.0",
"manifest-android": "^0.1.1",
"plist": "^3.0.1"
},
Expand Down
36 changes: 13 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import AndroidManifest from 'manifest-android';
import chalk from 'chalk';
import fs from 'fs';
import g2js from 'gradle-to-js/lib/parser';
import path from 'path';
import plist from 'plist';

Expand All @@ -11,23 +11,12 @@ import { versionStringToVersion, versionToVersionCode } from './versionUtils';
const display = console.log; // eslint-disable-line no-console

const paths = {
packageJson: './package.json',
buildGradle: './android/app/build.gradle',
androidManifest: './android/app/src/main/AndroidManifest.xml',
buildGradle: './android/app/build.gradle',
infoPlist: './ios/<APP_NAME>/Info.plist',
packageJson: './package.json',
};

const loadManifest = (manifest, filePath) => (
new Promise((resolve, reject) => {
manifest.load(filePath, (err, man) => {
if (err) {
return reject(err);
}
return resolve(man);
});
})
);

function setPackageVersion(versionText) {
let packageJSON = null;
try {
Expand Down Expand Up @@ -102,9 +91,9 @@ async function getAndroidVersionInfo(versionText) {
versionCode: null,
};
try {
const manifest = await loadManifest(new AndroidManifest(), { file: paths.androidManifest });
const currentVersion = versionStringToVersion(manifest._xml.attributes['android:versionName']); // eslint-disable-line no-underscore-dangle
const currentVersionCode = +(manifest._xml.attributes['android:versionCode']); // eslint-disable-line no-underscore-dangle
const gradle = await g2js.parseFile(paths.buildGradle);
const currentVersion = gradle.android.defaultConfig.versionName;
const currentVersionCode = +(gradle.android.defaultConfig.versionCode);
const version = versionStringToVersion(versionText, currentVersion, currentVersionCode);
versionInfo = {
currentVersionCode,
Expand All @@ -113,7 +102,7 @@ async function getAndroidVersionInfo(versionText) {
versionCode: versionToVersionCode(version),
};
} catch (err) {
display(chalk.yellowBright(`${chalk.bold.underline('WARNING:')} Cannot find attribute android:versionCode in file ${path.resolve(paths.buildGradle)}. Android version configuration will be skipped`));
display(chalk.yellowBright(`${chalk.bold.underline('WARNING:')} Cannot find attribute versionCode in file ${path.resolve(paths.buildGradle)}. Android version configuration will be skipped`));
}
return versionInfo;
}
Expand Down Expand Up @@ -143,12 +132,13 @@ async function setAndroidApplicationVersion(versionText) {

try {
const androidManifest = fs.readFileSync(paths.androidManifest, 'utf8');
if (androidManifest.includes('android:versionCode') || androidManifest.includes('android:versionName')) {
const newAndroidManifest = androidManifest.replace(/android:versionCode="\d*"/g, `android:versionCode="${versionCode}"`)
.replace(/android:versionName="[^"]*"/g, `android:versionName="${versionText}"`);

const newAndroidManifest = androidManifest.replace(/android:versionCode="\d*"/g, `android:versionCode="${versionCode}"`)
.replace(/android:versionName="[^"]*"/g, `android:versionName="${versionText}"`);

fs.writeFileSync(paths.androidManifest, newAndroidManifest, 'utf8');
display(chalk.green(`Version replaced in ${chalk.bold('AndroidManifest.xml')}`));
fs.writeFileSync(paths.androidManifest, newAndroidManifest, 'utf8');
display(chalk.green(`Version replaced in ${chalk.bold('AndroidManifest.xml')}`));
}
} catch (err) {
display(chalk.yellowBright(`${chalk.bold.underline('WARNING:')} Cannot find file with name ${path.resolve(paths.androidManifest)}. This file will be skipped`));
}
Expand Down
Loading

0 comments on commit 0bdaf44

Please sign in to comment.