-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-handling.js
68 lines (51 loc) · 1.71 KB
/
version-handling.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
const fs = require("fs");
const path = require("path");
const getNewVersion = (currentVersion, commit) => {
const [major, minor, patch] = currentVersion.split(".");
if (commit.includes("[PATCH]")) {
return `${major}.${minor}.${parseInt(patch) + 1}`;
} else if (commit.includes("[MINOR]")) {
return `${major}.${parseInt(minor) + 1}.${0}`;
} else if (commit.includes("[MAJOR]")) {
return `${parseInt(major) + 1}.${0}.${0}`;
} else {
return null;
}
};
module.exports = ({ github, context, core }) => {
const commit = context.payload.commits.map((x) => x.message).join(" = ");
console.log("Commit message:", commit);
const projectFilePath = path.join(
__dirname,
"./TextAloud.xcodeproj/project.pbxproj"
);
console.log(`Getting project file: ${projectFilePath}`);
const contents = fs
.readFileSync(projectFilePath, {
encoding: "utf8",
})
.toString();
const regex = new RegExp(/MARKETING_VERSION = (\d+\.\d+\.\d+);/, "g");
const results = contents.matchAll(regex);
let count = 0;
let currentVersion = null;
for (const result of results) {
if (count == 0) {
currentVersion = result[1];
} else {
if (result[1] !== currentVersion)
throw new Error("All versions didn't match");
}
count++;
}
console.log(`Changing ${count} version numbers`);
console.log("Current version is: ", currentVersion);
const newVersion = getNewVersion(currentVersion, commit);
if (newVersion === null) {
console.log("You didn't ask for a new version");
} else {
console.log("New version", newVersion);
const newContents = contents.replaceAll(currentVersion, newVersion);
fs.writeFileSync(projectFilePath, newContents);
}
};