Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1: uninstall if needed during install #1111

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions src/modes/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PackageRequirement, Path, PkgxError, hooks, utils } from "pkgx"
import { is_shebang } from "../utils/get-shebang.ts"
import { blurple, dim } from "../utils/color.ts"
import uninstall from "./uninstall.ts"
import undent from 'outdent'
const { usePantry } = hooks

Expand Down Expand Up @@ -37,15 +37,43 @@ export default async function(pkgs: PackageRequirement[], unsafe: boolean) {
async function write(dst: Path, pkgs: PackageRequirement[]) {
const UNSAFE = is_unsafe(unsafe)
for (const pkg of pkgs) {
const programs = await usePantry().project(pkg).provides()
program_loop:
const programs = (await usePantry().project(pkg).provides()).filter(
// skip for now since we would require specific versions and we haven't really got that
p => !p.includes("{{")
)
const pkgstr = utils.pkg.str(pkg)

for (const program of programs) {
const f = dst.join(program)

// skip for now since we would require specific versions and we haven't really got that
if (program.includes("{{")) continue
if (f.exists()) {
if (!f.isFile()) throw new PkgxError(`${f} already exists and is not a file`)

const pkgstr = utils.pkg.str(pkg)
const fd = is_shebang(f).catch(() => undefined)
if (!fd) throw new PkgxError(`${f} already exists and is not a pkgx installation`)

const lines = f.readLines()
const { value: shebang } = await lines.next()
if (shebang != "#!/bin/sh") {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
while (true) {
const { value, done } = await lines.next()
if (done) {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
const found = value.match(/^\s*exec pkgx \+([^ ]+)/);
const unsafe_found = value.match(/#MANAGED BY PKGX/);
if (found || unsafe_found) {
await uninstall([pkgstr])
break
}
}
}
}

for (const program of programs) {
const f = dst.mkdir('p').join(program)
let script = ""

if (UNSAFE) {
Expand Down Expand Up @@ -91,41 +119,6 @@ export default async function(pkgs: PackageRequirement[], unsafe: boolean) {
fi`
}

const f = dst.mkdir('p').join(program)

if (f.exists()) {
if (!f.isFile()) throw new PkgxError(`${f} already exists and is not a file`)

const fd = is_shebang(f).catch(() => undefined)
if (!fd) throw new PkgxError(`${f} already exists and is not a pkgx installation`)

const lines = f.readLines()
const { value: shebang } = await lines.next()
if (shebang != "#!/bin/sh") {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
while (true) {
const { value, done } = await lines.next()
if (done) {
throw new PkgxError(`${f} already exists and is not a pkgx installation`)
}
const found = value.match(/^\s*exec pkgx \+([^ ]+)/)?.[1]
const unsafe_found = value.match(/#MANAGED BY PKGX/);
if (found) {
if (found != pkgstr) {
throw new PkgxError(`different version already installed: ${blurple(program)} ${dim(`(${found})`)}`)
}
n++
console.warn(`pkgx: already installed: ${blurple(program)} ${dim(`(${found})`)}`)
continue program_loop
} else if (unsafe_found) {
n++
console.warn(`pkgx: already install: ${blurple(program)} ${dim("(UNSAFE)")}`);
continue program_loop
}
}
}

f.write({ force: true, text: undent`
#!/bin/sh
${script}`
Expand Down