Skip to content

Commit b6406ab

Browse files
committed
v3; defaults to pkgx^2; pure js rewrite (#256)
1 parent 6b6c1c2 commit b6406ab

File tree

20 files changed

+857
-440
lines changed

20 files changed

+857
-440
lines changed

.github/workflows/cd.qa.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
on:
2+
workflow_dispatch:
3+
inputs:
4+
version:
5+
required: true
6+
7+
jobs:
8+
qa-installer:
9+
uses: ./.github/workflows/ci.installer.yml
10+
qa-action:
11+
uses: ./.github/workflows/ci.action.yml

.github/workflows/cd.vx.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: cd·vx
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
concurrency:
9+
group: cd/vx/${{ github.event.release.tag_name }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: write
14+
15+
jobs:
16+
retag:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: fischerscode/tagger@v0
21+
with:
22+
prefix: v
23+
- run: |
24+
git tag -f latest
25+
git push origin latest --force

.github/workflows/cd.yml

Lines changed: 0 additions & 77 deletions
This file was deleted.

.github/workflows/ci.action.yml

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,27 @@ on:
22
workflow_call:
33
pull_request:
44
paths:
5-
- action.ts
5+
- action.js
66
- action.yml
77
- package.json
8+
- installer.sh
89

910
concurrency:
1011
group: ${{ github.ref || 'ci' }}/action.ts
1112
cancel-in-progress: true
1213

1314
jobs:
14-
dist:
15-
runs-on: ubuntu-latest
16-
steps:
17-
- uses: actions/checkout@v4
18-
- run: ./scripts/dist.sh
19-
- uses: actions/upload-artifact@v3
20-
with:
21-
name: dist
22-
path: dist
23-
2415
std:
25-
needs: dist
2616
runs-on: ubuntu-latest
2717
steps:
2818
- uses: actions/checkout@v4
29-
- uses: actions/download-artifact@v3
30-
with:
31-
name: dist
32-
path: dist
19+
- run: npm i
3320
- uses: ./
21+
with:
22+
version: null
3423
- run: pkgx --version
3524

3625
plus-pkgs:
37-
needs: dist
3826
runs-on: ${{ matrix.os }}
3927
strategy:
4028
fail-fast: false
@@ -46,7 +34,7 @@ jobs:
4634
- [self-hosted, linux, ARM64] # works in arm64 environments
4735
prefix:
4836
- null
49-
- /opt
37+
- /tmp/pkgx
5038
container:
5139
- null
5240
include:
@@ -55,31 +43,29 @@ jobs:
5543
container: ${{ matrix.container }}
5644
steps:
5745
- uses: actions/checkout@v4
58-
- uses: actions/download-artifact@v3
46+
47+
- uses: actions/setup-node@v4
5948
with:
60-
name: dist
61-
path: dist
49+
node-version: latest
50+
- run: npm i
6251

6352
- uses: ./
6453
with:
6554
PKGX_DIR: ${{ matrix.prefix }}
6655
+: node@18 deno.land
6756

68-
- run: test -f '${{ matrix.prefix }}/pkgx.sh/v*/bin/pkgx'
57+
- run: test -f '${{ matrix.prefix }}/deno.land/v*/bin/deno'
6958
if: ${{ matrix.prefix }}
59+
7060
- run: pkgx --version
71-
- run: node --eval 'console.log(1)'
61+
- run: if [[ "$(node --version)" != v18.* ]]; then exit 1; fi
7262
- run: deno --version
7363

7464
multiple-apply-is-ok:
7565
runs-on: ubuntu-latest
76-
needs: dist
7766
steps:
7867
- uses: actions/checkout@v4
79-
- uses: actions/download-artifact@v3
80-
with:
81-
name: dist
82-
path: dist
68+
- run: npm i
8369
- uses: ./
8470
- run: pkgx --version
8571
- uses: ./

.github/workflows/ci.installer.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,11 @@ jobs:
156156
- debian:buster-slim
157157
- debian:bullseye-slim
158158
- debian:bookworm-slim
159-
160159
- archlinux:latest
161-
162-
# - ubuntu:trusty glibc too old
163-
# - ubuntu:xenial glibc too old
164-
# - ubuntu:bionic glibc too old
165-
# - ubuntu:lunar out of support
166160
- ubuntu:focal
167161
- ubuntu:jammy
168-
- ubuntu:24.04
169-
- ubuntu:24.10
170-
162+
- ubuntu:noble
171163
- fedora:latest
172-
173-
# - nixos/nix:latest can’t exec
174-
# - alpine:latest pkgx binary doesn’t actually end up on the system (wtf)
175164
container: ${{ matrix.container }}
176165
steps:
177166
- uses: actions/checkout@v4
@@ -196,5 +185,7 @@ jobs:
196185
tar xz --strip-components=3
197186
- run: mv pkgx /usr/local/bin
198187
- run: test "$(pkgx --version)" = 'pkgx 1.0.0'
188+
- run: PKGX_UPDATE=no ./installer.sh
189+
- run: test "$(pkgx --version)" = 'pkgx 1.0.0'
199190
- run: ./installer.sh
200191
- run: pkgx semverator gt $(pkgx --version | awk '{print $2}') 1.0.0

action.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)