This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from chkfung/android-version-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
// versionCode — A positive integer [...] -> https://developer.android.com/studio/publish/versioning
const versionCodeRegexPattern = /(versionCode(?:\s|=)*)(.*)/;
// versionName — A string used as the version number shown to users [...] -> https://developer.android.com/studio/publish/versioning
const versionNameRegexPattern = /(versionName(?:\s|=)*)(.*)/;
try {
const gradlePath = core.getInput('gradlePath');
const versionCode = core.getInput('versionCode');
const versionName = core.getInput('versionName');
const incrementVersionCode = core.getInput('incrementVersionCode');
console.log(`Gradle Path : ${gradlePath}`);
console.log(`Version Code : ${versionCode}`);
console.log(`Version Name : ${versionName}`);
console.log(`Increment version code : ${incrementVersionCode}`);
fs.readFile(gradlePath, 'utf8', function (err, data) {
newGradle = data;
if (incrementVersionCode) {
const lastVersionCodeStr = newGradle.match(versionCodeRegexPattern);
const newVersionCode = parseInt(lastVersionCodeStr[2]) + 1;
console.log(`lastVersionCode = ${lastVersionCodeStr}, newVersionCode = ${newVersionCode}`)
newGradle = newGradle.replace(versionCodeRegexPattern, `$1${newVersionCode}`);
}
if (versionCode.length > 0)
newGradle = newGradle.replace(versionCodeRegexPattern, `$1${versionCode}`);
if (versionName.length > 0)
newGradle = newGradle.replace(versionNameRegexPattern, `$1\"${versionName}\"`);
fs.writeFile(gradlePath, newGradle, function (err) {
if (err) throw err;
console.log(`Successfully override version code ${versionCode}`)
if (versionName.length > 0)
console.log(`Successfully override version code ${versionName}`)
core.setOutput("result", `Done`);
});
});
} catch (error) {
core.setFailed(error.message);
}