Skip to content

Commit

Permalink
feat(ports): InstallsDb
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Dec 23, 2023
1 parent 929ef21 commit 18de1d2
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 284 deletions.
2 changes: 1 addition & 1 deletion host/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function cli(args: CliArgs) {
if (!mod) {
throw new Error(`unrecognized module specified by ghjk.ts: ${man.id}`);
}
const instance = mod.ctor(ctx, man);
const instance = mod.init(ctx, man);
cmd = cmd.command(man.id, instance.command());
}
await cmd
Expand Down
2 changes: 1 addition & 1 deletion install/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export async function install(
exePath,
`#!/bin/sh
GHJK_DIR="$\{GHJK_DIR:-${ghjkDir}}" DENO_DIR="$\{GHJK_DENO_DIR:-${denoCacheDir}}"
${args.ghjkExecDenoExec} run --unstable-worker-options -A ${lockFlag} ${
${args.ghjkExecDenoExec} run --unstable-kv --unstable-worker-options -A ${lockFlag} ${
import.meta.resolve("../main.ts")
} $*`,
{ mode: 0o700 },
Expand Down
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
PortsModuleSecureConfig,
} from "./modules/ports/types.ts";
import logger from "./utils/logger.ts";
import { $ } from "./utils/mod.ts";
import { $, getPortRef } from "./utils/mod.ts";
import * as std_ports from "./modules/ports/std.ts";
import * as cpy from "./ports/cpy_bs.ts";
import * as node from "./ports/node.ts";
Expand Down Expand Up @@ -108,7 +108,7 @@ function stdDeps(args = { enableRuntimes: false }) {
return portsValidators.allowedPortDep.parse({
manifest: port,
defaultInst: {
portName: port.name,
portRef: getPortRef(port),
...liteInst,
},
});
Expand Down
59 changes: 59 additions & 0 deletions modules/ports/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Deno.Kv api is unstable
/// <reference lib="deno.unstable" />

import type {
DownloadArtifacts,
InstallArtifacts,
InstallConfigLite,
PortManifestX,
} from "./types.ts";

export type InstallRow = {
installId: string;
conf: InstallConfigLite;
manifest: PortManifestX;
installArts?: InstallArtifacts;
downloadArts: DownloadArtifacts;
progress: "downloaded" | "installed";
};

export abstract class InstallsDb {
abstract all(): Promise<InstallRow[]>;
abstract get(id: string): Promise<InstallRow | undefined>;
abstract set(id: string, row: InstallRow): Promise<void>;
abstract delete(id: string): Promise<void>;
abstract [Symbol.dispose](): void;
}

export async function installsDbKv(path: string): Promise<InstallsDb> {
const kv = await Deno.openKv(path);
return new DenoKvInstallsDb(kv);
}

class DenoKvInstallsDb extends InstallsDb {
prefix = "installs";
constructor(public kv: Deno.Kv) {
super();
}
async all(): Promise<InstallRow[]> {
const iterator = this.kv.list<InstallRow>({ prefix: [this.prefix] });
return (await Array.fromAsync(iterator)).map((ent) => ent.value);
}
async get(id: string) {
const val = await this.kv.get<InstallRow>([this.prefix, id]);
if (!val.value) return;
return val.value;
}
async set(id: string, row: InstallRow) {
const res = await this.kv.set([this.prefix, id], row);
if (!res.ok) {
throw new Error("Error on to Deno.Kv.set");
}
}
async delete(id: string) {
await this.kv.delete([this.prefix, id]);
}
[Symbol.dispose](): void {
this.kv.close();
}
}
39 changes: 34 additions & 5 deletions modules/ports/mod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
export * from "./types.ts";

import { cliffy_cmd } from "../../deps/cli.ts";

import { $ } from "../../utils/mod.ts";
import validators from "./types.ts";
import type { PortsModuleConfig } from "./types.ts";
import type { GhjkCtx } from "../types.ts";
import type { GhjkCtx, ModuleManifest } from "../types.ts";
import { ModuleBase } from "../mod.ts";
import { sync } from "./sync.ts";
import { installsDbKv } from "./db.ts";

export class PortsModule extends ModuleBase {
public static init(
ctx: GhjkCtx,
manifest: ModuleManifest,
) {
const res = validators.portsModuleConfig.safeParse(manifest.config);
if (!res.success) {
throw new Error("error parsing ports module config", {
cause: {
config: manifest.config,
zodErr: res.error,
},
});
}
return new PortsModule(ctx, res.data);
}
constructor(
public ctx: GhjkCtx,
public config: PortsModuleConfig,
private ctx: GhjkCtx,
private config: PortsModuleConfig,
) {
super();
}
Expand All @@ -24,7 +41,19 @@ export class PortsModule extends ModuleBase {
.command(
"sync",
new cliffy_cmd.Command().description("Syncs the environment.")
.action(() => sync(this.ctx.ghjkDir, this.ctx.envDir, this.config)),
.action(async () => {
const portsDir = await $.path(this.ctx.ghjkDir).resolve("ports")
.ensureDir();
using db = await installsDbKv(
portsDir.resolve("installs.db").toString(),
);
return await sync(
portsDir.toString(),
this.ctx.envDir,
this.config,
db,
);
}),
)
.command(
"outdated",
Expand Down
3 changes: 2 additions & 1 deletion modules/ports/std.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { manifest as man_node_org } from "../../ports/node.ts";
import { manifest as man_pnpm_ghrel } from "../../ports/pnpm.ts";
import { manifest as man_asdf_plugin_git } from "../../ports/asdf_plugin_git.ts";
import { manifest as man_cpy_bs_ghrel } from "../../ports/cpy_bs.ts";
import { getPortRef } from "../../utils/mod.ts";

const aaPorts: PortManifest[] = [
man_tar_aa,
Expand All @@ -39,7 +40,7 @@ const defaultAllowedDeps: AllowedPortDepX[] = [
.map((manifest) => ({
manifest,
defaultInst: {
portName: manifest.name,
portRef: getPortRef(manifest),
},
}))
.map((portDep) => validators.allowedPortDep.parse(portDep));
Expand Down
Loading

0 comments on commit 18de1d2

Please sign in to comment.