Skip to content

Commit

Permalink
Merge 132605a into ba219da
Browse files Browse the repository at this point in the history
  • Loading branch information
rustdevbtw authored May 11, 2024
2 parents ba219da + 132605a commit 75b2825
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
34 changes: 32 additions & 2 deletions src/modes/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const { usePantry } = hooks

// * maybe impl `$XDG_BIN_HOME`

export function is_unsafe(): boolean {
// `--unsafe` takes precedence over the `$PKGX_UNSAFE_INSTALL` flag
const IS_UNSAFE = parseInt(Deno.env.get("PKGX_UNSAFE_INSTALL") || "0") ? true : (Deno.args.includes("--unsafe"));
return IS_UNSAFE;
}

export default async function(pkgs: PackageRequirement[]) {
const usrlocal = new Path("/usr/local/bin")
let n = 0
Expand All @@ -30,6 +36,7 @@ export default async function(pkgs: PackageRequirement[]) {
}

async function write(dst: Path, pkgs: PackageRequirement[]) {
const UNSAFE = is_unsafe();
for (const pkg of pkgs) {
const programs = await usePantry().project(pkg).provides()
program_loop:
Expand All @@ -39,7 +46,30 @@ export default async function(pkgs: PackageRequirement[]) {
if (program.includes("{{")) continue

const pkgstr = utils.pkg.str(pkg)
const exec = `exec pkgx +${pkgstr} -- ${program} "$@"`
if (UNSAFE) {
const parts = pkgstr.split("/")
parts.pop()
await Deno.mkdir(Path.home().join(`.cache/pkgx/envs/${parts.join("/")}`).toString(), {recursive: true})
}
//FIXME: doing `set -a` clears the args env
const exec = UNSAFE ? undent`
ARGS="$@"
pkgx_resolve() {
mkdir -p "$\{XDG_CACHE_DIR:-$HOME/.cache\}/pkgx/envs"
pkgx +${pkgstr} 1>"$\{XDG_CACHE_DIR:-$HOME/.cache\}/pkgx/envs/${pkgstr}.env"
run
}
run() {
if [[ -e "$\{XDG_CACHE_DIR:-$HOME/.cache\}/pkgx/envs/${pkgstr}.env" && -e "$\{PKGX_HOME:-$HOME/.pkgx\}/${pkgstr}/v*/bin/${program}" ]]; then
set -a
source "$\{XDG_CACHE_DIR:-$HOME/.cache\}/pkgx/envs/${pkgstr}.env"
exec "$\{PKGX_HOME:-$HOME/.pkgx\}/${pkgstr}/v*/bin/${program}" "$ARGS"
else
pkgx_resolve
fi
}
run
` : `exec pkgx +${pkgstr} -- ${program} "$@"`
const script = undent`
if [ "$PKGX_UNINSTALL" != 1 ]; then
${exec}
Expand All @@ -65,7 +95,7 @@ export default async function(pkgs: PackageRequirement[]) {
if (done) {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
const found = value.match(/^\s*exec pkgx \+([^ ]+)/)?.[1]
const found = value.match(/^\s*pkgx \+([^ ]+)/)?.[1]
if (found) {
n++
console.warn(`pkgx: already installed: ${blurple(program)} ${dim(`(${found})`)}`)
Expand Down
10 changes: 9 additions & 1 deletion src/modes/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse_pkg_str from "../prefab/parse-pkg-str.ts"
import { hooks, PackageRequirement, Path, PkgxError } from "pkgx"
import { hooks, PackageRequirement, Path, PkgxError, utils } from "pkgx"

export default async function(pkgspecs: string[]) {
const pkgs = await Promise.all(pkgspecs.map(x => parse_pkg_str(x, {latest: 'ok'})))
Expand All @@ -11,6 +11,14 @@ export default async function(pkgspecs: string[]) {
async function uninstall(prefix: Path, pkgs: PackageRequirement[]) {
for (const pkg of pkgs) {
const programs = await hooks.usePantry().project(pkg).provides()
const pkgstr = utils.pkg.str(pkg)
const parts = pkgstr.split("/")
parts.pop()
//FIXME: it removes the dir successfully. however, it still complains that it didn't delete that
try {
await Deno.remove(Path.home().join(`.cache/pkgx/envs/${parts}`).toString(), {recursive: true})
} catch (e) {
}
for (const program of programs) {
const f = prefix.join(program)
if (f.isFile()) {
Expand Down
9 changes: 7 additions & 2 deletions src/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ interface Flags {
sync: boolean
update: boolean
verbosity?: number
keepGoing: boolean
keepGoing: boolean,
unsafe: boolean
}

export default function(input: string[]): Args {
Expand All @@ -56,7 +57,8 @@ export default function(input: string[]): Args {
const flags: Flags = {
sync: false,
update: false,
keepGoing: false
keepGoing: false,
unsafe: false
}
let mode: string | undefined
let dryrun: boolean | undefined
Expand Down Expand Up @@ -89,6 +91,9 @@ export default function(input: string[]): Args {
case 'update':
flags.update = true
break
case 'unsafe':
flags.unsafe = true
break
case 'provides':
if (mode) throw new UsageError({msg: 'multiple modes specified'})
console.error("%cdeprecated: %cuse pkgx --provider instead", 'color: red', 'color: initial')
Expand Down

0 comments on commit 75b2825

Please sign in to comment.