Skip to content

Commit

Permalink
refactor(ghjk.ts): replace secureConfig with hack.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed May 25, 2024
1 parent f26fcec commit 3ee7475
Show file tree
Hide file tree
Showing 20 changed files with 554 additions and 324 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ for tasks that are meant to be common dependencies of other tasks.

### Secure configs

Certain options are configured through the `secureConfig` object.
To improve ergonmoics, the typescript ghjkfile implementation exports simple functions and objects that mutate some global variable.
This also means that any script you import, if it knows the URL of the exact ghjk implementation you're using, can import this authoring module and mess with your ghjkfile.
Certain options for your file are thus only read from an export called `secureConfig` that'll host some of the more sensetive configurations. These include:

```ts
import { env, stdSecureConfig } from "https://.../ghjk/mod.ts";
Expand Down
1 change: 1 addition & 0 deletions check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { $ } from "./utils/mod.ts";
const files = (await Array.fromAsync(
$.path(import.meta.url).parentOrThrow().expandGlob("**/*.ts", {
exclude: [
".git",
"play.ts",
".ghjk/**",
".deno-dir/**",
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"fmt": {
"exclude": [
"*.md",
"**/*.md",
".ghjk/**",
".deno-dir/**",
Expand Down
31 changes: 31 additions & 0 deletions examples/envs/ghjk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export { sophon } from "../../hack.ts";
import { config, env, install, task } from "../../hack.ts";
import * as ports from "../../ports/mod.ts";

config({
// we can change which environment
// is activated by default for example
// when we enter the directory
defaultEnv: "main",
// set the env all others envs will by
// default inherit from
defaultBaseEnv: "main",
});

env("test", {
installs: [ports.unzip()],
});

env("ci")
.install(ports.opentofu_ghrel());

// top level `install` calls just
// go to an enviroment called "main"
install(ports.protoc());

// we can modify "main" directly
env("main")
// hooks execute when environments are
// activated/deactivated in interactive shells
.onEnter(task(($) => $`echo enter`))
.onExit(task(($) => $`echo exit`));
100 changes: 100 additions & 0 deletions examples/kitchen/ghjk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { stdDeps } from "../../files/mod.ts";
import { file } from "../../mod.ts";
import * as ports from "../../ports/mod.ts";

const ghjk = file({
// configre an empty env so that no ports are avail by default in our workdir
defaultEnv: "empty",
envs: [{ name: "empty", inherit: false }],
// we wan't all other envs to start from empty unless they opt otherwise
defaultBaseEnv: "empty",

// we won't use the following for now
// but they pretty much configure the "main" env
allowedBuildDeps: [],
installs: [],
stdDeps: true,
enableRuntimes: true,
// tasks aren't attached to envs
// but have their own env
tasks: [],
});

// we need this export for this file to be a valid ghjkfile
// it's the one thing used by the ghjk host implementation to
// interact with your ghjkfile
export const sophon = ghjk.sophon;

const { install, env, task } = ghjk;

// we can configure main like this as well
env("main")
// provision env vars to be acccessbile in the env
.var("RUST_LOG", "info,actix=warn")
// provision programs to be avail in the env
.install(ports.jq_ghrel())
.allowedBuildDeps([
// ports can use the following installs at build time
// very WIP mechanism but this is meant to prevent ports from
// pulling whatever dependency they want at build time unless
// explicityl allowed to do so
ports.cpy_bs({ version: "3.8.18", releaseTag: "20240224" }),
ports.node({}),
ports.rust({ version: "stable" }),
// add the std deps including the runtime ports.
// These includes node and python but still, precedence is given
// to our configuration of those ports above
...stdDeps({ enableRuntimes: true }),
]);

// these top level installs go to the main env as well
install(
ports.rust({ version: "stable" }),
ports.protoc(),
);

const ci = env("ci", {
// this inherits from main so it gets protoc and curl
inherit: "main",
// extra installs
installs: [ports.jq_ghrel()],
// it has extra allowed deps
allowedBuildDeps: [ports.node()],
// more env vars
vars: {
CI: 1,
},
desc: "do ci stuff",
});

// tasks are invocable from the cli
task("install-app", ($) => $`cargo fetch`);

task("build-app", {
dependsOn: "install-app",
// the task's env inherits from ci
inherit: ci.name,
// it can add more items to that env
installs: [],
// vars
vars: {
RUST_BACKTRACE: 1,
},
// allowed build deps
allowedBuildDeps: [ports.zstd()],
desc: "build the app",
fn: async ($) => {
await $`cargo build -p app`;
// we can access tar here from the ci env
await $`tar xcv ./target/debug/app -o app.tar.gz`;
},
});

env("dev")
.inherit("main")
// we can set tasks to run on activation/decativation
.onEnter(task(($) => $`echo enter`))
.onEnter(task({
workingDir: "..",
fn: ($) => $`ls`,
}));
7 changes: 0 additions & 7 deletions examples/protoc/ghjk.ts

This file was deleted.

6 changes: 3 additions & 3 deletions examples/tasks/ghjk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { ghjk } from "../../mod.ts";
import { logger, task } from "../../mod.ts";
export { sophon } from "../../hack.ts";
import { logger, task } from "../../hack.ts";
import * as ports from "../../ports/mod.ts";

task("greet", async ($, { argv: [name] }) => {
Expand All @@ -9,7 +9,7 @@ task("greet", async ($, { argv: [name] }) => {
const ha = task({
name: "ha",
installs: [ports.protoc()],
envVars: { STUFF: "stuffier" },
vars: { STUFF: "stuffier" },
async fn($) {
await $`echo $STUFF;
protoc --version;
Expand Down
2 changes: 1 addition & 1 deletion files/deno/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function serializeConfig(uri: string, envVars: Record<string, string>) {
const { setup: setupLogger } = await import("../../utils/logger.ts");
setupLogger();
const mod = await import(uri);
const rawConfig = await mod.ghjk.getConfig(uri, mod.secureConfig);
const rawConfig = await mod.sophon.getConfig(uri, mod.secureConfig);
const config = JSON.parse(JSON.stringify(rawConfig));
return {
config,
Expand Down
Loading

0 comments on commit 3ee7475

Please sign in to comment.