-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
82 lines (62 loc) · 2.16 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
"use strict";
import fs from "fs";
import path from "path";
import url from "url";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as tc from "@actions/tool-cache";
import toml from 'toml';
const COMMAND = path.basename(path.dirname(require.main!.filename));
const CARGO_TOML = fs.readFileSync(path.join(__dirname, "../Cargo.toml"));
const {
name: NAME,
repository: REPOSITORY,
version: VERSION,
} = toml.parse(CARGO_TOML.toString()).package;
const { RUNNER_TEMP } = process.env;
const { platform: PLATFORM } = process;
const BINARY_NAME = PLATFORM === 'win32' ? `${NAME}.exe` : NAME;
const ACTION_NAME = url.parse(REPOSITORY).pathname!.slice(1);
const BASE_URL = `${REPOSITORY}/releases/download/v${VERSION}`;
const FILE_PREFIX = `${NAME}-v${VERSION}`;
type OS = "linux" | "darwin" | "win32";
async function downloadTar(os: OS): Promise<string> {
const file = `${FILE_PREFIX}-${os}-x64.tar.gz`;
const url = `${BASE_URL}/${file}`;
const downloadPath = await tc.downloadTool(url);
const extractPath = await tc.extractTar(downloadPath, RUNNER_TEMP);
const extractedFile = path.join(extractPath, BINARY_NAME);
return tc.cacheFile(extractedFile, BINARY_NAME, ACTION_NAME, VERSION);
}
async function linux(): Promise<void> {
const cacheDir = tc.find(ACTION_NAME, "0.0.0")
|| await downloadTar("linux");
const binary = path.join(cacheDir, NAME);
await exec.exec(binary, [COMMAND]);
}
async function macos(): Promise<void> {
const cacheDir = tc.find(ACTION_NAME, "0.0.0")
|| await downloadTar("darwin");
const binary = path.join(cacheDir, NAME);
await exec.exec(binary, [COMMAND]);
}
async function windows(): Promise<void> {
const cacheDir = tc.find(ACTION_NAME, "0.0.0")
|| await downloadTar("win32");
const binary = path.join(cacheDir, NAME);
await exec.exec(binary, [COMMAND]);
}
async function run(): Promise<void> {
switch (PLATFORM) {
case "linux": return linux();
case "darwin": return macos();
case "win32": return windows();
case "aix":
case "freebsd":
case "openbsd":
case "sunos":
default:
throw new Error(`Unsupported platform: ${PLATFORM}`);
}
}
run().catch((error) => core.setFailed(error.message));