forked from chkfung/android-version-actions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
39 lines (35 loc) · 1.75 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
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');
console.log(`Gradle Path : ${gradlePath}`);
console.log(`Version Code : ${versionCode}`);
console.log(`Version Name : ${versionName}`);
fs.readFile(gradlePath, 'utf8', function (err, data) {
newGradle = data;
if (versionCode.length > 0)
newGradle = newGradle.replace(versionCodeRegexPattern, `$1${versionCode}`);
else
const lastVersionCodeStr = newGradle.match(versionCodeRegexPattern)[2];
const newVersionCode = parseInt(lastVersionCodeStr) + 1;
newGradle = newGradle.replace(versionCodeRegexPattern, `$1${newVersionCode}`);
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 name ${versionName}`)
core.setOutput("result", `Done`);
});
});
} catch (error) {
core.setFailed(error.message);
}