Skip to content

Commit

Permalink
pkgx uninstall
Browse files Browse the repository at this point in the history
Fixes #752; Refs #788
  • Loading branch information
mxcl committed Oct 8, 2023
1 parent 8324722 commit 1d9024c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/modes/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ export default async function(pkgs: PackageRequirement[]) {

async function write(dst: Path, pkgs: PackageRequirement[]) {
for (const pkg of pkgs) {
program_loop:
for (const program of await usePantry().project(pkg).provides()) {

// skip for now since we would require specific versions and we haven't really got that
if (program.includes("{{")) continue

const pkgstr = utils.pkg.str(pkg)
const exec = `exec pkgx +${pkgstr} -- ${program} "$@"`
const script = undent`
if [ "$PKGX_UNINSTALL" != 1 ]; then
${exec}
else
rm "$0" && echo "uninstalled: $0" >&2
fi`
const f = dst.mkdir('p').join(program)

if (f.exists()) {
Expand All @@ -44,21 +51,23 @@ export default async function(pkgs: PackageRequirement[]) {
if (shebang != "#!/bin/sh") {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
const { value } = await lines.next()
if (value == exec) {
console.warn(`pkgx: already installed: ${blurple(program)} ${dim(`(${pkgstr})`)}`)
n++
continue
}
if (!value.startsWith('exec pkgx')) {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
while (true) {
const { value, done } = await lines.next()
if (value == exec) {
console.warn(`pkgx: already installed: ${blurple(program)} ${dim(`(${pkgstr})`)}`)
n++
continue program_loop
}
if (done) {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
}
}

f.write({ force: true, text: undent`
#!/bin/sh
${exec}
` }).chmod(0o755)
${script}`
}).chmod(0o755)
console.error('pkgx: installed:', f)
n++
}
Expand Down
13 changes: 13 additions & 0 deletions src/modes/uninstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import parse_pkg_str from "../prefab/parse-pkg-str.ts"

export default async function(pkgspecs: string[]) {
const pkgs = await Promise.all(pkgspecs.map(x => parse_pkg_str(x, {latest: 'ok'})))

for (const pkg of pkgs) {
if (pkg.project == 'pkgx.sh') {
uninstall_self()
} else {

}
}
}
3 changes: 2 additions & 1 deletion src/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type Args = {
mode: 'internal.activate'
dir: Path
} | {
mode: 'install'
mode: 'install' | 'uninstall'
args: string[]
}
)
Expand All @@ -60,6 +60,7 @@ export default function(input: string[]): Args {
switch (input[0]) {
case 'deintegrate':
case 'integrate':
case 'uninstall':
case 'install':
case 'run':
mode = input[0]
Expand Down

0 comments on commit 1d9024c

Please sign in to comment.