Skip to content

Commit

Permalink
refactor: native js unarchive
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Nov 30, 2023
1 parent 0a90307 commit 08b4c36
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 127 deletions.
5 changes: 3 additions & 2 deletions core/asdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
} from "./types.ts";
import {
depBinShimPath,
getInstallId,
pathWithDepShims,
spawn,
spawnOutput,
getInstallId
} from "./utils.ts";
// import * as std_plugs from "../std.ts";
import { std_fs, std_path } from "../deps/common.ts";

// FIXME: find a better way to expose std_plug.plug_id s
// that allows standard plugs to depend on each other
const curl_aa_id = {
id: "curl@aa",
};
Expand All @@ -29,7 +31,6 @@ export const manifest = {
name: "asdf@asdf",
version: "0.1.0",
moduleSpecifier: import.meta.url,
// deps: [std_plugs.tar_aa, std_plugs.git_aa],
deps: [curl_aa_id, git_aa_id],
};

Expand Down
130 changes: 130 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions deps/plug.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This contains dependencies used by plugins

export * from "./common.ts";
export * as compress from "https://deno.land/x/[email protected]/mod.ts";
export * as zip from "https://deno.land/x/[email protected]/mod.ts";
2 changes: 1 addition & 1 deletion ghjk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ import whiz from "./plugs/whiz.ts";
// protoc({});
// earthly({});
// ruff({});
whiz({});
// whiz({});
25 changes: 24 additions & 1 deletion plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
registerPlug,
validators,
} from "./core/mod.ts";
import { log, std_fs, std_path, std_url } from "./deps/plug.ts";
import { compress, log, std_fs, std_path, std_url, zip } from "./deps/plug.ts";
import { initDenoWorkerPlug, isWorker } from "./core/worker.ts";
import * as asdf from "./core/asdf.ts";
import logger from "./core/logger.ts";
Expand Down Expand Up @@ -161,4 +161,27 @@ export async function downloadFile(
);
}

/// Uses file extension to determine type
export async function unarchive(
path: string,
dest = "./",
ext = std_path.extname(path),
) {
switch (ext) {
case ".gz":
case ".tar.gz":
case ".tgz":
await compress.tgz.uncompress(path, dest);
break;
case ".tar":
await compress.tar.uncompress(path, dest);
break;
case ".zip":
await zip.decompress(path, dest);
break;
default:
throw Error("unsupported archive extension: ${ext}");
}
}

