Skip to content

Commit 4f9341b

Browse files
committed
Install dev aware stubs
1 parent 4ece506 commit 4f9341b

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
#TODO test on linux! we are currently broken due to rpath issues
2626
# https://github.com/pkgxdev/pkgm/pull/30#issuecomment-2678957666
2727
test:
28+
continue-on-error: true
2829
strategy:
2930
matrix:
3031
os:
@@ -90,6 +91,18 @@ jobs:
9091
env:
9192
XDG_DATA_HOME: /tmp/foo
9293
94+
- run: |
95+
sudo ./pkgm.ts i node@22 dev
96+
[[ $(node --version) = v22* ]] || exit 2
97+
mkdir foo
98+
cd foo
99+
echo "dependencies: node@20" > pkgx.yaml
100+
mkdir -p /tmp/foo/pkgx/dev$PWD/
101+
touch /tmp/foo/pkgx/dev$PWD/dev.pkgx.activated # `dev .` doesn’t work in CI (fix in dev^2)
102+
[[ $(node --version) = v20* ]] || exit 3
103+
env:
104+
XDG_DATA_HOME: /tmp/foo
105+
93106
# https://github.com/pkgxdev/pkgm/issues/62
94107
- run: |
95108
./pkgm.ts i spotify_player

pkgm.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,18 @@ async function install(args: string[], basePath: string) {
136136
`${x.pkg.project}/v${x.pkg.version}`
137137
);
138138

139-
const pkgx_dir = Deno.env.get("PKGX_DIR") || `${Deno.env.get("HOME")}/.pkgx`;
139+
const pkgx_dir = (() => {
140+
let dir = Deno.env.get("PKGX_DIR");
141+
if (dir) return dir;
142+
const default_dir = Path.home().join(".pkgx");
143+
if (
144+
Deno.build.os == "linux" && !default_dir.isDirectory() &&
145+
(dir = Deno.env.get("XDG_DATA_HOME"))
146+
) {
147+
return `${dir}/pkgx`;
148+
}
149+
return default_dir.string;
150+
})();
140151

141152
const runtime_env = expand_runtime_env(json, basePath);
142153

@@ -178,10 +189,13 @@ async function install(args: string[], basePath: string) {
178189
for (const [key, value] of Object.entries(env)) {
179190
sh += `export ${key}="${value}"\n`;
180191
}
181-
sh += `exec "${bin_prefix}/${entry.name}" "$@"\n`;
192+
193+
sh += "\n";
194+
//TODO should be specific with the project
195+
sh += dev_stub_text(to_stub, bin_prefix, entry.name);
182196

183197
await Deno.remove(to_stub); //FIXME inefficient to symlink for no reason
184-
await Deno.writeTextFile(to_stub, sh);
198+
await Deno.writeTextFile(to_stub, sh.trim() + "\n");
185199
await Deno.chmod(to_stub, 0o755);
186200

187201
rv.push(to_stub);
@@ -316,6 +330,7 @@ async function query_pkgx(
316330

317331
const out = await proc.output();
318332
const json = JSON.parse(new TextDecoder().decode(out.stdout));
333+
319334
const pkgs =
320335
(json.pkgs as { path: string; project: string; version: string }[]).map(
321336
(x) => {
@@ -726,3 +741,34 @@ function install_prefix() {
726741
return Path.home().join(".local");
727742
}
728743
}
744+
745+
function dev_stub_text(selfpath: string, bin_prefix: string, name: string) {
746+
return `
747+
dev_check() {
748+
[ -x /usr/local/bin/dev ] || return 1
749+
local d="$PWD"
750+
until [ "$d" = / ]; do
751+
if [ -f "${datadir()}/pkgx/dev/$d/dev.pkgx.activated" ]; then
752+
echo $d
753+
return 0
754+
fi
755+
d="$(dirname "$d")"
756+
done
757+
return 1
758+
}
759+
760+
if d="$(dev_check)"; then
761+
eval "$(/usr/local/bin/dev "$d" 2>/dev/null)"
762+
[ "$(command -v ${name} 2>/dev/null)" != "${selfpath}" ] && exec ${name} "$@"
763+
fi
764+
765+
exec ${bin_prefix}/${name} "$@"
766+
`.trim();
767+
}
768+
769+
function datadir() {
770+
const default_data_home = Deno.build.os == "darwin"
771+
? "/Library/Application Support"
772+
: "/.local/share";
773+
return `\${XDG_DATA_HOME:-$HOME${default_data_home}}`;
774+
}

0 commit comments

Comments
 (0)