|
| 1 | +const { execSync } = require('child_process'); |
| 2 | +const semver = require('semver'); |
| 3 | +const https = require('https'); |
| 4 | +const path = require('path'); |
| 5 | +const tar = require('tar'); |
| 6 | +const fs = require('fs'); |
| 7 | +const os = require('os'); |
| 8 | + |
| 9 | +const dstdir = (() => { |
| 10 | + try { |
| 11 | + fs.accessSync('/usr/local/bin', fs.constants.W_OK); |
| 12 | + return '/usr/local/bin'; |
| 13 | + } catch (err) { |
| 14 | + return path.join(process.env.INPUT_PKGX_DIR || path.join(process.env.HOME, '.pkgx'), 'bin'); |
| 15 | + } |
| 16 | +})(); |
| 17 | + |
| 18 | +fs.writeFileSync(process.env["GITHUB_PATH"], `${dstdir}\n`); |
| 19 | + |
| 20 | +function platform_key() { |
| 21 | + const platform = os.platform(); // 'darwin', 'linux', 'win32', etc. |
| 22 | + let arch = os.arch(); // 'x64', 'arm64', etc. |
| 23 | + if (arch == 'x64') arch = 'x86-64'; |
| 24 | + if (arch == 'arm64') arch = 'aarch64'; |
| 25 | + return `${platform}/${arch}`; |
| 26 | +} |
| 27 | + |
| 28 | +function downloadAndExtract(url, destination) { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + https.get(url, (response) => { |
| 31 | + if (response.statusCode !== 200) { |
| 32 | + reject(new Error(`Failed to get '${url}' (${response.statusCode})`)); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + console.log(`extracting tarball…`); |
| 37 | + |
| 38 | + const tar_stream = tar.x({ cwd: destination, strip: 3 }); |
| 39 | + |
| 40 | + response |
| 41 | + .pipe(tar_stream) // Extract directly to destination |
| 42 | + .on('finish', resolve) |
| 43 | + .on('error', reject); |
| 44 | + |
| 45 | + tar_stream.on('error', reject); |
| 46 | + |
| 47 | + }).on('error', reject); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function parse_pkgx_output(output) { |
| 52 | + |
| 53 | + const stripQuotes = (str) => |
| 54 | + str.startsWith('"') || str.startsWith("'") ? str.slice(1, -1) : str; |
| 55 | + |
| 56 | + const replaceEnvVars = (str) => { |
| 57 | + const value = str |
| 58 | + .replaceAll( |
| 59 | + /\$\{([a-zA-Z0-9_]+):\+:\$[a-zA-Z0-9_]+\}/g, |
| 60 | + (_, key) => ((v) => v ? `:${v}` : "")(process.env[key]), |
| 61 | + ) |
| 62 | + .replaceAll(/\$\{([a-zA-Z0-9_]+)\}/g, (_, key) => process.env[key] ?? "") |
| 63 | + .replaceAll(/\$([a-zA-Z0-9_]+)/g, (_, key) => process.env[key] ?? ""); |
| 64 | + return value; |
| 65 | + }; |
| 66 | + |
| 67 | + for (const line of output.split("\n")) { |
| 68 | + const match = line.match(/^([^=]+)=(.*)$/); |
| 69 | + if (match) { |
| 70 | + const [_, key, value_] = match; |
| 71 | + const value = stripQuotes(value_); |
| 72 | + if (key === "PATH") { |
| 73 | + value |
| 74 | + .replaceAll("${PATH:+:$PATH}", "") |
| 75 | + .replaceAll("$PATH", "") |
| 76 | + .replaceAll("${PATH}", "") |
| 77 | + .split(":").forEach((path) => { |
| 78 | + fs.appendFileSync(process.env["GITHUB_PATH"], `${path}\n`); |
| 79 | + }); |
| 80 | + } else { |
| 81 | + let v = replaceEnvVars(value); |
| 82 | + fs.appendFileSync(process.env["GITHUB_ENV"], `${key}=${v}\n`); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function install_pkgx() { |
| 89 | + let url = `https://dist.pkgx.dev/pkgx.sh/${platform_key()}/versions.txt`; |
| 90 | + |
| 91 | + console.log(`::group::installing ${dstdir}/pkgx`); |
| 92 | + console.log(`fetching ${url}`); |
| 93 | + |
| 94 | + const rsp = await fetch(url); |
| 95 | + const txt = await rsp.text(); |
| 96 | + |
| 97 | + const versions = txt.split('\n'); |
| 98 | + const version = process.env.INPUT_VERSION |
| 99 | + ? semver.maxSatisfying(versions, process.env.INPUT_VERSION) |
| 100 | + : versions.slice(-1)[0]; |
| 101 | + |
| 102 | + if (!version) { |
| 103 | + throw new Error(`no version found for ${process.env.INPUT_VERSION}`); |
| 104 | + } |
| 105 | + |
| 106 | + console.log(`selected pkgx v${version}`); |
| 107 | + |
| 108 | + url = `https://dist.pkgx.dev/pkgx.sh/${platform_key()}/v${version}.tar.gz`; |
| 109 | + |
| 110 | + console.log(`fetching ${url}`); |
| 111 | + |
| 112 | + if (!fs.existsSync(dstdir)) { |
| 113 | + fs.mkdirSync(dstdir, {recursive: true}); |
| 114 | + } |
| 115 | + |
| 116 | + await downloadAndExtract(url, dstdir); |
| 117 | + |
| 118 | + console.log(`::endgroup::`); |
| 119 | +} |
| 120 | + |
| 121 | +(async () => { |
| 122 | + await install_pkgx(); |
| 123 | + |
| 124 | + if (process.env.INPUT_PKGX_DIR) { |
| 125 | + fs.appendFileSync(process.env["GITHUB_ENV"], `PKGX_DIR=${process.env.INPUT_PKGX_DIR}\n`); |
| 126 | + } |
| 127 | + |
| 128 | + if (os.platform() != 'darwin') { |
| 129 | + console.log(`::group::installing pre-requisites`); |
| 130 | + const installer_script_path = path.join(path.dirname(__filename), "installer.sh"); |
| 131 | + execSync(installer_script_path, {env: {PKGX_INSTALL_PREREQS: '1', ...process.env}}); |
| 132 | + console.log(`::endgroup::`); |
| 133 | + } |
| 134 | + |
| 135 | + if (process.env['INPUT_+']) { |
| 136 | + console.log(`::group::installing pkgx input packages`); |
| 137 | + const args = process.env['INPUT_+'].split(' '); |
| 138 | + const cmd = `${dstdir}/pkgx ${args.map(x => `+${x}`).join(' ')}`; |
| 139 | + console.log(`running: \`${cmd}\``); |
| 140 | + let env = undefined; |
| 141 | + if (process.env.INPUT_PKGX_DIR) { |
| 142 | + env = process.env |
| 143 | + env['PKGX_DIR'] = process.env.INPUT_PKGX_DIR; |
| 144 | + } |
| 145 | + const output = execSync(cmd, {env}); |
| 146 | + parse_pkgx_output(output.toString()); |
| 147 | + console.log(`::endgroup::`); |
| 148 | + } |
| 149 | +})().catch(err => { |
| 150 | + console.error(`::error::${err.message}`) |
| 151 | + process.exit(1); |
| 152 | +}); |
0 commit comments