Skip to content

Commit

Permalink
test: dyn env, move impl to reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Jul 22, 2024
1 parent da37428 commit 2583453
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 45 deletions.
44 changes: 43 additions & 1 deletion modules/envs/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { unwrapZodRes } from "../../port.ts";
import { execTask } from "../tasks/exec.ts";
import { getTasksCtx } from "../tasks/inter.ts";
import type { GhjkCtx } from "../types.ts";
import type {
EnvRecipeX,
Expand All @@ -7,7 +9,7 @@ import type {
WellKnownEnvRecipeX,
WellKnownProvision,
} from "./types.ts";
import { wellKnownProvisionTypes } from "./types.ts";
import { envVarDynTy, wellKnownProvisionTypes } from "./types.ts";
import validators from "./types.ts";

export type ProvisionReducerStore = Map<
Expand All @@ -31,6 +33,10 @@ export function getProvisionReducerStore(
store = new Map();
gcx.blackboard.set(id, store);
}
store?.set(
envVarDynTy,
installDynEnvReducer(gcx) as ProvisionReducer<Provision, Provision>,
);
return store;
}

Expand Down Expand Up @@ -85,3 +91,39 @@ export async function reduceStrangeProvisions(
};
return out;
}


export function installDynEnvReducer(gcx: GhjkCtx) {
return async (provisions: Provision[]) => {
const output = [];
const badProvisions = [];
const taskCtx = getTasksCtx(gcx);

for (const provision of provisions) {
const ty = "posix.envVar";
const key = provision.taskKey as string;

const taskGraph = taskCtx.taskGraph;
const taskConf = taskCtx.config;

const targetKey = Object.entries(taskConf.tasks)
.filter(([_, task]) => task.key == key)
.shift()?.[0];

if (targetKey) {
// console.log("key", key, " maps to target ", targetKey);
const results = await execTask(gcx, taskConf, taskGraph, targetKey, []);
output.push({ ...provision, ty, val: results[key] as any ?? "" });
} else {
badProvisions.push(provision);
}
}

if (badProvisions.length >= 1) {
throw new Error("cannot deduce task from keys", {
cause: { badProvisions },
});
}
return output;
};
}
5 changes: 0 additions & 5 deletions modules/ports/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
import type { Blackboard } from "../../host/types.ts";
import { getProvisionReducerStore } from "../envs/reducer.ts";
import {
installDynEnvReducer,
installSetReducer,
installSetRefReducer,
} from "./reducers.ts";
Expand Down Expand Up @@ -112,10 +111,6 @@ export class PortsModule extends ModuleBase<PortsCtx, PortsLockEnt> {
installSetProvisionTy,
installSetReducer(gcx) as ProvisionReducer<Provision, Provision>,
);
reducerStore.set(
envVarDynTy,
installDynEnvReducer(gcx) as ProvisionReducer<Provision, Provision>,
);
return pcx;
}

Expand Down
35 changes: 0 additions & 35 deletions modules/ports/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,3 @@ async function reduceInstArts(

return out;
}

export function installDynEnvReducer(gcx: GhjkCtx) {
return async (provisions: Provision[]) => {
const output = [];
const badProvisions = [];
const taskCtx = getTasksCtx(gcx);

for (const provision of provisions) {
const ty = "posix.envVar";
const key = provision.taskKey as string;

const taskGraph = taskCtx.taskGraph;
const taskConf = taskCtx.config;

const targetKey = Object.entries(taskConf.tasks)
.filter(([_, task]) => task.key == key)
.shift()?.[0];

if (targetKey) {
// console.log("key", key, " maps to target ", targetKey);
const val = await execTask(gcx, taskConf, taskGraph, targetKey, []);
output.push({ ...provision, ty, val: val as any ?? "" });
} else {
badProvisions.push(provision);
}
}

if (badProvisions.length >= 1) {
throw new Error("cannot deduce task from keys", {
cause: { badProvisions },
});
}
return output;
};
}
13 changes: 9 additions & 4 deletions modules/tasks/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function execTask(
args: string[],
// taskEnv: TaskEnvX,
// installGraph: InstallGraph,
): Promise<unknown | undefined> {
): Promise<Record<string, unknown>> {
let workSet = new Set([targetKey]);
{
const stack = [targetKey];
Expand All @@ -109,6 +109,8 @@ export async function execTask(
if (pendingTasks.length == 0) {
throw new Error("something went wrong, task graph starting set is empty");
}

const output = {} as Record<string, unknown>;
while (pendingTasks.length > 0) {
const taskKey = pendingTasks.pop()!;
const taskDef = tasksConfig.tasks[taskKey];
Expand Down Expand Up @@ -153,7 +155,7 @@ export async function execTask(
),
};

let output: unknown | undefined;
let taskOutput: unknown | undefined;
try {
if (taskDef.ty == "denoFile@v1") {
if (!gcx.ghjkfilePath) {
Expand All @@ -162,7 +164,7 @@ export async function execTask(
);
}
const workingDir = gcx.ghjkfilePath.parentOrThrow();
output = await execTaskDeno(
taskOutput = await execTaskDeno(
gcx.ghjkfilePath.toFileUrl().toString(),
{
key: taskDef.key,
Expand Down Expand Up @@ -217,9 +219,12 @@ export async function execTask(
}
pendingTasks.push(...readyTasks);

return output;
Object.assign(output, {[taskDef.key]: taskOutput});
}

if (workSet.size > 0) {
throw new Error("something went wrong, task graph work set is not empty");
}

return output;
}
23 changes: 23 additions & 0 deletions tests/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ task({
stdin: `
ghjk x eddy
test (cat eddy) = 'ed edd eddy'
`,
},
{
name: "dyn_vars",
ghjkTs: `
import { file } from "$ghjk/hack.ts";
const ghjk = file({});
export const sophon = ghjk.sophon;
const { env, task } = ghjk;
env("main")
.var("A", "A#STATIC")
.var("B", () => "B#DYNAMIC")
.var("D", ($) => $\`echo $A, $B > output.txt\`.text())
.onEnter(task({ fn: ($) => $\`echo enter\` })) // not executing in test env?
`,
ePoint: `fish`,
stdin: `
ghjk sync main
cat output.txt
test (cat output.txt) = 'A#STATIC, B#DYNAMIC'
`,
},
];
Expand Down

0 comments on commit 2583453

Please sign in to comment.