@skathi-native/cli 0.1.0-beta.2 → 0.1.0-beta.4

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 (50) hide show
  1. package/aot/host-globals.json +105 -0
  2. package/aot/native.ts +142 -0
  3. package/aot/pin.ts +67 -0
  4. package/aot/toolchain.lock.json +17 -0
  5. package/aot/transform.ts +69 -0
  6. package/check/codes.ts +19 -0
  7. package/check/command.ts +5 -3
  8. package/check/index.ts +44 -1
  9. package/cli/aot-build.ts +192 -0
  10. package/cli/build-app.ts +102 -31
  11. package/cli/config.ts +45 -102
  12. package/cli/create.ts +12 -54
  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 -148
  17. package/cli/host-fetch.ts +116 -54
  18. package/cli/host.ts +59 -39
  19. package/cli/sk.ts +440 -246
  20. package/cli/skpk.ts +6 -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/effects/compile.ts +952 -0
  25. package/effects/redslangc.json +23 -0
  26. package/effects/redslangc.ts +220 -0
  27. package/effects/sk.slang +558 -0
  28. package/package.json +6 -2
  29. package/templates/icon.png +0 -0
  30. package/templates/minimal/.vscode/settings.json +3 -0
  31. package/templates/minimal/src/App.vue +2 -2
  32. package/templates/showcase/.vscode/settings.json +3 -0
  33. package/templates/showcase/shaders/sheen.slang +23 -0
  34. package/templates/showcase/src/components/Titlebar.vue +7 -3
  35. package/templates/showcase/src/engine.ts +3 -26
  36. package/templates/showcase/src/pages/AudioPage.vue +6 -9
  37. package/templates/showcase/src/pages/FilesPage.vue +11 -15
  38. package/templates/showcase/src/pages/FormsPage.vue +2 -3
  39. package/templates/showcase/src/pages/GraphicsPage.vue +13 -15
  40. package/templates/showcase/src/pages/HomePage.vue +1 -2
  41. package/templates/showcase/src/pages/LocalePage.vue +2 -2
  42. package/templates/showcase/src/pages/MediaPage.vue +1 -1
  43. package/templates/showcase/src/pages/MotionPage.vue +1 -1
  44. package/templates/showcase/src/pages/NetworkPage.vue +6 -5
  45. package/templates/showcase/src/pages/PermissionsPage.vue +2 -2
  46. package/templates/showcase/src/pages/StoragePage.vue +23 -36
  47. package/templates/showcase/src/pages/StylesPage.vue +9 -1
  48. package/templates/showcase/src/pages/SystemPage.vue +3 -3
  49. package/templates/showcase/src/styles.css +2 -2
  50. package/templates/showcase/src/components/Missing.vue +0 -26
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);
package/cli/help.ts CHANGED
@@ -1,160 +1,68 @@
1
- import { OPTIONAL } from "./config";
2
- import { CLI_VERSION } from "./host";
1
+ // `sk --help` and `sk <command> --help`, laid out from the commander declarations in sk.ts: usage,
2
+ // what the command does, its subcommands, and its options under their group headings.
3
+
4
+ import type { Argument, CommandUnknownOpts, Help } from "@commander-js/extra-typings";
3
5
  import { paint, type Paint } from "./term";
4
6
 
5
- type Row = [string, string];
6
- type Section = { title: string; rows: Row[] };
7
+ export type Row = [string, string];
7
8
 
8
- const COMMANDS: Row[] = [
9
- ["create [dir]", "Start a new app"],
10
- ["dev [app]", "Run the app with hot reload"],
11
- ["build [app]", "Debug build: devtools on, not minified"],
12
- ["publish [app]", "Release build to ship"],
13
- ["run [app]", "Build, then start the app"],
14
- ["check [app]", "What breaks under AOT, and how well the code is typed"],
15
- ["explain <code>", "What a check code means and how to fix it"],
16
- ["host <command>", "Manage the prebuilt engine hosts"],
17
- ];
9
+ export function table(p: Paint, rows: Row[]): string[] {
10
+ const width = Math.max(...rows.map(([term]) => term.length));
11
+ return rows.map(([term, text]) => ` ${p.cyan(term.padEnd(width))} ${text}`);
12
+ }
18
13
 
