Skip to content

Commit c80f89e

Browse files
committed
v3; defaults to pkgx^2; pure js rewrite
1 parent 6b6c1c2 commit c80f89e

File tree

19 files changed

+842
-419
lines changed

19 files changed

+842
-419
lines changed

.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 & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,3 @@ jobs:
99
uses: ./.github/workflows/ci.installer.yml
1010
qa-action:
1111
uses: ./.github/workflows/ci.action.yml
12-
13-
dist:
14-
needs: [qa-installer, qa-action]
15-
runs-on: ubuntu-latest
16-
steps:
17-
- uses: actions/checkout@v4
18-
- run: curl -Ssf https://pkgx.sh/$(uname)/$(uname -m).tgz | sudo tar xz -C /usr/local/bin
19-
- run: ./scripts/dist.sh --minify
20-
- uses: actions/upload-artifact@v3
21-
with:
22-
name: dist
23-
path: dist
24-
25-
put:
26-
permissions:
27-
contents: write
28-
deployments: write
29-
needs: dist
30-
runs-on: ubuntu-latest
31-
steps:
32-
- name: Create Deployment
33-
uses: bobheadxi/deployments@v1
34-
id: deployment
35-
with:
36-
step: start
37-
env: aws
38-
39-
- uses: actions/checkout@v4
40-
- uses: actions/download-artifact@v3
41-
with:
42-
name: dist
43-
path: dist
44-
45-
- uses: git-actions/set-user@v1
46-
47-
- run: |
48-
if ! git diff --exit-code; then
49-
git add dist
50-
git commit -m v${{ github.event.inputs.version }}
51-
git tag v${{ github.event.inputs.version }}
52-
git push origin v${{ github.event.inputs.version }} main
53-
fi
54-
55-
- uses: softprops/action-gh-release@v1
56-
with:
57-
files: ./installer.sh
58-
tag_name: v${{ github.event.inputs.version }}
59-
fail_on_unmatched_files: true
60-
61-
- uses: fischerscode/tagger@v0
62-
with:
63-
prefix: v
64-
tag: v${{ github.event.inputs.version }}
65-
66-
- run: |
67-
git tag --force latest
68-
git push origin latest
69-
70-
- name: Seal Deployment
71-
uses: bobheadxi/deployments@v1
72-
if: always()
73-
with:
74-
env: aws
75-
step: finish
76-
status: ${{ job.status }}
77-
deployment_id: ${{ steps.deployment.outputs.deployment_id }}

.github/workflows/ci.action.yml

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ on:
22
workflow_call:
33
pull_request:
44
paths:
5-
- action.ts
5+
- action.js
66
- action.yml
77
- package.json
88

@@ -11,30 +11,17 @@ concurrency:
1111
cancel-in-progress: true
1212

1313
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-
2414
std:
25-
needs: dist
2615
runs-on: ubuntu-latest
2716
steps:
2817
- uses: actions/checkout@v4
29-
- uses: actions/download-artifact@v3
30-
with:
31-
name: dist
32-
path: dist
18+
- run: npm i
3319
- uses: ./
20+
with:
21+
version: null
3422
- run: pkgx --version
3523

3624
plus-pkgs:
37-
needs: dist
3825
runs-on: ${{ matrix.os }}
3926
strategy:
4027
fail-fast: false
@@ -46,7 +33,7 @@ jobs:
4633
- [self-hosted, linux, ARM64] # works in arm64 environments
4734
prefix:
4835
- null
49-
- /opt
36+
- /tmp/pkgx
5037
container:
5138
- null
5239
include:
@@ -55,31 +42,29 @@ jobs:
5542
container: ${{ matrix.container }}
5643
steps:
5744
- uses: actions/checkout@v4
58-
- uses: actions/download-artifact@v3
45+
46+
- uses: actions/setup-node@v4
5947
with:
60-
name: dist
61-
path: dist
48+
node-version: latest
49+
- run: npm i
6250

6351
- uses: ./
6452
with:
6553
PKGX_DIR: ${{ matrix.prefix }}
6654
+: node@18 deno.land
6755

68-
- run: test -f '${{ matrix.prefix }}/pkgx.sh/v*/bin/pkgx'
56+
- run: test -f '${{ matrix.prefix }}/deno.land/v*/bin/deno'
6957
if: ${{ matrix.prefix }}
58+
7059
- run: pkgx --version
71-
- run: node --eval 'console.log(1)'
60+
- run: '[[ "$(node --version)" == v18.* ]]'
7261
- run: deno --version
7362

7463
multiple-apply-is-ok:
7564
runs-on: ubuntu-latest
76-
needs: dist
7765
steps:
7866
- uses: actions/checkout@v4
79-
- uses: actions/download-artifact@v3
80-
with:
81-
name: dist
82-
path: dist
67+
- run: npm i
8368
- uses: ./
8469
- run: pkgx --version
8570
- uses: ./

.github/workflows/ci.installer.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,7 @@ jobs:
196196
tar xz --strip-components=3
197197
- run: mv pkgx /usr/local/bin
198198
- run: test "$(pkgx --version)" = 'pkgx 1.0.0'
199+
- run: PKGX_UPDATE=no ./installer.sh
200+
- run: test "$(pkgx --version)" = 'pkgx 1.0.0'
199201
- run: ./installer.sh
200202
- 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_UPDATE: 'no'}});
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)