forked from ankeetmaini/calver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (98 loc) · 3.85 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
108
109
110
111
112
113
114
115
116
117
118
119
const core = require("@actions/core");
const fs = require("fs");
const exec = require("@actions/exec");
const github = require('@actions/github')
const versionCodeRegex = new RegExp(/versionCode\s*=\s*(\d*)/);
const versionNameRegex = new RegExp(/versionName\s*=\s*"([0-9|.|a-z]*)"/);
const calverRegex = new RegExp(/\d{2}.\d{2}.\d{2}.((\d).*)(-[a-z].*)?/);
const packageVersion = new RegExp(/"version"\s*:\s*"([^\s]*)"/);
const pad = (n) => {
n = n + "";
if (n.length === 2) return n;
if (n.length === 1) return "0" + n;
};
async function execCommand(command, options = {}) {
const projectPath = core.getInput('project-path')
options.cwd = projectPath
return exec.exec(command, [], options)
}
const isCalver = (version, isReleseBranch) => {
const date = new Date();
const newVersion = `${date.getFullYear() % 100}.${pad(
date.getMonth() + 1
)}.${pad(date.getDate())}`;
let fullVersion;
const result = calverRegex.exec(version);
if (!result) return newVersion + ".0";
const prev = result[0];
console.log(prev, newVersion, isReleseBranch);
if (isReleseBranch) {
fullVersion = `${prev.slice(0, 8)}.${Number(result[1]) + 1}`;
} else if (prev.slice(0, 8) === newVersion) {
fullVersion = `${newVersion}.${Number(result[1]) + 1}`;
} else {
fullVersion = `${newVersion}.0`;
}
return fullVersion;
};
// most @actions toolkit packages have async methods
async function run() {
try {
const filePath = core.getInput("path");
const platform = core.getInput("platform");
const branchName = github.context.ref
const isReleseBranch = branchName && branchName.startsWith("refs/heads/release-") ? true : false
if (!filePath && !platform) return;
const fileContents = fs.readFileSync(filePath).toString();
if (platform === "android") {
// eslint-disable-next-line no-unused-vars
const [_, versionCode] = versionCodeRegex.exec(fileContents);
const newVersion = Number(versionCode) + 1;
// eslint-disable-next-line no-unused-vars
const [__, versionName] = versionNameRegex.exec(fileContents);
const fullVersion = isCalver(versionName, isReleseBranch);
const versionUpdated = fileContents.replace(
versionCodeRegex,
(main, old) => main.replace(old, newVersion)
);
console.log('updated version string')
const versionNameUpdated = versionUpdated.replace(
versionNameRegex,
(main, old) => main.replace(old, fullVersion)
);
fs.writeFileSync(filePath, versionNameUpdated);
} else if (platform === "web") {
const packageJson = JSON.parse(fileContents);
const fullVersion = isCalver(packageJson.version, isReleseBranch);
const newContent = fileContents.replace(packageVersion, (main, old) =>
main.replace(old, fullVersion)
);
fs.writeFileSync(filePath, newContent);
} else if (platform === "ios") {
const currentVersion = await execCommand('agvtool what-marketing-version -terse1').catch(error => {
core.setFailed(error.message)
})
const fullVersion = isCalver(currentVersion, isReleseBranch);
const [major,minor,patch,buildVersion] = fullVersion.split('.');
console.log(buildVersion)
var combinedVersion = major + '.' + minor + '.' + patch;
const updatedVersion = `xcrun agvtool next-version -all`
await execCommand(updatedVersion).catch(error => {
core.setFailed(error.message)
})
if(!isReleseBranch) {
const newMarketingVersion = `xcrun agvtool new-marketing-version ${combinedVersion}`
await execCommand(newMarketingVersion).catch(error => {
core.setFailed(error.message)
})
}
}
else {
core.setFailed("Only `android` and `web` supported right now.");
}
} catch (error) {
core.setFailed(error.message);
}
}
run();
module.exports = run;