19
- const BUILD: Section[] = [
20
- {
21
- title: "Options",
22
- rows: [
23
- ["--platform <os>", "windows, macos or linux (default: this OS)"],
24
- ["--without <parts>", `Leave engine parts out: ${OPTIONAL.join(", ")}`],
25
- ["--out <dir>", "Output directory (default: <app>/dist/debug or dist/release)"],
26
- ["--define K=V", "A Vite-style define; repeatable"],
27
- ["--config <path>", "An sk.config.json outside the app"],
28
- ["--sign <identity>", "macOS: codesign identity (- for ad hoc)"],
29
- ["--no-sign", "macOS: leave the bundle unsigned"],
30
- ["--console", "Windows: keep the console window"],
31
- ["--json", "Print the result as JSON"],
32
- ],
33
- },
34
- {
35
- title: "Advanced",
36
- rows: [
37
- ["--features <parts>", "Take only these engine parts"],
38
- ["--host <exe>", "Stamp this host instead of the cached one"],
39
- ["--payload-only", "Write dist/.sk/app.skpk and stop"],
40
- ["--aot-inputs", "Keep the typed sources for a later AOT compile"],
41
- ],
42
- },
43
- ];
14
+ export const section = (p: Paint, title: string, rows: Row[]): string[] =>
15
+ rows.length ? ["", p.bold(title), ...table(p, rows)] : [];
44
16
 
