Skip to content

Commit

Permalink
feat(envs): dynamic env (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Jul 3, 2024
1 parent f83710d commit 7480007
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@
play.*
examples/**/.ghjk
.dev

deps/https
node_analysis_*
v8_code_cache_*
dep_analysis_*
gen
npm
deno.land
jsr.io
esm.sh
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"tasks": {
"test": "deno test --parallel --unstable-worker-options --unstable-kv -A tests/*",
"self": "deno run -A --unstable-kv --unstable-worker-options main.ts ",
"cache": "deno cache deps/*",
"check": "deno run -A ./scripts/check.ts",
"dev": "deno run -A ./scripts/dev.ts"
Expand Down
41 changes: 22 additions & 19 deletions files/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
// here to make the resulting config reasonably stable
// across repeated serializaitons. No random identifiers.

import {
deep_eql,
hashAnyValue,
multibase32,
multibase64,
zod,
} from "../deps/common.ts";
import { deep_eql, multibase32, multibase64, zod } from "../deps/common.ts";

// ports specific imports
import portsValidators from "../modules/ports/types.ts";
Expand Down Expand Up @@ -76,7 +70,6 @@ export type EnvDefArgs = {
inherit?: EnvParent;
desc?: string;
vars?: Record<string, string | number>;
dynVars?: Record<string, DynEnvValue>;
/**
* Task to execute when environment is activated.
*/
Expand Down Expand Up @@ -107,7 +100,7 @@ export type TaskDefArgs = {
desc?: string;
dependsOn?: string | string[];
workingDir?: string | Path;
vars?: Record<string, string | number>;
vars?: Record<string, string | number>; // TODO: add DynEnvValue?
allowedBuildDeps?: (InstallConfigFat | AllowedPortDep)[];
installs?: InstallConfigFat | InstallConfigFat[];
inherit?: EnvParent;
Expand Down Expand Up @@ -660,6 +653,12 @@ export class Ghjkfile {
const prov: WellKnownProvision = { ty: "posix.envVar", key, val };
return prov;
}),
...Object.entries(final.dynVars).map((
[key, val],
) => {
const prov = { ty: "posix.envVarDyn", key, val } as Provision;
return prov;
}),
// env hooks
...hooks,
],
Expand Down Expand Up @@ -893,6 +892,7 @@ type EnvFinalizer = () => {
installSetId: string;
inherit: string | string[] | boolean;
vars: Record<string, string>;
dynVars: Record<string, string>;
desc?: string;
onEnterHookTasks: string[];
onExitHookTasks: string[];
Expand Down Expand Up @@ -951,7 +951,7 @@ export class EnvBuilder {
#file: Ghjkfile;
#inherit: string | string[] | boolean = true;
#vars: Record<string, string | number> = {};
#dynVarEvaluators: Record<string, DynEnvValue> = {};
#dynVars: Record<string, string> = {};
#desc?: string;
#onEnterHookTasks: string[] = [];
#onExitHookTasks: string[] = [];
Expand All @@ -972,6 +972,7 @@ export class EnvBuilder {
vars: Object.fromEntries(
Object.entries(this.#vars).map(([key, val]) => [key, val.toString()]),
),
dynVars: this.#dynVars,
desc: this.#desc,
onExitHookTasks: this.#onExitHookTasks,
onEnterHookTasks: this.#onEnterHookTasks,
Expand Down Expand Up @@ -1014,18 +1015,20 @@ export class EnvBuilder {
* Add multiple environment variable.
*/
vars(envVars: Record<string, string | number | DynEnvValue>) {
const vars = {};
const vars = {}, dynVars = {};
for (const [k, v] of Object.entries(envVars)) {
switch (typeof v) {
case "string":
case "number":
Object.assign(vars, { [k]: v });
break;
case "function": {
const fnHash = `dyn__${hashAnyValue(v, { algorithm: "md5" })}`;
Object.assign(vars, { [k]: fnHash });
const fnKey = getDynEvalKey(k, fnHash);
Object.assign(this.#dynVarEvaluators, { [fnKey]: v });
const taskKey = this.#file.addTask({
ty: "denoFile@v1",
fn: v,
nonce: k,
});
Object.assign(dynVars, { [k]: taskKey });
break;
}
default:
Expand All @@ -1039,6 +1042,10 @@ export class EnvBuilder {
this.#vars,
unwrapZodRes(validators.envVars.safeParse(vars), { envVars: vars }),
);
Object.assign(
this.#dynVars,
dynVars,
);
return this;
}

Expand Down Expand Up @@ -1135,7 +1142,3 @@ export function reduceAllowedDeps(
function objectHashSafe(obj: unknown) {
return objectHash(JSON.parse(JSON.stringify(obj)));
}

function getDynEvalKey(name: string, fnHash: string) {
return `${name}__${fnHash}`;
}
3 changes: 2 additions & 1 deletion modules/envs/posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export async function cookPosixEnv(
case "posix.headerFile":
includePaths.push(wellKnownProv.absolutePath);
break;
// case "posix.envVarDyn":
case "posix.envVar":
if (vars[wellKnownProv.key]) {
throw new Error(
Expand All @@ -84,7 +85,7 @@ export async function cookPosixEnv(
break;
default:
throw Error(
`unsupported provision type: ${(wellKnownProv as any).provision}`,
`unsupported provision type: ${(wellKnownProv as any).ty}`,
);
}
}));
Expand Down
16 changes: 16 additions & 0 deletions modules/envs/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { unwrapZodRes } from "../../port.ts";
// import { execTask } from "../tasks/exec.ts";
import type { GhjkCtx } from "../types.ts";
import type {
EnvRecipeX,
Expand Down Expand Up @@ -29,6 +30,7 @@ export function getProvisionReducerStore(
| undefined;
if (!store) {
store = new Map();
store.set("posix.envVarDyn", getEnvReducer(gcx));
gcx.blackboard.set(id, store);
}
return store;
Expand Down Expand Up @@ -85,3 +87,17 @@ export async function reduceStrangeProvisions(
};
return out;
}

function getEnvReducer(_gcx: GhjkCtx) {
// TODO:
// How to exec task from here? how to look for envs.#task?
// execTask(gcx, tasksConfig, taskGraph, targetKey, args)
// await execTask(gcx, {...??}, {}, "", "");
return (provisions: Provision[]) => {
return Promise.resolve(provisions.map((p) => {
p.ty = "posix.envVar";
// TODO
return p;
}));
};
}
6 changes: 6 additions & 0 deletions modules/envs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const installProvisionTypes = [
// array in the interest of type inference
export const wellKnownProvisionTypes = [
"posix.envVar",
// "posix.envVarDyn",
...posixFileProvisionTypes,
...hookProvisionTypes,
...installProvisionTypes,
Expand All @@ -38,6 +39,11 @@ const wellKnownProvision = zod.discriminatedUnion(
key: moduleValidators.envVarName,
val: zod.string(),
}),
// zod.object({
// ty: zod.literal(wellKnownProvisionTypes[1]),
// key: moduleValidators.envVarName,
// val: zod.string(),
// }),
...hookProvisionTypes.map((ty) =>
zod.object({
ty: zod.literal(ty),
Expand Down

0 comments on commit 7480007

Please sign in to comment.