-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathaction.js
152 lines (124 loc) · 4.36 KB
/
action.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { execSync } = require('child_process');
const semver = require('semver');
const https = require('https');
const path = require('path');
const tar = require('tar');
const fs = require('fs');
const os = require('os');
const dstdir = (() => {
try {
fs.accessSync('/usr/local/bin', fs.constants.W_OK);
return '/usr/local/bin';
} catch (err) {
return path.join(process.env.INPUT_PKGX_DIR || path.join(process.env.HOME, '.pkgx'), 'bin');
}
})();
fs.writeFileSync(process.env["GITHUB_PATH"], `${dstdir}\n`);
function platform_key() {
const platform = os.platform(); // 'darwin', 'linux', 'win32', etc.
let arch = os.arch(); // 'x64', 'arm64', etc.
if (arch == 'x64') arch = 'x86-64';
if (arch == 'arm64') arch = 'aarch64';
return `${platform}/${arch}`;
}
function downloadAndExtract(url, destination) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
console.log(`extracting tarball…`);
const tar_stream = tar.x({ cwd: destination, strip: 3 });
response
.pipe(tar_stream) // Extract directly to destination
.on('finish', resolve)
.on('error', reject);
tar_stream.on('error', reject);
}).on('error', reject);
});
}
function parse_pkgx_output(output) {
const stripQuotes = (str) =>
str.startsWith('"') || str.startsWith("'") ? str.slice(1, -1) : str;
const replaceEnvVars = (str) => {
const value = str
.replaceAll(
/\$\{([a-zA-Z0-9_]+):\+:\$[a-zA-Z0-9_]+\}/g,
(_, key) => ((v) => v ? `:${v}` : "")(process.env[key]),
)
.replaceAll(/\$\{([a-zA-Z0-9_]+)\}/g, (_, key) => process.env[key] ?? "")
.replaceAll(/\$([a-zA-Z0-9_]+)/g, (_, key) => process.env[key] ?? "");
return value;
};
for (const line of output.split("\n")) {
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
const [_, key, value_] = match;
const value = stripQuotes(value_);
if (key === "PATH") {
value
.replaceAll("${PATH:+:$PATH}", "")
.replaceAll("$PATH", "")
.replaceAll("${PATH}", "")
.split(":").forEach((path) => {
fs.appendFileSync(process.env["GITHUB_PATH"], `${path}\n`);
});
} else {
let v = replaceEnvVars(value);
fs.appendFileSync(process.env["GITHUB_ENV"], `${key}=${v}\n`);
}
}
}
}
async function install_pkgx() {
let url = `https://dist.pkgx.dev/pkgx.sh/${platform_key()}/versions.txt`;
console.log(`::group::installing ${dstdir}/pkgx`);
console.log(`fetching ${url}`);
const rsp = await fetch(url);
const txt = await rsp.text();
const versions = txt.split('\n');
const version = process.env.INPUT_VERSION
? semver.maxSatisfying(versions, process.env.INPUT_VERSION)
: versions.slice(-1)[0];
if (!version) {
throw new Error(`no version found for ${process.env.INPUT_VERSION}`);
}
console.log(`selected pkgx v${version}`);
url = `https://dist.pkgx.dev/pkgx.sh/${platform_key()}/v${version}.tar.gz`;
console.log(`fetching ${url}`);
if (!fs.existsSync(dstdir)) {
fs.mkdirSync(dstdir, {recursive: true});
}
await downloadAndExtract(url, dstdir);
console.log(`::endgroup::`);
}
(async () => {
await install_pkgx();
if (process.env.INPUT_PKGX_DIR) {
fs.appendFileSync(process.env["GITHUB_ENV"], `PKGX_DIR=${process.env.INPUT_PKGX_DIR}\n`);
}
if (os.platform() != 'darwin') {
console.log(`::group::installing pre-requisites`);
const installer_script_path = path.join(path.dirname(__filename), "installer.sh");
execSync(installer_script_path, {env: {PKGX_INSTALL_PREREQS: '1', ...process.env}});
console.log(`::endgroup::`);
}
if (process.env['INPUT_+']) {
console.log(`::group::installing pkgx input packages`);
const args = process.env['INPUT_+'].split(' ');
const cmd = `${dstdir}/pkgx ${args.map(x => `+${x}`).join(' ')}`;
console.log(`running: \`${cmd}\``);
let env = undefined;
if (process.env.INPUT_PKGX_DIR) {
env = process.env
env['PKGX_DIR'] = process.env.INPUT_PKGX_DIR;
}
const output = execSync(cmd, {env});
parse_pkgx_output(output.toString());
console.log(`::endgroup::`);
}
})().catch(err => {
console.error(`::error::${err.message}`)
process.exit(1);
});