Skip to content

Commit

Permalink
feat: clean build output (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 9, 2024
1 parent adf1a46 commit 9f2d251
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/build/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export { Ajv };

import dedent from "npm:dedent@^1.5.1";
export { dedent };

export * as colors from "https://deno.land/std@0.208.0/fmt/colors.ts";
44 changes: 29 additions & 15 deletions src/build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
shutdownBuildState,
waitForBuildPromises,
} from "../build_state/mod.ts";
import { success } from "../term/status.ts";

/**
* Which format to use for building.
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function build(project: Project, opts: BuildOpts) {

await shutdownBuildState(buildState);

console.log("✅ Finished");
success("Success");
}

async function buildSteps(
Expand All @@ -80,7 +81,8 @@ async function buildSteps(
for (const module of project.modules.values()) {
if (module.db) {
buildStep(buildState, {
name: `Migrate`,
name: "Migrate",
description: `modules/${module.name}/db/schema.prisma`,
module,
// TODO: Also watch migrations folder in case a migration is created/destroyed
condition: {
Expand Down Expand Up @@ -112,7 +114,8 @@ async function buildSteps(

// TODO: Add way to compare runtime artifacts (or let this be handled by the cache version and never rerun?)
buildStep(buildState, {
name: "Inflate runtime",
name: "Generate",
description: "_gen/runtime/",
async build() {
await inflateRuntimeArchive(project);
},
Expand All @@ -126,7 +129,8 @@ async function buildSteps(
}

buildStep(buildState, {
name: "Type helpers",
name: "Generate",
description: "_gen/registry.d.ts",
condition: {
files: [...project.modules.values()].map((m) => resolve(m.path, "module.yaml")),
},
Expand All @@ -136,7 +140,8 @@ async function buildSteps(
});

buildStep(buildState, {
name: "Deno config",
name: "Generate",
description: "deno.json",
async build() {
await generateDenoConfig(project);
},
Expand All @@ -146,14 +151,16 @@ async function buildSteps(
await waitForBuildPromises(buildState);

buildStep(buildState, {
name: "Entrypoint",
name: "Generate",
description: "_gen/entrypoint.ts",
async build() {
await generateEntrypoint(project, opts);
},
});

buildStep(buildState, {
name: "OpenAPI",
name: "Generate",
description: "_gen/openapi.json",
async build() {
await generateOpenApi(project);
},
Expand All @@ -162,6 +169,7 @@ async function buildSteps(
if (opts.format == Format.Bundled) {
buildStep(buildState, {
name: "Bundle",
description: "_gen/output.js",
async build() {
const gen = resolve(project.path, "_gen");
const bundledFile = resolve(gen, "/output.js");
Expand Down Expand Up @@ -257,7 +265,8 @@ async function buildModule(
module: Module,
) {
buildStep(buildState, {
name: "Module config",
name: "Parse",
description: `modules/${module.name}/config.ts`,
module,
condition: {
// TODO: use tjs.getProgramFiles() to get the dependent files?
Expand Down Expand Up @@ -285,29 +294,32 @@ async function buildModule(
});

buildStep(buildState, {
name: "Module helper",
name: "Generate",
description: `modules/${module.name}/_gen/registry.d.ts`,
module,
condition: {
files: [resolve(module.path, "module.yaml")],
},
async build() {
await compileModuleHelper(project, module);
await compileModuleTypeHelper(project, module);
},
});

buildStep(buildState, {
name: "Type helper",
name: "Generate",
description: `modules/${module.name}/_gen/mod.ts`,
module,
condition: {
files: [resolve(module.path, "module.yaml")],
},
async build() {
await compileModuleTypeHelper(project, module);
await compileModuleHelper(project, module);
},
});

buildStep(buildState, {
name: "Test helper",
name: "Generate",
description: `modules/${module.name}/_gen/test.ts`,
module,
condition: {
files: [resolve(module.path, "module.yaml")],
Expand All @@ -329,7 +341,8 @@ async function buildScript(
script: Script,
) {
buildStep(buildState, {
name: "Script schema",
name: "Parse",
description: `modules/${module.name}/scripts/${script.name}.ts`,
module,
script,
condition: {
Expand Down Expand Up @@ -373,7 +386,8 @@ async function buildScript(
});

buildStep(buildState, {
name: "Script helper",
name: "Generate",
description: `modules/${module.name}/_gen/scripts/${script.name}.ts`,
module,
script,
condition: {
Expand Down
16 changes: 11 additions & 5 deletions src/build_state/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exists, resolve, tjs } from "../deps.ts";
import { Project } from "../project/project.ts";
import { verbose } from "../term/status.ts";
import { crypto, encodeHex } from "./deps.ts";

// TODO: Replace this with the OpenGB version instead since it means we'll change . We need to compile this in the build artifacts.
Expand Down Expand Up @@ -108,18 +109,22 @@ export async function compareFileHash(
const newHash = await hashFile(cache, path);
if (!oldHash) {
hasChanged = true;
verbose("Created", path);
} else if ("missing" in oldHash && "missing" in newHash) {
hasChanged = oldHash.missing != newHash.missing;
// Do nothing
} else if ("hash" in oldHash && "hash" in newHash) {
hasChanged = oldHash.hash != newHash.hash;
if (oldHash.hash != newHash.hash) {
hasChanged = true;
verbose("Edited", path);
}
} else {
hasChanged = true;
if ("missing" in oldHash) verbose("Created", path);
else verbose("Removed", path);
}

// Cache diff so we don't have to rehash the file
cache.fileDiffs.set(path, hasChanged);

if (hasChanged) console.log(`✏️ ${path}`);
}

return hasChanged;
Expand Down Expand Up @@ -173,7 +178,8 @@ export async function compareExprHash(
const newHash = await hashExpr(cache, name, value);
if (newHash != oldHash) {
hasChanged = true;
console.log(`✏️ ${name}`);

verbose("Changed", name);
}

// Cache diff so we don't have to rehash the expr
Expand Down
20 changes: 7 additions & 13 deletions src/build_state/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Module, Project, Script } from "../project/mod.ts";
import { Cache, compareExprHash, compareFileHash, loadCache, shutdownCache } from "./cache.ts";
import { progress } from "../term/status.ts";

// TODO: Convert this to a build flag
export const FORCE_BUILD = false;
Expand Down Expand Up @@ -34,6 +35,7 @@ export async function shutdownBuildState(buildState: BuildState) {

interface BuildStepOpts {
name: string;
description?: string;

/** Module this build step is relevant to. Only affects printed status. */
module?: Module;
Expand Down Expand Up @@ -85,14 +87,6 @@ export function buildStep(
buildState: BuildState,
opts: BuildStepOpts,
) {
// Build step name
let stepName = opts.name;
if (opts.module && opts.script) {
stepName += ` (${opts.module.name}.${opts.script.name})`;
} else if (opts.module) {
stepName += ` (${opts.module.name})`;
}

const fn = async () => {
// Determine if needs to be built
let needsBuild: boolean;
Expand All @@ -117,12 +111,12 @@ export function buildStep(
let onStart: () => void | undefined;
if (opts.delayedStart) {
// Wait to log start
onStart = () => logBuildStepStart(stepName);
onStart = () => logBuildStepStart(opts);
} else {
// Log start immediately
logBuildStepStart(stepName);
logBuildStepStart(opts);
onStart = () => {
console.warn(`onStart was called for ${stepName} but it can't have a delayed start`);
console.warn(`onStart was called for ${opts.name} but it can't have a delayed start`);
};
}

Expand All @@ -139,8 +133,8 @@ export function buildStep(
buildState.promises.push(fn());
}

function logBuildStepStart(name: string) {
console.log(`🔨 ${name}`);
function logBuildStepStart(opts: BuildStepOpts) {
progress(opts.name, opts.description);
}

export async function waitForBuildPromises(buildState: BuildState): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions src/project/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { emptyDir, exists, resolve } from "../deps.ts";
import { RegistryConfig, RegistryConfigGit, RegistryConfigLocal } from "../config/project.ts";
import { progress } from "../term/status.ts";

export interface Registry {
path: string;
Expand Down Expand Up @@ -119,7 +120,7 @@ async function resolveRegistryGit(
throw new Error(`Failed to find valid git endpoint for registry ${name}`);
}

console.log("📦 Cloning git registry", originUrl);
progress("Fetching", originUrl);

// Remove potentially dirty existing directory
await emptyDir(repoPath);
Expand Down Expand Up @@ -164,7 +165,7 @@ async function resolveRegistryGit(
args: ["cat-file", "-t", gitRef],
}).output();
if (!catOutput.success) {
console.log("📦 Fetching git registry", name, gitRef);
progress("Fetching", name);

const fetchOutput = await new Deno.Command("git", {
cwd: repoPath,
Expand Down
1 change: 1 addition & 0 deletions src/term/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as colors from "https://deno.land/std@0.208.0/fmt/colors.ts";
27 changes: 27 additions & 0 deletions src/term/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Based on https://github.com/rivet-gg/rivet-term/blob/f70f76f63eba12e535e53db1b436d5297edf979a/src/status.rs

import { colors } from "./deps.ts";

export function verbose(msg: string, data = "") {
if (Deno.env.get("VERBOSE")) console.error(`${colors.bold(colors.blue(msg))} ${data}`);
}

export function info(msg: string, data = "") {
console.error(`${colors.bold(colors.blue(msg))} ${data}`);
}

export function progress(msg: string, data = "") {
console.error(`${colors.bold(colors.green(msg))} ${data}`);
}

export function success(msg: string, data = "") {
console.error(`${colors.bold(colors.green(msg))} ${data}`);
}

export function warn(msg: string, data = "") {
console.error(`${colors.bold(colors.yellow(msg))} ${data}`);
}

export function error(msg: string, data = "") {
console.error(`${colors.bold(colors.red(msg))} ${data}`);
}

0 comments on commit 9f2d251

Please sign in to comment.