45
- const HELP: Record<string, { usage: string; about: string; sections: Section[] }> = {
46
- create: {
47
- usage: "sk create [dir] [options]",
48
- about: "Start a new app. In a terminal it asks for whatever the options leave open.",
49
- sections: [
50
- {
51
- title: "Options",
52
- rows: [
53
- ["--name <name>", "The app's name (default: the directory's)"],
54
- ["--template <name>", "showcase (every feature) or minimal"],
55
- ["--os <list>", "What to package for: windows, macos, linux"],
56
- ["--without <parts>", "Engine parts to leave out"],
57
- ["--devtools", "Keep devtools (F12) in published builds"],
58
- ["--pm <name>", "bun, npm, pnpm or yarn"],
59
- ["-y, --yes", "Ask nothing, take the defaults"],
60
- ],
61
- },
62
- ],
63
- },
64
- dev: {
65
- usage: "sk dev [app] [options]",
66
- about: "Run the app with hot reload: edits to .vue, .ts and .css apply without a restart.",
67
- sections: [{ title: "Options", rows: [["--exe <host>", "Run this dev host instead of the cached one"]] }],
68
- },
69
- build: {
70
- usage: "sk build [app] [options]",
71
- about: "A debug build in <app>/dist/debug: devtools (F12) on, the bundle not minified.",
72
- sections: BUILD,
73
- },
74
- publish: {
75
- usage: "sk publish [app] [options]",
76
- about: "The release build in <app>/dist/release: minified, devtools only if sk.config.json keeps them.",
77
- sections: BUILD,
78
- },
79
- run: {
80
- usage: "sk run [app] [options] [-- app args]",
81
- about: "sk build, then start the app with its output in this terminal.",
82
- sections: BUILD,
83
- },
84
- check: {
85
- usage: "sk check [app] [options]",
86
- about: "What in the bundle, app code and node_modules, breaks or slows down under AOT, and how well it is typed.",
87
- sections: [
88
- {
89
- title: "Options",
90
- rows: [
91
- ["--format json", "One JSON report on stdout (schema skathi.check)"],
92
- ["--deny-warnings", "Fail on warnings too"],
93
- ["--no-deps", "Only the app's own code"],
94
- ["--config <path>", "An sk.config.json outside the app"],
95
- ],
96
- },
97
- {
98
- title: "Exit codes",
99
- rows: [
100
- ["0", "Clean"],
101
- ["1", "Errors (or warnings with --deny-warnings)"],
102
- ["2", "Bad usage or config, or the check failed"],
103
- ],
104
- },
105
- ],
106
- },
107
- explain: {
108
- usage: "sk explain <code> | --list",
109
- about: "What a check code means, an example, and how to fix it.",
110
- sections: [{ title: "Options", rows: [["--list", "Every code"]] }],
111
- },
112
- host: {
113
- usage: "sk host <command>",
114
- about: "The engine host is the prebuilt executable an app is packed into. sk build fetches it on demand.",
115
- sections: [
116
- {
117
- title: "Commands",
118
- rows: [
119
- ["fetch", "Download this version's hosts into the local cache"],
120
- ["path", "Which host sk build would use"],
121
- ["info [exe]", "What a host says about itself"],
122
- ["add <exe>", "Verify a host and add it to the cache"],
123
- ["build", "In a Skathi checkout: build the hosts with cargo"],
124
- ],
125
- },
126
- ],
127
- },
17
+ const argTerm = (a: Argument) => {
18
+ const name = `${a.name()}${a.variadic ? "..." : ""}`;
19
+ return a.required ? `<${name}>` : `[${name}]`;
128
20
  };
129
21
 
130
- function rows(p: Paint, list: Row[]): string[] {
131
- const width = Math.max(...list.map(([name]) => name.length));
132
- return list.map(([name, text]) => ` ${p.cyan(name.padEnd(width))} ${text}`);
133
- }
134
-
135
- function sections(p: Paint, list: Section[]): string[] {
136
- return list.flatMap((s) => ["", p.bold(s.title), ...rows(p, s.rows)]);
22
+ function path(cmd: CommandUnknownOpts): string {
23
+ const names: string[] = [];
24
+ for (let c: CommandUnknownOpts | null = cmd; c; c = c.parent) names.unshift(c.name());
25
+ return names.join(" ");
137
26
  }
138
27
 
139
- const head = (p: Paint) => `${p.bold("sk")} ${p.dim(CLI_VERSION)} Skathi apps for Windows, macOS and Linux`;
140
-
141
- export function topHelp(color: boolean): string {
142
- const p = paint(color);
143
- return [
144
- head(p),
145
- "",
146
- `${p.bold("Usage")} sk <command> [app] [options]`,
147
- "",
148
- p.bold("Commands"),
149
- ...rows(p, COMMANDS),
150
- "",
151
- p.dim("Run sk <command> --help for its options. [app] is a directory (default: .)."),
152
- ].join("\n");
153
- }
28
+ const visible = (helper: Help, cmd: CommandUnknownOpts) =>
29
+ helper.visibleCommands(cmd).filter((c) => c.name() !== "help");
154
30
 
155
- export function commandHelp(command: string, color: boolean): string | null {
156
- const help = HELP[command];
157
- if (!help) return null;
31
+ export function helpFormat(color: boolean): Partial<Help> {
158
32
  const p = paint(color);
159
- return [`${p.bold("Usage")} ${help.usage}`, "", help.about, ...sections(p, help.sections)].join("\n");
33
+ return {
34
+ formatHelp(cmd, helper) {
35
+ const commands = visible(helper, cmd);
36
+ const options = helper.visibleOptions(cmd);
37
+ const usage = [
38
+ path(cmd),
39
+ ...cmd.registeredArguments.map(argTerm),
40
+ ...(commands.length ? ["<command>"] : []),
41
+ ...(options.length ? ["[options]"] : []),
42
+ ].join(" ");
43
+ const lines: string[] = [];
44
+ if (!cmd.parent) lines.push(`${p.bold(cmd.name())} ${p.dim(cmd.version() ?? "")} ${cmd.description()}`, "");
45
+ lines.push(`${p.bold("Usage")} ${usage}`);
46
+ if (cmd.parent && cmd.description()) lines.push("", cmd.description());
47
+ lines.push(
48
+ ...section(
49
+ p,
50
+ "Commands",
51
+ commands.map((c) => [
52
+ [c.name(), ...c.registeredArguments.map(argTerm), ...(visible(helper, c).length ? ["<command>"] : [])].join(
53
+ " ",
54
+ ),
55
+ c.summary() || c.description(),
56
+ ]),
57
+ ),
58
+ );
59
+ const groups = new Map<string, Row[]>();
60
+ for (const o of options) {
61
+ const title = (o.helpGroupHeading ?? "Options").replace(/:$/, "");
62
+ groups.set(title, [...(groups.get(title) ?? []), [o.flags, o.description]]);
63
+ }
64
+ for (const [title, rows] of groups) lines.push(...section(p, title, rows));
65
+ return `${lines.join("\n")}\n`;
66
+ },
67
+ };
160
68
  }
package/cli/host-fetch.ts CHANGED
@@ -8,10 +8,12 @@
8
8
 
9
9
  import { chmodSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
10
10
  import { dirname, join } from "node:path";
11
- import { partsLeftOut, VARIANTS, type Variant } from "./config";
11
+ import type { CompilerInfo } from "../aot/pin";
12
+ import type { Variant } from "./config";
12
13
  import {
13
14
  CLI_VERSION,
14
15
  cachedHost,
16
+ cacheRoot,
15
17
  HostError,
16
18
  resolveHost,
17
19
  type HostInfo,
@@ -35,14 +37,31 @@ export interface HostManifestEntry {
35
37
  info: HostInfo;
36
38
  }
37
39
 
40
+ /** sk-aot (`sk build --aot`), matching the release's hosts. */
41
+ export interface AotCompilerEntry {
42
+ platform: Platform;
43
+ arch: string;
44
+ /** The release asset: sk-aot, gzipped. */
45
+ file: string;
46
+ /** Of sk-aot itself, not of the gzip. */
47
+ sha256: string;
48
+ size: number;
49
+ /** What `sk-aot info` prints: the frontend, engine and builtin table it compiles for. */
50
+ info: CompilerInfo;
51
+ }
52
+
38
53
  export interface HostManifest {
39
54
  skathi: string;
40
55
  hosts: HostManifestEntry[];
56
+ aot?: AotCompilerEntry[];
41
57
  }
42
58
 
43
59
  export const hostAssetName = (platform: Platform, arch: string, variant: Variant): string =>
44
60
  `sk-host-${platform}-${arch}-${variant}${platform === "windows" ? ".exe" : ""}.gz`;
45
61
 
62
+ export const aotCompilerAssetName = (platform: Platform, arch: string): string =>
63
+ `sk-aot-${platform}-${arch}${platform === "windows" ? ".exe" : ""}.gz`;
64
+
46
65
  export const sha256 = (bytes: Uint8Array): string =>
47
66
  new Bun.CryptoHasher("sha256").update(new Uint8Array(bytes)).digest("hex");
48
67
 
@@ -58,6 +77,7 @@ export interface FetchOptions {
58
77
  platform?: Platform;
59
78
  arch?: string;
60
79
  log?: (line: string) => void;
80
+ onBytes?: OnBytes | undefined;
61
81
  }
62
82
 
63
83
  function source() {
@@ -69,20 +89,43 @@ function source() {
69
89
  return { repo, api, headers };
70
90
  }
71
91
 
72
- async function download(asset: ReleaseAsset, headers: Record<string, string>): Promise<Uint8Array<ArrayBuffer>> {
92
+ export type OnBytes = (done: number, total: number) => void;
93
+
94
+ async function download(
95
+ asset: ReleaseAsset,
96
+ headers: Record<string, string>,
97
+ onBytes?: OnBytes,
98
+ ): Promise<Uint8Array<ArrayBuffer>> {
73
99
  const res = await fetch(asset.url, { headers: { ...headers, Accept: "application/octet-stream" } });
74
100
  if (!res.ok) throw new HostError(`downloading ${asset.name}: HTTP ${res.status}`);
75
- return new Uint8Array(await res.arrayBuffer());
101
+ if (!onBytes || !res.body) return new Uint8Array(await res.arrayBuffer());
102
+ const total = Number(res.headers.get("content-length")) || asset.size;
103
+ const chunks: Uint8Array[] = [];
104
+ let done = 0;
105
+ for await (const chunk of res.body) {
106
+ chunks.push(chunk);
107
+ done += chunk.length;
108
+ onBytes(done, total);
109
+ }
110
+ const bytes = new Uint8Array(done);
111
+ let at = 0;
112
+ for (const chunk of chunks) {
113
+ bytes.set(chunk, at);
114
+ at += chunk.length;
115
+ }
116
+ return bytes;
76
117
  }
77
118
 
78
- /** Downloads the host for `variant` from the release of `version` into the local cache, checked against `hosts.json`. */
79
- export async function fetchHost(options: FetchOptions): Promise<{ path: string; info: HostInfo }> {
80
- const version = options.version ?? CLI_VERSION;
81
- const platform = options.platform ?? currentPlatform();
82
- const arch = options.arch ?? process.arch;
83
- const log = options.log ?? (() => {});
84
- const { repo, api, headers } = source();
119
+ interface Release {
120
+ tag: string;
121
+ repo: string;
122
+ headers: Record<string, string>;
123
+ manifest: HostManifest;
124
+ asset: (name: string) => ReleaseAsset | undefined;
125
+ }
85
126
 
127
+ async function readRelease(version: string): Promise<Release> {
128
+ const { repo, api, headers } = source();
86
129
  const tag = `v${version}`;
87
130
  const res = await fetch(`${api}/repos/${repo}/releases/tags/${tag}`, {
88
131
  headers: { ...headers, Accept: "application/vnd.github+json" },
@@ -100,6 +143,42 @@ export async function fetchHost(options: FetchOptions): Promise<{ path: string;
100
143
  const manifestAsset = asset(MANIFEST_NAME);
101
144
  if (!manifestAsset) throw new HostError(`release ${tag} of github.com/${repo} has no ${MANIFEST_NAME}`);
102
145
  const manifest = JSON.parse(new TextDecoder().decode(await download(manifestAsset, headers))) as HostManifest;
146
+ return { tag, repo, headers, manifest, asset };
147
+ }
148
+
149
+ /** Gunzips `name` and writes it to `dest` only when it matches `size` and `sha256`. */
150
+ async function cacheAsset(
151
+ release: Release,
152
+ name: string,
153
+ size: number,
154
+ digest: string,
155
+ dest: string,
156
+ log: (line: string) => void,
157
+ onBytes?: OnBytes,
158
+ ): Promise<void> {
159
+ const asset = release.asset(name);
160
+ if (!asset) throw new HostError(`release ${release.tag} lists ${name} in ${MANIFEST_NAME} but does not carry it`);
161
+ log(`downloading ${name} (${(asset.size / 1048576).toFixed(1)} MB) from github.com/${release.repo} ${release.tag}`);
162
+ const bytes = Bun.gunzipSync(await download(asset, release.headers, onBytes));
163
+ if (bytes.length !== size || sha256(bytes) !== digest) {
164
+ throw new HostError(`${name} does not match the sha256 in ${MANIFEST_NAME}; nothing was cached`);
165
+ }
166
+ mkdirSync(dirname(dest), { recursive: true });
167
+ const tmp = `${dest}.partial`;
168
+ writeFileSync(tmp, bytes);
169
+ if (process.platform !== "win32") chmodSync(tmp, 0o755);
170
+ rmSync(dest, { force: true });
171
+ renameSync(tmp, dest);
172
+ }
173
+
174
+ /** Downloads the workload for `variant` from the release of `version` into the local cache, checked against `hosts.json`. */
175
+ export async function fetchHost(options: FetchOptions): Promise<{ path: string; info: HostInfo }> {
176
+ const version = options.version ?? CLI_VERSION;
177
+ const platform = options.platform ?? currentPlatform();
178
+ const arch = options.arch ?? process.arch;
179
+ const log = options.log ?? (() => {});
180
+ const release = await readRelease(version);
181
+ const { manifest, tag } = release;
103
182
  const entry = manifest.hosts.find((h) => h.platform === platform && h.arch === arch && h.variant === options.variant);
104
183
  if (!entry) {
105
184
  const offered = manifest.hosts
@@ -107,69 +186,52 @@ export async function fetchHost(options: FetchOptions): Promise<{ path: string;
107
186
  .map((h) => h.variant)
108
187
  .join(", ");
109
188
  throw new HostError(
110
- `release ${tag} has no ${options.variant} host for ${platform}-${arch}` +
111
- (offered ? ` (it has: ${offered}); other variants are built from a checkout with sk host build` : ""),
189
+ `release ${tag} has no ${options.variant} workload for ${platform}-${arch}` +
190
+ (offered ? ` (it has: ${offered}); other variants are built from a checkout with sk workload build` : ""),
112
191
  );
113
192
  }
114
- const hostAsset = asset(entry.file);
115
- if (!hostAsset) throw new HostError(`release ${tag} lists ${entry.file} in ${MANIFEST_NAME} but does not carry it`);
116
-
117
- log(`downloading ${entry.file} (${(hostAsset.size / 1048576).toFixed(1)} MB) from github.com/${repo} ${tag}`);
118
- const bytes = Bun.gunzipSync(await download(hostAsset, headers));
119
- if (bytes.length !== entry.size || sha256(bytes) !== entry.sha256) {
120
- throw new HostError(`${entry.file} does not match the sha256 in ${MANIFEST_NAME}; nothing was cached`);
121
- }
122
-
123
193
  const dest = cachedHost(version, options.variant, platform);
124
- mkdirSync(dirname(dest), { recursive: true });
125
- const tmp = `${dest}.partial`;
126
- writeFileSync(tmp, bytes);
127
- if (process.platform !== "win32") chmodSync(tmp, 0o755);
128
- rmSync(dest, { force: true });
129
- renameSync(tmp, dest);
194
+ await cacheAsset(release, entry.file, entry.size, entry.sha256, dest, log, options.onBytes);
130
195
  writeFileSync(join(dirname(dest), "host.json"), JSON.stringify(entry.info, null, 2) + "\n");
131
- log(`cached ${options.variant} host ${version} → ${dest}`);
196
+ log(`cached ${options.variant} workload ${version} → ${dest}`);
132
197
  return { path: dest, info: entry.info };
133
198
  }
134
199
 
135
- /**
136
- * The published variant that can stand in for `variant`: devtools exactly as asked, and every other
137
- * part the app may use, with the fewest extra. A host with a part the app does not use only costs size.
138
- */
139
- export function standIn(variant: Variant, published: readonly Variant[] = VARIANTS): Variant | null {
140
- const off = new Set(partsLeftOut(variant));
141
- const fits = published.filter((v) => {
142
- const o = partsLeftOut(v);
143
- return o.includes("devtools") === off.has("devtools") && o.every((p) => off.has(p));
144
- });
145
- return fits.sort((a, b) => partsLeftOut(b).length - partsLeftOut(a).length)[0] ?? null;
200
+ /** Where the sk-aot of Skathi `version` is cached. */
201
+ export const cachedAotCompiler = (version: string, platform: Platform = currentPlatform()) =>
202
+ join(cacheRoot(platform), version, "aot", `sk-aot${platform === "windows" ? ".exe" : ""}`);
203
+
204
+ /** Downloads sk-aot from the release of `version` into the local cache, checked against `hosts.json`. */
205
+ export async function fetchAotCompiler(
206
+ options: Omit<FetchOptions, "variant"> = {},
207
+ ): Promise<{ path: string; info: CompilerInfo }> {
208
+ const version = options.version ?? CLI_VERSION;
209
+ const platform = options.platform ?? currentPlatform();
210
+ const arch = options.arch ?? process.arch;
211
+ const log = options.log ?? (() => {});
212
+ const release = await readRelease(version);
213
+ const entry = release.manifest.aot?.find((c) => c.platform === platform && c.arch === arch);
214
+ if (!entry) throw new HostError(`release ${release.tag} has no sk-aot for ${platform}-${arch}`);
215
+ const dest = cachedAotCompiler(version, platform);
216
+ await cacheAsset(release, entry.file, entry.size, entry.sha256, dest, log, options.onBytes);
217
+ writeFileSync(join(dirname(dest), "compiler.json"), JSON.stringify(entry.info, null, 2) + "\n");
218
+ log(`cached sk-aot ${version} → ${dest}`);
219
+ return { path: dest, info: entry.info };
146
220
  }
147
221
 
148
222
  /**
149
223
  * {@link resolveHost}, and when it finds nothing for this OS and no host was named, a download into the
150
- * cache first. A variant no release carries falls back to its {@link standIn}, with `substitute` saying
151
- * so. SK_HOST_OFFLINE=1 turns the download off.
224
+ * cache first. SK_HOST_OFFLINE=1 turns the download off.
152
225
  */
153
- export async function resolveOrFetchHost(options: ResolveOptions): Promise<ResolvedHost & { substitute?: string }> {
226
+ export async function resolveOrFetchHost(options: ResolveOptions): Promise<ResolvedHost> {
154
227
  try {
155
228
  return resolveHost(options);
156
229
  } catch (e) {
157
230
  const named = options.host ?? process.env.SK_HOST;
158
231
  const cross = (options.platform ?? currentPlatform()) !== currentPlatform();
159
232
  if (!(e instanceof HostError) || named || cross || process.env.SK_HOST_OFFLINE === "1") throw e;
160
- const instead = VARIANTS.includes(options.variant) ? null : standIn(options.variant);
161
- if (instead) {
162
- const host = await resolveOrFetchHost({ ...options, variant: instead });
163
- const extra = partsLeftOut(instead).length < partsLeftOut(options.variant).length;
164
- return {
165
- ...host,
166
- substitute:
167
- `no prebuilt ${options.variant} host: using ${instead}` +
168
- (extra ? ", which also carries parts this app leaves out (sk host build makes an exact one)" : ""),
169
- };
170
- }
171
233
  try {
172
- await fetchHost({ variant: options.variant, ...(options.log && { log: options.log }) });
234
+ await fetchHost({ variant: options.variant, onBytes: options.onBytes, ...(options.log && { log: options.log }) });
173
235
  } catch (f) {
174
236
  throw new HostError(`${e.message}\n(downloading it failed too: ${(f as Error).message})`);
175
237
  }