Skip to content

Commit

Permalink
refactor: remove python and node from default portDeps
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Dec 20, 2023
1 parent 3722078 commit 5100e29
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 51 deletions.
4 changes: 4 additions & 0 deletions ghjk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ ghjk.install(
act(),
...pipi({ packageName: "pre-commit" }),
);

export const secureConfig = ghjk.secureConfig({
allowedPortDeps: [...ghjk.stdDeps({ enableRuntimes: true })],
});
2 changes: 0 additions & 2 deletions install/mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! this installs the different shell ghjk hooks in ~/.local/share/ghjk
//! and a `ghjk` bin at ~/.local/share/bin

// TODO: support for different environments to use different versions of ghjk

import logger from "../utils/logger.ts";
import { std_fs, std_path } from "../deps/cli.ts";
import { $, dirs, importRaw } from "../utils/mod.ts";
Expand Down
89 changes: 64 additions & 25 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
//! This module is intended to be re-exported by `ghjk.ts` config scripts. Please
//! avoid importing elsewhere at it has side-effects.

// TODO: harden most of the items in here

import "./setup_logger.ts";

import portsValidators, {
type InstallConfigFat,
type PortsModuleConfig,
type PortsModuleConfigBase,
type PortsModuleSecureConfig,
import portsValidators from "./modules/ports/types.ts";
import type {
AllowedPortDep,
InstallConfigFat,
PortsModuleConfig,
PortsModuleConfigBase,
PortsModuleSecureConfig,
} from "./modules/ports/types.ts";
import type { SerializedConfig } from "./host/types.ts";
import logger from "./utils/logger.ts";
import { $ } 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";
import { std_modules } from "./modules/mod.ts";

const portsConfig: PortsModuleConfigBase = { installs: [] };
Expand All @@ -22,7 +28,7 @@ export const ghjk = Object.freeze({
getConfig: Object.freeze(getConfig),
});

export { $, install, logger };
export { $, install, logger, secureConfig, stdDeps };

function install(...configs: InstallConfigFat[]) {
const cx = portsConfig;
Expand All @@ -40,26 +46,59 @@ function addInstall(
cx.installs.push(config);
}

function secureConfig(
config: PortsModuleSecureConfig,
) {
return config;
}

function stdDeps(args = { enableRuntimes: false }) {
const out: AllowedPortDep[] = [
...Object.values(std_ports.map),
];
if (args.enableRuntimes) {
out.push(
...[
node.default(),
cpy.default(),
].map((fatInst) => {
const { port, ...liteInst } = fatInst;
return portsValidators.allowedPortDep.parse({
manifest: port,
defaultInst: {
portName: port.name,
...liteInst,
},
});
}),
);
}
return out;
}

function getConfig(secureConfig: PortsModuleSecureConfig | undefined) {
let allowedDeps;
if (secureConfig?.allowedPortDeps) {
allowedDeps = Object.fromEntries([
...secureConfig.allowedPortDeps.map((dep) =>
[dep.manifest.name, dep] as const
),
try {
const allowedDeps = Object.fromEntries([
...(secureConfig?.allowedPortDeps ?? stdDeps())
.map((dep) =>
[
dep.manifest.name,
portsValidators.allowedPortDep.parse(dep),
] as const
),
]);
} else {
allowedDeps = std_ports.map;
const fullPortsConfig: PortsModuleConfig = {
installs: portsConfig.installs,
allowedDeps: allowedDeps,
};
const config: SerializedConfig = {
modules: [{
id: std_modules.ports,
config: fullPortsConfig,
}],
};
return config;
} catch (cause) {
throw new Error(`error constructing config for serializatino`, { cause });
}
const fullPortsConfig: PortsModuleConfig = {
installs: portsConfig.installs,
allowedDeps: allowedDeps,
};
const config: SerializedConfig = {
modules: [{
id: std_modules.ports,
config: fullPortsConfig,
}],
};
return config;
}
14 changes: 7 additions & 7 deletions modules/ports/std.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This plugin exports the list of standard ports other
//! plugins are allowed to depend on.
import validators, {
type AllowedPortDep,
type AllowedPortDepX,
type PortDep,
type PortManifest,
} from "./types.ts";
Expand All @@ -26,27 +26,27 @@ const aaPorts: PortManifest[] = [

const denoPorts: PortManifest[] = [
man_cbin_ghrel,
man_node_org,
man_pnpm_ghrel,
man_asdf_plugin_git,
man_cpy_bs_ghrel,
// man_cpy_bs_ghrel,
// man_node_org,
];

const allowedDeps: AllowedPortDep[] = [
const defaultAllowedDeps: AllowedPortDepX[] = [
...aaPorts,
...denoPorts,
]
.map((man) => validators.portManifest.parse(man))
.map((manifest) => ({
manifest,
defaultInst: {
portName: manifest.name,
},
}));
}))
.map((portDep) => validators.allowedPortDep.parse(portDep));

export const map = Object.freeze(
Object.fromEntries(
allowedDeps.map((dep) => [dep.manifest.name, dep]),
defaultAllowedDeps.map((dep) => [dep.manifest.name, dep]),
),
);

Expand Down
8 changes: 6 additions & 2 deletions modules/ports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,16 @@ export type PortsModuleConfigBase = zod.infer<
typeof validators.portsModuleConfigBase
>;

export type AllowedPortDep = zod.infer<typeof validators.allowedPortDep>;
export type AllowedPortDep = zod.input<typeof validators.allowedPortDep>;
export type AllowedPortDepX = zod.infer<typeof validators.allowedPortDep>;

/// This is a secure sections of the config intended to be direct exports
/// from the config script instead of the global variable approach the
/// main [`GhjkConfig`] can take.
export type PortsModuleSecureConfig = zod.infer<
export type PortsModuleSecureConfig = zod.input<
typeof validators.portsModuleSecureConfig
>;
export type PortsModuleSecureConfigX = zod.input<
typeof validators.portsModuleSecureConfig
>;

Expand Down
8 changes: 6 additions & 2 deletions tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import whiz from "../ports/whiz.ts";
import cpython from "../ports/cpy_bs.ts";
import pipi from "../ports/pipi.ts";

type CustomE2eTestCase = Omit<E2eTestCase, "ePoints"> & { ePoint: string };
// order tests by download size to make failed runs less expensive
const cases: E2eTestCase[] = [
const cases: CustomE2eTestCase[] = [
...(Deno.build.os == "linux"
? [
// 8 megs
Expand Down Expand Up @@ -133,7 +134,7 @@ const cases: E2eTestCase[] = [
];

function testlManyE2e(
cases: E2eTestCase[],
cases: CustomE2eTestCase[],
testFn: (inp: E2eTestCase) => Promise<void>,
defaultEnvs: Record<string, string> = {},
) {
Expand All @@ -143,6 +144,9 @@ function testlManyE2e(
() =>
testFn({
...testCase,
ePoints: ["bash -c", "fish -c", "zsh -c"].map((sh) =>
`env ${sh} '${testCase.ePoint}'`
),
envs: {
...defaultEnvs,
...testCase.envs,
Expand Down
34 changes: 21 additions & 13 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export type E2eTestCase = {
name: string;
installConf: InstallConfigFat | InstallConfigFat[];
envs?: Record<string, string>;
ePoint: string;
ePoints: string[];
};

export async function localE2eTest(testCase: E2eTestCase) {
const { envs: testEnvs, installConf, ePoint } = testCase;
const { envs: testEnvs, installConf, ePoints } = testCase;
const tmpDir = $.path(
await Deno.makeTempDir({
prefix: "ghjk_le2e_",
Expand All @@ -32,12 +32,16 @@ export async function localE2eTest(testCase: E2eTestCase) {
await tmpDir.join("ghjk.ts").writeText(
`
export { ghjk } from "$ghjk/mod.ts";
import { install } from "$ghjk/mod.ts";
import * as ghjk from "$ghjk/mod.ts";
const confStr = \`
${JSON.stringify(installConfArray)}
\`;
const confObj = JSON.parse(confStr);
install(...confObj)
ghjk.install(...confObj)
export const secureConfig = ghjk.secureConfig({
allowedPortDeps: [...ghjk.stdDeps({ enableRuntimes: true })],
});
`
.replaceAll(
"$ghjk",
Expand All @@ -64,16 +68,16 @@ install(...confObj)
await $`${ghjkDir.join("ghjk").toString()} ports sync`
.cwd(tmpDir.toString())
.env(env);

/*
// print the contents of the ghjk dir for debugging purposes
const ghjkDirLen = ghjkDir.toString().length;
dbg((await Array.fromAsync(ghjkDir.walk())).map((entry) => [
entry.isDirectory ? "dir " : entry.isSymlink ? "ln " : "file",
entry.path.toString().slice(ghjkDirLen),
]));

for (const shell of ["bash -c", "fish -c", "zsh -c"]) {
await $.raw`env ${shell} '${ePoint}'`
*/
for (const ePoint of ePoints) {
await $.raw`${ePoint}`
.cwd(tmpDir.toString())
.env(env);
}
Expand All @@ -87,7 +91,7 @@ const templateStrings = {
};

export async function dockerE2eTest(testCase: E2eTestCase) {
const { name, envs: testEnvs, ePoint, installConf } = testCase;
const { name, envs: testEnvs, ePoints, installConf } = testCase;
const tag = `ghjk_e2e_${name}`;
const env = {
...testEnvs,
Expand All @@ -103,12 +107,16 @@ export async function dockerE2eTest(testCase: E2eTestCase) {
);
const configFile = `
export { ghjk } from "$ghjk/mod.ts";
import { install } from "$ghjk/mod.ts";
import * as ghjk from "$ghjk/mod.ts";
const confStr = \\\`
${serializedConf}
\\\`;
const confObj = JSON.parse(confStr);
install(...confObj)
ghjk.install(...confObj)
export const secureConfig = ghjk.secureConfig({
allowedPortDeps: [...ghjk.stdDeps({ enableRuntimes: true })],
});
`.replaceAll("$ghjk", "/ghjk");

const dFile = dbg(dFileTemplate.replaceAll(
Expand All @@ -119,12 +127,12 @@ install(...confObj)
.raw`${dockerCmd} buildx build --tag '${tag}' --network=host --output type=docker -f- .`
.env(env)
.stdinText(dFile);
for (const shell of ["bash", "fish", "zsh"]) {
for (const ePoint of ePoints) {
await $
.raw`${dockerCmd} run --rm ${
Object.entries(env).map(([key, val]) => ["-e", `${key}=${val}`])
.flat()
} ${tag} ${shell} -c '${ePoint}'`
} ${tag} ${ePoint}`
.env(env);
}
await $
Expand Down

0 comments on commit 5100e29

Please sign in to comment.