export const removeFile = Deno.remove;
14 changes: 2 additions & 12 deletions plugs/act.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addInstallGlobal,
depBinShimPath,
DownloadArgs,
downloadFile,
InstallArgs,
Expand All @@ -9,20 +8,16 @@ import {
PlugBase,
registerDenoPlugGlobal,
removeFile,
spawn,
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
import * as std_plugs from "../std.ts";

const manifest = {
name: "act@ghrel",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [
std_plugs.tar_aa,
],
};

registerDenoPlugGlobal(manifest, () => new Plug());
Expand Down Expand Up @@ -80,12 +75,7 @@ export class Plug extends PlugBase {
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await spawn([
depBinShimPath(std_plugs.tar_aa, "tar", args.depShims),
"xf",
fileDwnPath,
`--directory=${args.tmpDirPath}`,
]);
await unarchive(fileDwnPath, args.tmpDirPath);

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
Expand Down
33 changes: 8 additions & 25 deletions plugs/cargo-binstall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addInstallGlobal,
depBinShimPath,
DownloadArgs,
downloadFile,
InstallArgs,
Expand All @@ -9,24 +8,16 @@ import {
PlugBase,
registerDenoPlugGlobal,
removeFile,
spawn,
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
// FIXME: find a better way to expose std_plug.plug_Id
// that allows standard plugs to depend on each other
// import * as std_plugs from "../std.ts";

const tar_aa_id = {
id: "tar@aa",
};

export const manifest = {
name: "cargo-binstall@ghrel",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [tar_aa_id],
};

registerDenoPlugGlobal(manifest, () => new Plug());
Expand All @@ -38,15 +29,13 @@ export default function install(config: InstallConfigBase = {}) {
});
}

const repoAddress = "https://github.com/cargo-bins/cargo-binstall";
const repoOwner = "cargo-bins";
const repoName = "cargo-binstall";
const repoAddress = `https://github.com/${repoOwner}/${repoName}`;

export class Plug extends PlugBase {
manifest = manifest;

listBinPaths(): string[] {
return ["cargo-binstall", "detect-targets", "detect-wasi"];
}

async listAll() {
const metadataRequest = await fetch(
`https://index.crates.io/ca/rg/cargo-binstall`,
Expand All @@ -73,20 +62,14 @@ export class Plug extends PlugBase {
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await spawn([
depBinShimPath(tar_aa_id, "tar", args.depShims),
"xf",
fileDwnPath,
`--directory=${args.tmpDirPath}`,
]);
await unarchive(fileDwnPath, args.tmpDirPath);

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
}

await std_fs.copy(
args.tmpDirPath,
args.installPath,
std_path.resolve(args.installPath, "bin"),
);
}
}
Expand All @@ -105,12 +88,12 @@ function downloadUrl(installVersion: string, platform: PlatformInfo) {
}
if (platform.os == "darwin") {
// NOTE: the archive file name extensions are different from os to os
return `${repoAddress}/releases/download/v${installVersion}/cargo-binstall-${arch}-apple-darwin.full.zip`;
return `${repoAddress}/releases/download/v${installVersion}/${repoName}-${arch}-apple-darwin.full.zip`;
} else if (platform.os == "linux") {
// TODO: support for ubuntu/debian versions
// we'll need a way to expose that to plugs
const os = "unknown-linux-musl";
return `${repoAddress}/releases/download/v${installVersion}/cargo-binstall-${arch}-${os}.full.tgz`;
return `${repoAddress}/releases/download/v${installVersion}/${repoName}-${arch}-${os}.full.tgz`;
} else {
throw new Error(`unsupported os: ${platform.os}`);
}
Expand Down
9 changes: 2 additions & 7 deletions plugs/jco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
import node from "./node.ts";
import * as std_plugs from "../std.ts";
Expand All @@ -24,7 +25,6 @@ const manifest = {
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [
std_plugs.tar_aa,
std_plugs.node_org,
],
};
Expand Down Expand Up @@ -73,12 +73,7 @@ class Plug extends PlugBase {
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await spawn([
depBinShimPath(std_plugs.tar_aa, "tar", args.depShims),
"xf",
fileDwnPath,
`--directory=${args.tmpDirPath}`,
]);
await unarchive(fileDwnPath, args.tmpDirPath);

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
Expand Down
19 changes: 4 additions & 15 deletions plugs/mold.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addInstallGlobal,
depBinShimPath,
DownloadArgs,
downloadFile,
InstallArgs,
Expand All @@ -9,20 +8,16 @@ import {
PlugBase,
registerDenoPlugGlobal,
removeFile,
spawn,
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
import * as std_plugs from "../std.ts";

const manifest = {
name: "mold@ghrel",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [
std_plugs.tar_aa,
],
};

registerDenoPlugGlobal(manifest, () => new Plug());
Expand Down Expand Up @@ -75,12 +70,7 @@ export class Plug extends PlugBase {
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await spawn([
depBinShimPath(std_plugs.tar_aa, "tar", args.depShims),
"xf",
fileDwnPath,
`--directory=${args.tmpDirPath}`,
]);
await unarchive(fileDwnPath args.tmpDirPath)

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
Expand Down Expand Up @@ -118,9 +108,8 @@ function downloadUrl(installVersion: string, platform: PlatformInfo) {
default:
throw new Error(`unsupported arch: ${platform.arch}`);
}
return `${repoAddress}/releases/download/${installVersion}/${repoName}-${
installVersion.startsWith("v") ? installVersion.slice(1) : installVersion
}-${arch}-${os}.tar.gz`;
return `${repoAddress}/releases/download/${installVersion}/${repoName}-${installVersion.startsWith("v") ? installVersion.slice(1) : installVersion
}-${arch}-${os}.tar.gz`;
} else {
throw new Error(`unsupported os: ${platform.os}`);
}
Expand Down
18 changes: 2 additions & 16 deletions plugs/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addInstallGlobal,
depBinShimPath,
DownloadArgs,
downloadFile,
ExecEnvArgs,
Expand All @@ -11,25 +10,17 @@ import {
PlugBase,
registerDenoPlugGlobal,
removeFile,
spawn,
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
// import * as std_plugs from "../std.ts";

const tar_aa_id = {
id: "tar@aa",
};

// TODO: sanity check exports of all plugs
export const manifest = {
name: "node@org",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [
tar_aa_id,
],
};

registerDenoPlugGlobal(manifest, () => new Plug());
Expand Down Expand Up @@ -77,12 +68,7 @@ export class Plug extends PlugBase {
artifactUrl(args.installVersion, args.platform),
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);
await spawn([
depBinShimPath(tar_aa_id, "tar", args.depShims),
"xf",
fileDwnPath,
`--directory=${args.tmpDirPath}`,
]);
await unarchive(fileDwnPath, args.tmpDirPath);

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
Expand Down
14 changes: 2 additions & 12 deletions plugs/protoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addInstallGlobal,
depBinShimPath,
DownloadArgs,
downloadFile,
InstallArgs,
Expand All @@ -9,20 +8,16 @@ import {
PlugBase,
registerDenoPlugGlobal,
removeFile,
spawn,
std_fs,
std_path,
std_url,
unarchive,
} from "../plug.ts";
import * as std_plugs from "../std.ts";

const manifest = {
name: "protoc@ghrel",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [
std_plugs.unzip_aa,
],
};

registerDenoPlugGlobal(manifest, () => new Plug());
Expand Down Expand Up @@ -75,12 +70,7 @@ export class Plug extends PlugBase {
);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await spawn([
depBinShimPath(std_plugs.unzip_aa, "unzip", args.depShims),
fileDwnPath,
"-d",
args.tmpDirPath,
]);
await unarchive(fileDwnPath, args.tmpDirPath);

if (await std_fs.exists(args.installPath)) {
await removeFile(args.installPath, { recursive: true });
Expand Down
Loading

0 comments on commit 08b4c36

Please sign in to comment.