-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (56 loc) · 1.65 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
const {getInput, getBooleanInput, setFailed, info, notice, getMultilineInput} = require('@actions/core');
const {spawnSync} = require('node:child_process');
try {
const name = getInput('name', {required: true});
const binaryName = getInput('binary-name') || name;
const version = getInput('version');
const release = getBooleanInput('release');
const forceVersion = getBooleanInput('force-version');
const flags = getMultilineInput('flags');
const alreadyInstalled = getInstalledVersion(name, binaryName);
if (alreadyInstalled && (!forceVersion || (version && alreadyInstalled === version))) {
info(`Already installed ${name}@${alreadyInstalled}`);
return;
}
const args = ['install', name, ...flags];
if (version) {
args.push('--version', version);
}
if (!release) {
args.push('--debug');
}
if (alreadyInstalled) {
notice(`Upgrading ${name}@${alreadyInstalled} to ${version}`);
args.push('--force');
}
const res = spawnSync('cargo', args, {
env: process.env,
stdio: 'inherit',
});
if (res.status === 0) {
info(`Installed ${name}@${version}`);
} else {
setFailed(`Failed to install ${name}@${version}`);
}
} catch (e) {
setFailed(e);
}
function getInstalledVersion(pkg, binaryName) {
let app;
const args = ['--version'];
if (pkg.match(/^cargo-[\w-_]+$/)) {
app = 'cargo';
args.unshift(pkg.slice('cargo-'.length));
} else {
app = binaryName;
}
const res = spawnSync(app, args, {
encoding: 'utf8',
env: process.env,
stdio: ['ignore', 'pipe', 'ignore'],
});
if (res.status !== 0) {
return;
}
return res.stdout.match(/\d+\.\d+\.\d+(-\w+\.\d+)?/)?.[0];
}