@skathi-native/cli 0.1.0-beta.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/aot-inputs.ts +21 -2
  2. package/build.ts +241 -60
  3. package/check/bundle.ts +265 -0
  4. package/check/codes.ts +19 -0
  5. package/check/command.ts +5 -3
  6. package/check/graph.ts +3 -1
  7. package/check/index.ts +54 -2
  8. package/check/program.ts +26 -6
  9. package/cli/aot-build.ts +10 -7
  10. package/cli/build-app.ts +37 -8
  11. package/cli/config.ts +2 -2
  12. package/cli/create.ts +31 -8
  13. package/cli/dev.ts +109 -19
  14. package/cli/effects.ts +162 -0
  15. package/cli/elf.ts +3 -2
  16. package/cli/help.ts +56 -147
  17. package/cli/host-fetch.ts +33 -10
  18. package/cli/host.ts +43 -33
  19. package/cli/sk.ts +437 -234
  20. package/cli/skpk.ts +2 -0
  21. package/cli/stamp.ts +2 -2
  22. package/cli/term.ts +169 -38
  23. package/cli/workload-cache.ts +258 -0
  24. package/devtools/commands.ts +25 -0
  25. package/effects/compile.ts +1081 -0
  26. package/effects/redslangc.json +23 -0
  27. package/effects/redslangc.ts +220 -0
  28. package/effects/sk.slang +882 -0
  29. package/package.json +10 -4
  30. package/prod.ts +40 -7
  31. package/svelte.ts +76 -0
  32. package/templates/icon.png +0 -0
  33. package/templates/minimal/src/App.vue +2 -2
  34. package/templates/showcase/assets/textures/golden.png +0 -0
  35. package/templates/showcase/assets/textures/hammered.png +0 -0
  36. package/templates/showcase/assets/textures/noir.png +0 -0
  37. package/templates/showcase/assets/textures/reeded.png +0 -0
  38. package/templates/showcase/assets/textures/teal-orange.png +0 -0
  39. package/templates/showcase/shaders/dissolve.slang +40 -0
  40. package/templates/showcase/shaders/fireflies.slang +58 -0
  41. package/templates/showcase/shaders/glass.slang +31 -0
  42. package/templates/showcase/shaders/grade.slang +34 -0
  43. package/templates/showcase/shaders/halftone.slang +42 -0
  44. package/templates/showcase/shaders/lens.slang +51 -0
  45. package/templates/showcase/shaders/nebula.slang +57 -0
  46. package/templates/showcase/shaders/orbit.slang +52 -0
  47. package/templates/showcase/shaders/pond.slang +76 -0
  48. package/templates/showcase/shaders/ripples.slang +71 -0
  49. package/templates/showcase/shaders/shatter.slang +116 -0
  50. package/templates/showcase/shaders/sheen.slang +23 -0
  51. package/templates/showcase/shaders/snow.slang +57 -0
  52. package/templates/showcase/shaders/spotlight.slang +52 -0
  53. package/templates/showcase/src/App.vue +11 -16
  54. package/templates/showcase/src/components/FullWindow.vue +77 -0
  55. package/templates/showcase/src/components/Page.vue +2 -0
  56. package/templates/showcase/src/fullwindow.ts +36 -0
  57. package/templates/showcase/src/icons.ts +2 -0
  58. package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
  59. package/templates/showcase/src/pages/MotionPage.vue +71 -1
  60. package/templates/showcase/src/pages/ShadersPage.vue +443 -0
  61. package/templates/showcase/src/pages/StylesPage.vue +9 -1
  62. package/templates/showcase/src/router.ts +32 -1
  63. package/templates/showcase/src/styles.css +2 -2
  64. package/templates/svelte/.vscode/settings.json +3 -0
  65. package/templates/svelte/assets/hello.txt +1 -0
  66. package/templates/svelte/src/App.svelte +142 -0
  67. package/templates/svelte/src/main.ts +4 -0
  68. package/vitecompat.ts +2 -0
package/cli/build-app.ts CHANGED
@@ -9,7 +9,8 @@
9
9
  // macos-bundle.ts: the host is copied into `<exeName>.app`, the SKPK goes in Contents/Resources,
10
10
  // and the bundle is signed (ad hoc unless an identity is given). On Linux the SKPK is appended to a
11
11
  // copy of the host ELF (elf.ts). `PACKAGERS` holds the three. With `--aot` (Windows x64), aot-build.ts adds
12
- // the app's native image to the SKPK.
12
+ // the app's native image to the SKPK. The app's shaders/*.slang go in as `sk/effects/*` (effects.ts), every
13
+ // renderer's target whatever the OS.
13
14
 
14
15
  import { createHash } from "node:crypto";
15
16
  import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
@@ -17,6 +18,7 @@ import { join, relative, resolve, sep } from "node:path";
17
18
  import { aotCheck, aotPlatformProblem, buildNative, hostAot, type NativeBuild } from "./aot-build";
