-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
94 lines (79 loc) · 3 KB
/
index.ts
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
import * as core from "@actions/core";
import * as github from "@actions/github";
import { patchFile, VersionType } from "./patchers";
import { ReleaseEvent } from "@octokit/webhooks-types/schema";
import * as glob from "@actions/glob";
const names = {
[VersionType.CSProject]: ".csproj",
[VersionType.NPM]: "package.json for npm",
[VersionType.SetupPython]: "setuptools setup.py",
[VersionType.InitPython]: "__init__.py for Python Package",
[VersionType.CFXManifest]: "fxmanifest.lua for cfx.re",
[VersionType.Gemspec]: "Bundler gemspec",
[VersionType.PyProject]: "pyproject.toml"
};
function toBoolean(input: string) {
return input.toLowerCase().trim() === "true";
}
async function run() {
try
{
let version = core.getInput("version").trim();
const useTag = toBoolean(core.getInput("use-tag"));
if (useTag)
{
const context = github.context;
if (context.eventName === "release")
{
const payload = context.payload as ReleaseEvent;
version = payload.release.tag_name;
console.log("Using version from Release Tag " + version);
}
}
if (!version)
{
core.error("No version was specified to patch!");
return;
}
if (toBoolean(core.getInput("trim")) && (version.indexOf("v") === 0 || version.indexOf("V") === 0))
{
version = version.substring(1);
console.log("Trimmed v from the beginning");
}
console.log(`Using Version ${version}`);
core.setOutput("version", version);
const patches: Record<VersionType, string> = {
[VersionType.CSProject]: core.getInput("csproj-files"),
[VersionType.NPM]: core.getInput("npm-files"),
[VersionType.SetupPython]: core.getInput("setuppy-files"),
[VersionType.InitPython]: core.getInput("initpy-files"),
[VersionType.CFXManifest]: core.getInput("fxmanifest-files"),
[VersionType.Gemspec]: core.getInput("gemspec-files"),
[VersionType.PyProject]: core.getInput("pyproject-files")
};
for (const [versionType, glob_str] of (Object.entries(patches) as unknown as ([VersionType, string])[])) {
const name = names[versionType];
if (glob_str.length == 0) {
console.log(`Skipping ${name} as no glob was specified`);
continue;
}
const files = await (await glob.create(glob_str)).glob();
if (files.length == 0) {
core.setFailed(`No files found matching glob ${glob_str} for format ${name}`);
}
for (const file of files)
{
console.log(`Patching ${file} as ${name}`);
await patchFile(file, version, versionType);
}
}
}
catch (e)
{
if (e instanceof Error)
{
core.setFailed(e.message);
}
}
}
run();