-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.ts
104 lines (93 loc) · 2.75 KB
/
action.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
95
96
97
98
99
100
101
102
103
104
import * as core from "@actions/core";
import {
parseCargoPackageManifestAsync,
Release,
Repo,
} from "action-get-release";
import * as path from "path";
async function getVersion(): Promise<string> {
let version = "latest";
const manifest = await parseCargoPackageManifestAsync(
path.join(__dirname, "../Cargo.toml"),
);
const manifestVersion = manifest.package?.version;
if (manifestVersion && manifestVersion !== "") {
version = `v${manifestVersion}`;
}
const versionOverride = core.getInput("version");
if (versionOverride && versionOverride !== "") {
version = versionOverride;
}
return version;
}
async function download(release: Release, asset: string) {
let downloaded: string;
try {
downloaded = await release.downloadAsset(asset, { cache: false });
} catch (err: unknown) {
throw new Error(`failed to download asset ${asset}: ${err}`);
}
core.addPath(downloaded);
}
function trimPrefix(toTrim: string, trim: string): string {
if (!toTrim || !trim) {
return toTrim;
}
const index = toTrim.indexOf(trim);
if (index !== 0) {
return toTrim;
}
return toTrim.substring(trim.length);
}
async function run(): Promise<void> {
const repo = new Repo();
const version = await getVersion();
core.debug(`version=${version}`);
let release: Release;
try {
release =
version === "" || version === "latest"
? await repo.getLatestRelease()
: await repo.getReleaseByTag(version);
} catch (err: unknown) {
throw new Error(
`failed to fetch ${version} release for ${repo.fullName()}: ${err}`,
);
}
core.debug(
`found ${release.assets().length
} assets for ${version} release of ${repo.fullName()}`,
);
let platform: "windows" | "darwin" | "linux";
if (process.platform === "linux") {
platform = "linux";
} else if (process.platform === "darwin") {
platform = "darwin";
} else if (process.platform === "win32") {
platform = "windows";
} else {
throw new Error(`platform ${process.platform} is not supported`);
}
let arch: "arm64" | "amd64";
if (process.arch === "arm64") {
arch = "arm64";
} else if (process.arch === "x64") {
arch = "amd64";
} else {
throw new Error(`arch ${process.arch} is not supported`);
}
const extension = platform === "windows" ? "zip" : "tar.gz";
await Promise.all([
// cargo-fc_0.0.30_linux_amd64.tar.gz
download(
release,
`cargo-feature-combinations_${trimPrefix(version, "v")}_${platform}_${arch}.${extension}`,
),
// cargo-feature-combinations_0.0.30_linux_amd64.tar.gz
download(
release,
`cargo-feature-combinations_${trimPrefix(version, "v")}_${platform}_${arch}.${extension}`,
),
]);
}
run().catch((error) => core.setFailed(error.message));