18
19
  import { collectAssets, type AssetFile } from "./assets";
19
20
  import { ConfigError, loadAppConfig, parseVariant, type AppConfig, type PackConfig, type Variant } from "./config";
21
+ import { buildAppEffects, type BuiltEffects } from "./effects";
20
22
  import { stampElf, type ElfStampResult } from "./elf";
21
23
  import { CLI_VERSION, PKG_DIR, type ResolvedHost } from "./host";
22
24
  import { resolveOrFetchHost } from "./host-fetch";
@@ -60,6 +62,8 @@ export interface BuildOptions {
60
62
  aot?: boolean | undefined;
61
63
  /** The sk-aot to use instead of the checkout's or the release's. */
62
64
  aotCompiler?: string | undefined;
65
+ /** The redslangc that compiles the app's effect shaders instead of the pinned release. */
66
+ shaderCompiler?: string | undefined;
63
67
  /** Colors in a report that refuses the build. */
64
68
  color?: boolean;
65
69
  platform?: Platform | undefined;
@@ -119,6 +123,8 @@ export interface BuildResult {
119
123
  payloadBytes: number;
120
124
  packs: { name: string; path: string; bytes: number; assets: number }[];
121
125
  aotInputs: string | null;
126
+ /** The app's own --shading effects, compiled into the payload. */
127
+ effects: { names: string[]; dir: string; compiler: string } | null;
122
128
  native: {
123
129
  manifest: string;
124
130
  imageBytes: number;
@@ -344,9 +350,12 @@ const PACKAGERS: Record<Platform, (input: PackageInput) => Packaged> = {
344
350
  export async function buildApp(options: BuildOptions): Promise<BuildResult> {
345
351
  const progress = options.progress ?? (options.log ? lineProgress(options.log, false) : progressFor(process.stderr));
346
352
  try {
347
- return await build(options, progress);
348
- } finally {
353
+ const result = await build(options, progress);
349
354
  if (!options.progress) progress.end();
355
+ return result;
356
+ } catch (e) {
357
+ progress.fail();
358
+ throw e;
350
359
  }
351
360
  }
352
361
 
@@ -366,14 +375,16 @@ async function build(options: BuildOptions, progress: Progress): Promise<BuildRe
366
375
  const problem = aotPlatformProblem(platform);
367
376
  if (problem) throw new ConfigError(problem);
368
377
  if (options.payloadOnly)
369
- throw new ConfigError("--aot compiles against the host it stamps, so it cannot be combined with --payload-only");
378
+ throw new ConfigError(
379
+ "--aot compiles against the workload it stamps, so it cannot be combined with --payload-only",
380
+ );
370
381
  }
371
382
  const outDir = resolve(options.out ?? join(config.appRoot, "dist", ...(options.mode ? [options.mode] : [])));
372
383
  const work = join(outDir, ".sk");
373
384
  mkdirSync(work, { recursive: true });
374
385
 
375
386
  // The host first: a missing or mismatched host should fail before a bundle is spent on it.
376
- if (!options.payloadOnly) progress.step("host");
387
+ if (!options.payloadOnly) progress.step("workload");
377
388
  const host = options.payloadOnly
378
389
  ? null
379
390
  : await resolveOrFetchHost({
@@ -382,6 +393,7 @@ async function build(options: BuildOptions, progress: Progress): Promise<BuildRe
382
393
  allowMismatch: options.allowHostMismatch,
383
394
  platform,
384
395
  log,
396
+ onBytes: (done, total) => progress.bytes(done, total),
385
397
  });
386
398
  if (host) {
387
399
  progress.detail(`${host.info.variant} · Skathi ${host.info.skathi} ${host.info.profile} · ${host.source}`);
@@ -389,6 +401,14 @@ async function build(options: BuildOptions, progress: Progress): Promise<BuildRe
389
401
  }
390
402
  const hostNative = aot && host ? hostAot(host) : null;
391
403
 
404
+ const effects: BuiltEffects | null = await buildAppEffects({
405
+ appRoot: config.appRoot,
406
+ out: join(work, "effects"),
407
+ compiler: options.shaderCompiler,
408
+ progress,
409
+ });
410
+ if (!effects) rmSync(join(work, "effects"), { recursive: true, force: true });
411
+
392
412
  const jsOut = join(work, "app.prod.js");
393
413
  const assetsJson = join(work, "assets.json");
394
414
  // A stale list from an older build must not survive a builder that wrote none.
@@ -482,6 +502,7 @@ async function build(options: BuildOptions, progress: Progress): Promise<BuildRe
482
502
  { name: NAMES.WINDOW, kind: KIND.WINDOW, data: windowJson },
483
503
  { name: NAMES.SCRIPT, kind: KIND.SCRIPT, data: script },
484
504
  ...split.own.map((f) => ({ name: f.key, kind: KIND.ASSET, data: new Uint8Array(readFileSync(f.path)) })),
505
+ ...(effects?.inputs ?? []),
485
506
  ];
486
507
  const skpk = join(work, "app.skpk");
487
508
  for (const p of packs) writeFileSync(join(work, p.file), p.blob);
@@ -535,6 +556,11 @@ async function build(options: BuildOptions, progress: Progress): Promise<BuildRe
535
556
  assets: collected.files,
536
557
  bundleBytes: script.length,
537
558
  aotInputs: options.aotInputs ? join(work, "aot-inputs", "manifest.json") : null,
559
+ effects: effects && {
560
+ names: effects.effects.compiled.map((c) => c.effect.name),
561
+ dir: join(work, "effects"),
562
+ compiler: effects.compiler.path,
563
+ },
538
564
  native: native && {
539
565
  manifest: native.manifestPath,
540
566
  imageBytes: native.imageBytes,
@@ -578,7 +604,7 @@ export function summary(r: BuildResult, color = false): string {
578
604
  row("exe", p.cyan(r.exe)),
579
605
  row(
580
606
  "",
581
- `${mib(s.bytes)}, ${r.variant} host, subsystem ${s.subsystem === 2 ? "GUI" : "console"}, ` +
607
+ `${mib(s.bytes)}, ${r.variant} workload, subsystem ${s.subsystem === 2 ? "GUI" : "console"}, ` +
582
608
  `${s.iconCount ? `${s.iconCount} icon` : "no icon"}, ` +
583
609
  bad(s.dataAligned, "data page-aligned", "data NOT aligned"),
584
610
  ),
@@ -590,7 +616,7 @@ export function summary(r: BuildResult, color = false): string {
590
616
  row("exe", p.cyan(e.out)),
591
617
  row(
592
618
  "",
593
- `${mib(e.bytes)}, ${r.variant} host (${e.machine}), payload appended at ${e.payloadOffset}, ` +
619
+ `${mib(e.bytes)}, ${r.variant} workload (${e.machine}), payload appended at ${e.payloadOffset}, ` +
594
620
  bad(e.dataAligned, "data page-aligned", "data NOT aligned"),
595
621
  ),
596
622
  );
@@ -602,7 +628,7 @@ export function summary(r: BuildResult, color = false): string {
602
628
  b.signed === "-" ? "signed ad hoc" : b.signed ? `signed as ${JSON.stringify(b.signed)}` : p.yellow("NOT signed");
603
629
  lines.push(
604
630
  row("app", p.cyan(b.app)),
605
- row("", `${mib(b.bytes)}, ${r.variant} host, ${b.bundleId}, ${icon}, ${signed}`),
631
+ row("", `${mib(b.bytes)}, ${r.variant} workload, ${b.bundleId}, ${icon}, ${signed}`),
606
632
  );
607
633
  if (b.signNote) lines.push(row("", p.dim(b.signNote)));
608
634
  }
@@ -613,6 +639,9 @@ export function summary(r: BuildResult, color = false): string {
613
639
  ),
614
640
  row("payload", `${kib(r.payloadBytes)} ${p.dim(r.skpk)}`),
615
641
  );
642
+ if (r.effects) {
643
+ lines.push(row("effects", `${r.effects.names.join(", ")} ${p.dim("hlsl · wgsl · metal")}`));
644
+ }
616
645
  if (r.native) {
617
646
  lines.push(
618
647
  row(
package/cli/config.ts CHANGED
@@ -37,7 +37,7 @@ export const PARTS_NOT_SELECTABLE =
37
37
  export function parseVariant(v: unknown, where: string): Variant {
38
38
  const name = typeof v === "string" ? v.trim() : "";
39
39
  if (VARIANTS.includes(name)) return name;
40
- throw new ConfigError(`${where}: ${JSON.stringify(v)} is not a host variant — ${VARIANTS.join(" or ")}`);
40
+ throw new ConfigError(`${where}: ${JSON.stringify(v)} is not a workload variant — ${VARIANTS.join(" or ")}`);
41
41
  }
42
42
 
43
43
  export function cargoFeatureArgs(variant: Variant): string[] {
@@ -306,7 +306,7 @@ function readMacos(
306
306
  const [wantMaj = 0, wantMinor = 0] = MACOS_MINIMUM.split(".").map(Number);
307
307
  if (maj < wantMaj || (maj === wantMaj && minor < wantMinor)) {
308
308
  warnings.push(
309
- `sk.config.json "macos.minimumSystemVersion" ${min} is below ${MACOS_MINIMUM}, which the host is built for — ` +
309
+ `sk.config.json "macos.minimumSystemVersion" ${min} is below ${MACOS_MINIMUM}, which the workload is built for — ` +
310
310
  "an older macOS will offer to open the app and then fail to load it",
311
311
  );
312
312
  }
package/cli/create.ts CHANGED
@@ -19,15 +19,25 @@ import { colorEnabled, paint } from "./term";
19
19
  export const PACKAGE_MANAGERS = ["bun", "npm", "pnpm", "yarn"] as const;
20
20
  export type PackageManager = (typeof PACKAGE_MANAGERS)[number];
21
21
 
22
- export const TEMPLATES = ["showcase", "minimal"] as const;
22
+ export const TEMPLATES = ["showcase", "minimal", "svelte"] as const;
23
23
  export type Template = (typeof TEMPLATES)[number];
24
24
 
25
+ interface Checker {
26
+ script: string;
27
+ devDependencies: Record<string, string>;
28
+ /** The component files the app's tsconfig.json includes besides its .ts sources. */
29
+ sources: string;
30
+ }
31
+
32
+ const VUE_TSC: Checker = { script: "vue-tsc --noEmit", devDependencies: { "vue-tsc": "^3" }, sources: "src/**/*.vue" };
33
+
25
34
  interface TemplateInfo {
26
35
  hint: string;
27
- /** Taken at the versions the runtime itself depends on. */
36
+ /** Taken at the versions the runtime itself depends on, or accepts as a peer. */
28
37
  dependencies: string[];
29
38
  window: Record<string, unknown>;
30
39
  packs?: { name: string; match?: string[] }[];
40
+ checker: Checker;
31
41
  }
32
42
 
33
43
  const TEMPLATE_INFO: Record<Template, TemplateInfo> = {
@@ -36,11 +46,19 @@ const TEMPLATE_INFO: Record<Template, TemplateInfo> = {
36
46
  dependencies: ["pinia", "vue-router"],
37
47
  window: { width: 1100, height: 720, minWidth: 480, minHeight: 360, titleBarStyle: "hidden", titleBarHeight: 40 },
38
48
  packs: [{ name: "media", match: ["assets/media/**"] }],
49
+ checker: VUE_TSC,
39
50
  },
40
51
  minimal: {
41
52
  hint: "one window, one component",
42
53
  dependencies: [],
43
54
  window: { width: 760, height: 520, titleBarStyle: "default" },
55
+ checker: VUE_TSC,
56
+ },
57
+ svelte: {
58
+ hint: "the minimal app in Svelte 5",
59
+ dependencies: ["svelte"],
60
+ window: { width: 760, height: 520, titleBarStyle: "default" },
61
+ checker: { script: "svelte-check", devDependencies: { "svelte-check": "^4" }, sources: "src/**/*.svelte" },
44
62
  },
45
63
  };
46
64
 
@@ -49,7 +67,7 @@ export const TEMPLATES_DIR = resolve(import.meta.dir, "..", "templates");
49
67
  export const CONFIG_SCHEMA = "./node_modules/@skathi-native/runtime/sk.config.schema.json";
50
68
 
51
69
  /** Files whose text has its `{{key}}` placeholders filled in; anything else is copied byte for byte. */
52
- const TEXT_FILES = new Set([".vue", ".ts", ".css", ".txt", ".json", ".html", ".svg"]);
70
+ const TEXT_FILES = new Set([".vue", ".svelte", ".ts", ".css", ".txt", ".json", ".html", ".svg"]);
53
71
 
54
72
  export interface CreateOptions {
55
73
  name?: string | undefined;
@@ -114,7 +132,11 @@ export function create(target: string, options: CreateOptions = {}): string[] {
114
132
  const template = options.template ?? "showcase";
115
133
  const info = TEMPLATE_INFO[template];
116
134
  const platforms = options.platforms?.length ? options.platforms : [currentPlatform()];
117
- const runtimeDeps = (runtimeRequire("./package.json") as { dependencies: Record<string, string> }).dependencies;
135
+ const runtimePkg = runtimeRequire("./package.json") as {
136
+ dependencies: Record<string, string>;
137
+ peerDependencies?: Record<string, string>;
138
+ };
139
+ const runtimeDeps = { ...runtimePkg.peerDependencies, ...runtimePkg.dependencies };
118
140
  const sdk = `^${CLI_VERSION}`;
119
141
 
120
142
  const files: Record<string, string | Uint8Array> = {
@@ -129,13 +151,13 @@ export function create(target: string, options: CreateOptions = {}): string[] {
129
151
  build: "sk build",
130
152
  publish: "sk publish",
131
153
  check: "sk check",
132
- typecheck: "vue-tsc --noEmit",
154
+ typecheck: info.checker.script,
133
155
  },
134
156
  dependencies: {
135
157
  "@skathi-native/runtime": sdk,
136
158
  ...Object.fromEntries(info.dependencies.map((dep) => [dep, runtimeDeps[dep]])),
137
159
  },
138
- devDependencies: { "@skathi-native/cli": sdk, typescript: "^5", "vue-tsc": "^3" },
160
+ devDependencies: { "@skathi-native/cli": sdk, typescript: "^5", ...info.checker.devDependencies },
139
161
  }),
140
162
  "sk.config.json": json({
141
163
  $schema: CONFIG_SCHEMA,
@@ -153,7 +175,7 @@ export function create(target: string, options: CreateOptions = {}): string[] {
153
175
  "icon.png": readFileSync(join(TEMPLATES_DIR, "icon.png")),
154
176
  "tsconfig.json": json({
155
177
  extends: "@skathi-native/runtime/tsconfig.app.json",
156
- include: ["src/**/*.ts", "src/**/*.vue"],
178
+ include: ["src/**/*.ts", info.checker.sources],
157
179
  }),
158
180
  ".gitignore": "node_modules/\ndist/\n",
159
181
  };
@@ -179,6 +201,7 @@ export interface CreateFlags {
179
201
  devtools?: boolean | undefined;
180
202
  pm?: string | undefined;
181
203
  yes?: boolean | undefined;
204
+ color?: boolean | undefined;
182
205
  }
183
206
 
184
207
  function parsePm(v: string): PackageManager {
@@ -277,7 +300,7 @@ export async function createCommand(target: string | undefined, flags: CreateFla
277
300
  p.note(next, "Next");
278
301
  p.outro(`${options.name ?? basename(resolve(dir))} is ready`);
279
302
  } else {
280
- const c = paint(colorEnabled(process.stdout));
303
+ const c = paint(colorEnabled(process.stdout, flags.color));
281
304
  console.log(`${c.green("✓")} created ${c.cyan(resolve(dir))}\n ${c.dim("next:")} ${next.replace(/\n/g, " && ")}`);
282
305
  }
283
306
  return 0;
package/cli/dev.ts CHANGED
@@ -3,23 +3,32 @@
3
3
  //
4
4
  // The dev host, first hit wins: `--exe` or SK_DEV_HOST; in a checkout, its debug sk-app (`cargo build
5
5
  // -p sk-app`); the cached dev host of this version; a download of it from the release (host-fetch.ts).
6
+ //
7
+ // An app with shaders/*.slang has them compiled into <dist>/effects, which the host reads through
8
+ // SK_EFFECTS_DIR, and recompiled on every change. A good rebuild reaches the running app as a message
9
+ // beside the HMR changes.json and replaces its effects in place; a host that predates that (its
10
+ // --sk-host-info has no `devReload: ["effects"]`) is restarted. A failed rebuild prints the
11
+ // diagnostics and keeps the last good build running.
6
12
 
7
- import { spawn } from "node:child_process";
13
+ import { spawn, type ChildProcess } from "node:child_process";
8
14
  import { existsSync, mkdirSync } from "node:fs";
9
15
  import { join } from "node:path";
16
+ import { compileAppEffects, EffectsError, formatProblems, hasAppEffects, writeEffectFiles } from "../effects/compile";
17
+ import { resolveCompiler } from "../effects/redslangc";
10
18
  import { loadAppConfig } from "./config";
19
+ import { devEffectsWatch, type EffectsWatch } from "./effects";
11
20
  import { cachedHost, checkHost, DEV_HOST, HostError, hostInfo, PKG_DIR, REPO_ROOT, CLI_VERSION } from "./host";
12
- import { fetchHost } from "./host-fetch";
21
+ import { fetchHost, type OnBytes } from "./host-fetch";
13
22
  import { currentPlatform, hostsFor, repoHostExe } from "./platform";
14
23
  import { colorEnabled, paint, progressFor } from "./term";
15
24
 
16
25
  export async function devHost(
17
- options: { exe?: string | undefined; log?: (line: string) => void } = {},
26
+ options: { exe?: string | undefined; log?: (line: string) => void; onBytes?: OnBytes } = {},
18
27
  ): Promise<string> {
19
28
  const log = options.log ?? console.log;
20
29
  const named = options.exe ?? process.env.SK_DEV_HOST;
21
30
  if (named) {
22
- if (!existsSync(named)) throw new HostError(`no dev host at ${named}`);
31
+ if (!existsSync(named)) throw new HostError(`no dev workload at ${named}`);
23
32
  return named;
24
33
  }
25
34
  if (REPO_ROOT) {
@@ -37,26 +46,70 @@ export async function devHost(
37
46
  if (!usable()) {
38
47
  if (process.env.SK_HOST_OFFLINE === "1") {
39
48
  throw new HostError(
40
- `no dev host: ${cached} is missing or unusable and SK_HOST_OFFLINE=1 forbids a download — ` +
41
- "pass --exe <dev host>" +
49
+ `no dev workload: ${cached} is missing or unusable and SK_HOST_OFFLINE=1 forbids a download — ` +
50
+ "pass --exe <dev workload>" +
42
51
  (REPO_ROOT
43
52
  ? `, or build one with \`${["cargo build -p sk-app", ...hostsFor(currentPlatform()).cargoTarget].join(" ")}\``
44
53
  : ""),
45
54
  );
46
55
  }
47
- await fetchHost({ variant: DEV_HOST, log });
56
+ await fetchHost({ variant: DEV_HOST, log, onBytes: options.onBytes });
48
57
  }
49
58
  return cached;
50
59
  }
51
60
 
52
- export async function dev(target: string, options: { exe?: string | undefined } = {}): Promise<number> {
61
+ function reloadsEffects(exe: string): boolean {
62
+ try {
63
+ return hostInfo(exe).devReload?.includes("effects") ?? false;
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+
69
+ export async function dev(
70
+ target: string,
71
+ options: { exe?: string | undefined; shaderCompiler?: string | undefined; color?: boolean | undefined } = {},
72
+ ): Promise<number> {
53
73
  const config = loadAppConfig(target);
54
- const c = paint(colorEnabled(process.stdout));
55
- const progress = progressFor(process.stderr);
56
- progress.step("dev host");
57
- const exe = await devHost({ ...options, log: (line) => progress.detail(line) }).finally(() => progress.end());
74
+ const c = paint(colorEnabled(process.stdout, options.color));
75
+ const progress = progressFor(process.stderr, colorEnabled(process.stderr, options.color));
76
+ progress.step("dev workload");
77
+ const exe = await devHost({
78
+ ...options,
79
+ log: (line) => progress.detail(line),
80
+ onBytes: (done, total) => progress.bytes(done, total),
81
+ }).catch((e: unknown) => {
82
+ progress.fail();
83
+ throw e;
84
+ });
58
85
  const dist = join(config.appRoot, "dist", ".sk-dev");
59
86
  mkdirSync(dist, { recursive: true });
87
+
88
+ const effectsDir = join(dist, "effects");
89
+ const overridden = !!process.env.SK_EFFECTS_DIR;
90
+ let compiler: string | null = null;
91
+ if (hasAppEffects(config.appRoot) && !overridden) {
92
+ progress.step("effects");
93
+ try {
94
+ compiler = (
95
+ await resolveCompiler({
96
+ named: options.shaderCompiler,
97
+ log: (line) => progress.info(line),
98
+ onBytes: (done, total) => progress.bytes(done, total),
99
+ })
100
+ ).path;
101
+ const effects = await compileAppEffects(config.appRoot, compiler);
102
+ writeEffectFiles(effectsDir, effects.files);
103
+ progress.detail(effects.compiled.map((e) => e.effect.name).join(", "));
104
+ } catch (e) {
105
+ progress.fail();
106
+ const text = e instanceof EffectsError ? formatProblems(e.problems) : e instanceof Error ? e.message : String(e);
107
+ console.error(`${c.red("effects")}: ${text}`);
108
+ if (existsSync(join(effectsDir, "effects.json"))) console.error(c.dim("the app starts with the last good build"));
109
+ }
110
+ }
111
+ progress.end();
112
+
60
113
  const watcher = spawn(process.execPath, [join(PKG_DIR, "build.ts"), config.entry, dist, "--watch"], {
61
114
  cwd: PKG_DIR,
62
115
  stdio: "inherit",
@@ -68,13 +121,42 @@ export async function dev(target: string, options: { exe?: string | undefined }
68
121
  if (Date.now() > deadline) throw new Error("bun build.ts produced no manifest within 2 minutes");
69
122
  await Bun.sleep(100);
70
123
  }
71
- console.log(`${c.green("▶")} ${c.bold(config.name)} ${c.dim(`dist ${dist}, host ${exe}`)}`);
72
- const host = spawn(exe, [], {
73
- stdio: "inherit",
74
- // SK_OWNER_PID: the host dies with this process however it ends (crates/sk-app/src/owner.rs).
75
- env: { ...process.env, SK_DIST: dist, SK_APP_ROOT: config.appRoot, SK_OWNER_PID: String(process.pid) },
76
- });
124
+ console.log(`${c.green("▶")} ${c.bold(config.name)} ${c.dim(`dist ${dist}, workload ${exe}`)}`);
125
+ const start = (): ChildProcess =>
126
+ spawn(exe, [], {
127
+ stdio: "inherit",
128
+ // SK_OWNER_PID: the host dies with this process however it ends (crates/sk-app/src/owner.rs).
129
+ env: {
130
+ ...process.env,
131
+ SK_DIST: dist,
132
+ SK_APP_ROOT: config.appRoot,
133
+ SK_OWNER_PID: String(process.pid),
134
+ ...(!overridden && existsSync(join(effectsDir, "effects.json")) ? { SK_EFFECTS_DIR: effectsDir } : {}),
135
+ },
136
+ });
137
+ let host = start();
138
+ let restarting = false;
139
+ let effectsWatch: EffectsWatch | null = null;
140
+ if (compiler) {
141
+ effectsWatch = devEffectsWatch({
142
+ appRoot: config.appRoot,
143
+ dist,
144
+ out: effectsDir,
145
+ compiler,
146
+ inPlace: reloadsEffects(exe),
147
+ restart: () => {
148
+ restarting = true;
149
+ host.kill();
150
+ },
151
+ log: (line) => console.log(`${c.cyan("↻")} ${line}`),
152
+ onError: (text) => {
153
+ console.error(`${c.red("effects")}: ${text}`);
154
+ console.error(c.dim("the app keeps running the last good build"));
155
+ },
156
+ });
157
+ }
77
158
  const stop = () => {
159
+ effectsWatch?.close();
78
160
  try {
79
161
  watcher.kill();
80
162
  } catch {}
@@ -86,7 +168,15 @@ export async function dev(target: string, options: { exe?: string | undefined }
86
168
  stop();
87
169
  process.exit(130);
88
170
  });
89
- const code: number = await new Promise((done) => host.on("exit", (c) => done(c ?? 0)));
171
+ const code: number = await new Promise((done) => {
172
+ const exited = (status: number | null) => {
173
+ if (!restarting) return done(status ?? 0);
174
+ restarting = false;
175
+ host = start();
176
+ host.on("exit", exited);
177
+ };
178
+ host.on("exit", exited);
179
+ });
90
180
  stop();
91
181
  return code;
92
182
  }
package/cli/effects.ts ADDED
@@ -0,0 +1,162 @@
1
+ // An app's own `--shading` effects in the sk commands: its shaders/*.slang compiled for every renderer
2
+ // (HLSL, WGSL, MSL), so a build made on one OS runs on all three. `sk build` puts them into the payload
3
+ // as `sk/effects/*` entries; `sk dev` writes them to dist/.sk-dev/effects and points the dev host there.
4
+
5
+ import { watch, writeFileSync, type FSWatcher } from "node:fs";
6
+ import { join } from "node:path";
7
+ import {
8
+ appShadersDir,
9
+ compileAppEffects,
10
+ EffectsError,
11
+ formatProblems,
12
+ hasAppEffects,
13
+ writeEffectFiles,
14
+ type AppEffects,
15
+ } from "../effects/compile";
16
+ import { resolveCompiler, type ShaderCompiler } from "../effects/redslangc";
17
+ import { ConfigError } from "./config";
18
+ import { KIND, NAMES, type SkpkInput } from "./skpk";
19
+ import type { Progress } from "./term";
20
+
21
+ export interface BuiltEffects {
22
+ effects: AppEffects;
23
+ compiler: ShaderCompiler;
24
+ /// The SKPK entries: effects.json and one file per effect and target.
25
+ inputs: SkpkInput[];
26
+ }
27
+
28
+ const enc = new TextEncoder();
29
+
30
+ const effectInputs = (effects: AppEffects): SkpkInput[] =>
31
+ effects.files.map((f) => ({ name: `${NAMES.EFFECTS_DIR}${f.name}`, kind: KIND.EFFECT, data: enc.encode(f.text) }));
32
+
33
+ export function effectsFailure(e: EffectsError): ConfigError {
34
+ const errors = e.problems.filter((p) => p.severity === "error");
35
+ return new ConfigError(
36
+ `the app's effect shaders do not compile (${errors.length} error${errors.length === 1 ? "" : "s"}):\n` +
37
+ formatProblems(e.problems),
38
+ );
39
+ }
40
+
41
+ /// Compiles the app's shaders into `out` and the payload entries; null for an app without any.
42
+ export async function buildAppEffects(options: {
43
+ appRoot: string;
44
+ out: string;
45
+ compiler?: string | undefined;
46
+ progress: Progress;
47
+ }): Promise<BuiltEffects | null> {
48
+ const { appRoot, out, progress } = options;
49
+ if (!hasAppEffects(appRoot)) return null;
50
+ progress.step("effects");
51
+ const compiler = await resolveCompiler({
52
+ named: options.compiler,
53
+ log: (line) => progress.info(line),
54
+ onBytes: (done, total) => progress.bytes(done, total),
55
+ });
56
+ let effects: AppEffects;
57
+ try {
58
+ effects = await compileAppEffects(appRoot, compiler.path);
59
+ } catch (e) {
60
+ throw e instanceof EffectsError ? effectsFailure(e) : e;
61
+ }
62
+ for (const line of formatProblems(effects.warnings).split("\n").filter(Boolean)) progress.warn(line);
63
+ writeEffectFiles(out, effects.files);
64
+ progress.detail(`${effects.compiled.map((c) => c.effect.name).join(", ")} · hlsl, wgsl, metal`);
65
+ return { effects, compiler, inputs: effectInputs(effects) };
66
+ }
67
+
68
+ export interface EffectsWatch {
69
+ close(): void;
70
+ }
71
+
72
+ /// Recompiles into `out` when a `.slang` under shaders/ changes, debounced. A failed compile keeps the
73
+ /// last good files and reports through `onError`.
74
+ export function watchAppEffects(options: {
75
+ appRoot: string;
76
+ out: string;
77
+ compiler: string;
78
+ onBuilt: (effects: AppEffects) => void;
79
+ onError: (message: string) => void;
80
+ debounceMs?: number;
81
+ }): EffectsWatch {
82
+ const dir = appShadersDir(options.appRoot);
83
+ let timer: ReturnType<typeof setTimeout> | null = null;
84
+ let running = false;
85
+ let again = false;
86
+ const rebuild = async () => {
87
+ if (running) {
88
+ again = true;
89
+ return;
90
+ }
91
+ running = true;
92
+ try {
93
+ const effects = await compileAppEffects(options.appRoot, options.compiler);
94
+ writeEffectFiles(options.out, effects.files);
95
+ options.onBuilt(effects);
96
+ } catch (e) {
97
+ options.onError(
98
+ e instanceof EffectsError ? formatProblems(e.problems) : e instanceof Error ? e.message : String(e),
99
+ );
100
+ } finally {
101
+ running = false;
102
+ if (again) {
103
+ again = false;
104
+ void rebuild();
105
+ }
106
+ }
107
+ };
108
+ let watcher: FSWatcher | null = null;
109
+ try {
110
+ watcher = watch(dir, { recursive: true }, (_event, file) => {
111
+ if (file && !String(file).endsWith(".slang")) return;
112
+ if (timer) clearTimeout(timer);
113
+ timer = setTimeout(() => {
114
+ timer = null;
115
+ void rebuild();
116
+ }, options.debounceMs ?? 120);
117
+ });
118
+ } catch (e) {
119
+ options.onError(`cannot watch ${dir}: ${e instanceof Error ? e.message : String(e)}`);
120
+ }
121
+ return {
122
+ close() {
123
+ if (timer) clearTimeout(timer);
124
+ watcher?.close();
125
+ },
126
+ };
127
+ }
128
+
129
+ /// The dev host polls this beside the HMR changes.json: `{"dir": "<compiled effects>"}`.
130
+ export const EFFECTS_CHANGED = "effects-changed.json";
131
+
132
+ /// `sk dev`'s shader watch. A good rebuild goes to a host that reloads effects in place as the
133
+ /// [`EFFECTS_CHANGED`] message, written after the files; a host older than that is restarted.
134
+ export function devEffectsWatch(options: {
135
+ appRoot: string;
136
+ dist: string;
137
+ out: string;
138
+ compiler: string;
139
+ inPlace: boolean;
140
+ restart: () => void;
141
+ log: (line: string) => void;
142
+ onError: (message: string) => void;
143
+ debounceMs?: number;
144
+ }): EffectsWatch {
145
+ return watchAppEffects({
146
+ appRoot: options.appRoot,
147
+ out: options.out,
148
+ compiler: options.compiler,
149
+ ...(options.debounceMs === undefined ? {} : { debounceMs: options.debounceMs }),
150
+ onBuilt: (effects) => {
151
+ const names = effects.compiled.map((e) => e.effect.name).join(", ");
152
+ if (options.inPlace) {
153
+ writeFileSync(join(options.dist, EFFECTS_CHANGED), `${JSON.stringify({ dir: options.out })}\n`);
154
+ options.log(`effects rebuilt (${names}) — reloaded in the running app`);
155
+ } else {
156
+ options.log(`effects rebuilt (${names}) — restarting the app`);
157
+ options.restart();
158
+ }
159
+ },
160
+ onError: options.onError,
161
+ });
162
+ }
package/cli/elf.ts CHANGED
@@ -104,9 +104,10 @@ export function stampElf(options: ElfStampOptions): ElfStampResult {
104
104
  const t0 = performance.now();
105
105
  const host = new Uint8Array(readFileSync(options.host));
106
106
  const machine = elfMachine(host);
107
- if (!machine) throw new Error(`${options.host} is not a 64-bit ELF executable — a Linux build needs a Linux sk-app`);
107
+ if (!machine)
108
+ throw new Error(`${options.host} is not a 64-bit ELF executable — a Linux build needs a Linux workload (sk-app)`);
108
109
  if (appendedPayload(host, host.length)) {
109
- throw new Error(`${options.host} is a packaged app, not a bare host (it already carries a payload)`);
110
+ throw new Error(`${options.host} is a packaged app, not a bare workload (it already carries a payload)`);
110
111
  }
111
112
  const offset = Math.ceil(host.length / PAGE) * PAGE;
112
113
  const unskewed = options.payload(0);