-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (89 loc) · 4.53 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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|=)*)(.*)/;
const groupVersionCodeValueIndex = 2
// versionName — A string used as the version number shown to users [...] -> https://developer.android.com/studio/publish/versioning
const versionNameRegexPattern = /(versionName(?:\s|=)*)(.*)/;
const groupVersionNameValueIndex = 2
// semver version regex — https://semver.org/ and added (\") as optional for Android version name string support
const semverVersionFromVersionNameRegexPattern = /^(\")?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?(\")?$/;
const groupMajorVersionIndex = 2
const groupMinorVersionIndex = 3
const groupPatchVersionIndex = 4
const groupPrereleaseBuildIndex = 5
const groupBuildMetaDataIndex = 6
// extract the build number from the prerelease version ({stage}.{buildNumber})
const prereleaseBuildNumberRegexPattern = /(.*)(\.)(\d+)$/
const groupPrereleaseIndex = 1
const groupBuildNumberIndex = 3
try {
const gradlePath = core.getInput('gradlePath');
console.log(`Gradle Path : ${gradlePath}`);
fs.readFile(gradlePath, 'utf8', function (err, data) {
if (err) {
core.setFailed(err.message);
return
}
newGradle = data;
const versionCode = newGradle.match(versionCodeRegexPattern)[groupVersionCodeValueIndex];
const versionName = newGradle.match(versionNameRegexPattern)[groupVersionNameValueIndex];
const semverVersionMatch = versionName.match(semverVersionFromVersionNameRegexPattern);
const majorVersion = semverVersionMatch[groupMajorVersionIndex];
const minorVersion = semverVersionMatch[groupMinorVersionIndex];
const patchVersion = semverVersionMatch[groupPatchVersionIndex];
const prereleaseVersion = semverVersionMatch[groupPrereleaseBuildIndex];
const buildMetaDataVersion = semverVersionMatch[groupBuildMetaDataIndex];
console.log(`versionName : ${versionName}`);
core.setOutput("versionName", `${versionName}`);
const cleanVersionName = versionName.replace(/['"]+/g, '')
console.log(`cleanVersionName : ${cleanVersionName}`);
core.setOutput("cleanVersionName", `${cleanVersionName}`);
console.log(`versionCode : ${versionCode}`);
core.setOutput("versionCode", `${versionCode}`);
core.setOutput("majorVersion", `${majorVersion}`);
core.setOutput("minorVersion", `${minorVersion}`);
core.setOutput("patchVersion", `${patchVersion}`);
if (prereleaseVersion)
core.setOutput("prereleaseVersion", `${prereleaseVersion}`);
if (buildMetaDataVersion)
core.setOutput("buildMetaDataVersion", `${buildMetaDataVersion}`);
if (prereleaseVersion && prereleaseVersion.length > 0) {
const prereleaseBuildNumberMatch = prereleaseVersion.match(prereleaseBuildNumberRegexPattern)
if (prereleaseBuildNumberMatch && prereleaseBuildNumberMatch.length >= groupPrereleaseIndex) {
const prereleaseStage = prereleaseBuildNumberMatch[groupPrereleaseIndex];
if (prereleaseStage) {
core.setOutput("prereleaseStage", `${prereleaseStage}`);
}
} else {
core.setOutput("prereleaseStage", `${prereleaseVersion}`);
}
if (prereleaseBuildNumberMatch && prereleaseBuildNumberMatch.length >= groupBuildNumberIndex) {
const prereleaseBuildNumber = prereleaseBuildNumberMatch[groupBuildNumberIndex];
if (prereleaseBuildNumber && prereleaseBuildNumber.length > 0) {
core.setOutput("prereleaseBuildNumber", `${prereleaseBuildNumber}`);
}
}
}
if (buildMetaDataVersion && buildMetaDataVersion.length > 0) {
const buildMetaDataBuildNumberMatch = buildMetaDataVersion.match(prereleaseBuildNumberRegexPattern)
if (buildMetaDataBuildNumberMatch && buildMetaDataBuildNumberMatch.length >= groupPrereleaseIndex) {
const buildMetaDataType = buildMetaDataBuildNumberMatch[groupPrereleaseIndex];
if (buildMetaDataType) {
core.setOutput("buildMetaDataType", `${buildMetaDataType}`);
}
} else {
core.setOutput("buildMetaDataType", `${buildMetaDataType}`);
}
if (buildMetaDataBuildNumberMatch && buildMetaDataBuildNumberMatch.length >= groupBuildNumberIndex) {
const buildMetaDataBuildNumber = buildMetaDataBuildNumberMatch[groupBuildNumberIndex];
if (buildMetaDataBuildNumber) {
core.setOutput("buildMetaDataBuildNumber", `${buildMetaDataBuildNumber}`);
}
}
}
});
} catch (error) {
core.setFailed(error.message);
}