From 25834539bba1dc8bb3619b29cb26a9a1fe2f2a7e Mon Sep 17 00:00:00 2001 From: afmika Date: Mon, 22 Jul 2024 22:35:21 +0300 Subject: [PATCH] test: dyn env, move impl to reducers --- modules/envs/reducer.ts | 44 ++++++++++++++++++++++++++++++++++++++- modules/ports/mod.ts | 5 ----- modules/ports/reducers.ts | 35 ------------------------------- modules/tasks/exec.ts | 13 ++++++++---- tests/tasks.ts | 23 ++++++++++++++++++++ 5 files changed, 75 insertions(+), 45 deletions(-) diff --git a/modules/envs/reducer.ts b/modules/envs/reducer.ts index ff402a3..8eb1df4 100644 --- a/modules/envs/reducer.ts +++ b/modules/envs/reducer.ts @@ -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, @@ -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< @@ -31,6 +33,10 @@ export function getProvisionReducerStore( store = new Map(); gcx.blackboard.set(id, store); } + store?.set( + envVarDynTy, + installDynEnvReducer(gcx) as ProvisionReducer, + ); return store; } @@ -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; + }; +} diff --git a/modules/ports/mod.ts b/modules/ports/mod.ts index a6f430a..59bf1a5 100644 --- a/modules/ports/mod.ts +++ b/modules/ports/mod.ts @@ -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"; @@ -112,10 +111,6 @@ export class PortsModule extends ModuleBase { installSetProvisionTy, installSetReducer(gcx) as ProvisionReducer, ); - reducerStore.set( - envVarDynTy, - installDynEnvReducer(gcx) as ProvisionReducer, - ); return pcx; } diff --git a/modules/ports/reducers.ts b/modules/ports/reducers.ts index f75b172..6528c9d 100644 --- a/modules/ports/reducers.ts +++ b/modules/ports/reducers.ts @@ -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; - }; -} diff --git a/modules/tasks/exec.ts b/modules/tasks/exec.ts index 0e4e73a..89ceae1 100644 --- a/modules/tasks/exec.ts +++ b/modules/tasks/exec.ts @@ -91,7 +91,7 @@ export async function execTask( args: string[], // taskEnv: TaskEnvX, // installGraph: InstallGraph, -): Promise { +): Promise> { let workSet = new Set([targetKey]); { const stack = [targetKey]; @@ -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; while (pendingTasks.length > 0) { const taskKey = pendingTasks.pop()!; const taskDef = tasksConfig.tasks[taskKey]; @@ -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) { @@ -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, @@ -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; } diff --git a/tests/tasks.ts b/tests/tasks.ts index 5e35475..d704094 100644 --- a/tests/tasks.ts +++ b/tests/tasks.ts @@ -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' `, }, ];