Skip to content

Commit 8ef5cf9

Browse files
authored
Merge pull request #45 from pkgxdev/update
`pkgm update`
2 parents 13050ad + 1b2f67c commit 8ef5cf9

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ jobs:
6565
- run: ./pkgm.ts shim semverator
6666
- run: ~/.local/bin/semverator validate 1.0.0
6767

68+
- run: ./pkgm.ts i hyperfine@1.18
69+
- run: ./pkgm.ts outdated | grep hyperfine
70+
- run: ./pkgm.ts update
71+
6872
# TODO pending: https://github.com/pkgxdev/pantry/issues/8487
6973
# - run: ./pkgm.ts i xpra.org # https://github.com/pkgxdev/pkgm/issues/13
7074
# - run: ls -la /usr/local/pkgs/xpra.org/v6.2.3/venv/bin

pkgm.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
plumbing,
77
SemVer,
88
semver,
9+
utils,
910
} from "https://deno.land/x/libpkgx@v0.20.3/mod.ts";
1011
import { dirname, fromFileUrl, join } from "jsr:@std/path@^1";
1112
import { ensureDir, existsSync, walk } from "jsr:@std/fs@^1";
@@ -86,7 +87,13 @@ if (parsedArgs.help) {
8687
break;
8788
case "up":
8889
case "update":
90+
case "upgrade":
91+
await update();
92+
break;
93+
8994
case "pin":
95+
console.error("%cU EARLY! soz, not implemented", "color:red");
96+
Deno.exit(1);
9097
break;
9198
case "outdated":
9299
await outdated();
@@ -650,3 +657,66 @@ async function* walk_pkgs() {
650657
}
651658
}
652659
}
660+
661+
async function update() {
662+
const pkgs: Installation[] = [];
663+
for await (const pkg of walk_pkgs()) {
664+
pkgs.push(pkg);
665+
}
666+
667+
const { pkgs: raw_graph } = await hydrate(
668+
pkgs.map((x) => ({
669+
project: x.pkg.project,
670+
constraint: new semver.Range(`^${x.pkg.version}`),
671+
})),
672+
);
673+
const graph: Record<string, semver.Range> = {};
674+
for (const { project, constraint } of raw_graph) {
675+
graph[project] = constraint;
676+
}
677+
678+
const local_update_list = [];
679+
const system_update_list = [];
680+
681+
for (const { path, pkg } of pkgs) {
682+
const versions = await hooks.useInventory().get(pkg);
683+
// console.log(pkg, graph[pkg.project]);
684+
const constrained_versions = versions.filter((x) =>
685+
graph[pkg.project].satisfies(x) && x.gt(pkg.version)
686+
);
687+
688+
if (constrained_versions.length) {
689+
const pkgspec = `${pkg.project}=${constrained_versions.slice(-1)[0]}`;
690+
if (path.string.startsWith("/usr/local")) {
691+
system_update_list.push(pkgspec);
692+
} else {
693+
local_update_list.push(pkgspec);
694+
}
695+
}
696+
}
697+
698+
for (const pkgspec of local_update_list) {
699+
const pkg = utils.pkg.parse(pkgspec);
700+
console.log(
701+
"updating:",
702+
Path.home().join(".local/pkgs", pkg.project),
703+
"to",
704+
pkg.constraint.single(),
705+
);
706+
}
707+
for (const pkgspec of system_update_list) {
708+
const pkg = utils.pkg.parse(pkgspec);
709+
console.log(
710+
"updating:",
711+
new Path("/usr/local/pkgs").join(pkg.project),
712+
"to",
713+
pkg.constraint.single(),
714+
);
715+
}
716+
717+
if (local_update_list.length) {
718+
await install(local_update_list, Path.home().join(".local/bin").string);
719+
} else {
720+
await install(system_update_list, "/usr/local");
721+
}
722+
}

0 commit comments

Comments
 (0)