spine-rigc 1.9.4 → 2.0.1

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.
package/src/cli/shared.ts CHANGED
@@ -14,16 +14,19 @@
14
14
  * anything it imports: the bodies that reach the runtime are
15
15
  * `./spine_commands.ts`'s, which only `cli.ts` imports.
16
16
  */
17
- import { DEFAULT_PADDING, DEFAULT_PAGE_EDGES, DEFAULT_PAGE_SIZE } from '../atlas.ts';
17
+ import { type AtlasRegion, DEFAULT_PADDING, DEFAULT_PAGE_EDGES, DEFAULT_PAGE_SIZE, packAtlas, PAGE_EDGES, type PageEdges, pageFootprint } from '../atlas.ts';
18
+ import { copyAtlasPages, plannedPageCopies } from '../emit.ts';
19
+ import { CLI_DEFAULT_PROFILE, VALIDATE_PROFILES } from '../assertions/report.ts';
20
+ import type { AssertionProfile } from '../assertions/kinds.ts';
18
21
  import { MAX_CANDIDATES, MIN_CANDIDATES } from '../ballot.ts';
19
22
  import { BONEDIST_SPEC, IDENTITY_CORRESPONDENCE } from '../correspondence.ts';
20
23
  import { ANCHOR_MAX_RESIDUAL, ANCHOR_MAX_UNEXPLAINED, DEFAULT_HINGE_MAX, DEFAULT_HINGE_MIN, DEFAULT_MIN_LEVER_PX, DEFAULT_MIN_VISIBLE, DEFAULT_PASSES } from '../chainfit.ts';
21
24
  import { checkAgainstFrames, type CheckOptions, CheckPlates, type CheckReport } from '../check.ts';
22
- import { CompileError, droppedStateReason, type CompileOptions } from '../compile.ts';
25
+ import { compile, CompileError, droppedStateReason, type CompileOptions } from '../compile.ts';
23
26
  import { depthStepLevels, type FoldLimit, type TurnCeiling } from '../depth.ts';
24
27
  import { parseJsonWithPosition } from '../json-position.ts';
25
28
  import { RUNG_IDS } from '../ladder.ts';
26
- import { MODEL_DOCUMENT_FILE } from '../model.ts';
29
+ import { MODEL_DOCUMENT_FILE, modelDocument } from '../model.ts';
27
30
  import { DEFAULT_MAX_RESIDUAL, DEFAULT_SCALE_MAX, DEFAULT_SCALE_MIN } from '../pose.ts';
28
31
  import {
29
32
  CandidateAtlasError,
@@ -45,7 +48,7 @@ import { PoseError } from '../pose.ts';
45
48
  import { SpineRuntimeError, SPINE_SIDE_ABSENT } from '../spine_side.ts';
46
49
  import type { CompileResult, DroppedState } from '../types.ts';
47
50
  import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
48
- import { dirname, join, resolve } from 'node:path';
51
+ import { basename, dirname, join, resolve } from 'node:path';
49
52
 
50
53
 
51
54
  /**
@@ -550,43 +553,96 @@ export function meshBudget(rig: CompileResult['rig']): string {
550
553
  }
551
554
 
552
555
  /**
553
- * Where a pair of artifacts lives, given what the caller pointed at.
556
+ * What a path names as a build — the one statement of it every command that
557
+ * takes a build resolves through (issue #1046): the Spine skeleton, the atlas
558
+ * and the model document, each with whether it is there.
554
559
  *
555
- * Two shapes, because rigc's own output and a foreign export are named
556
- * differently and both have to be gateable. rigc writes `skeleton.json` +
557
- * `skeleton.atlas` into a directory. Everybody else writes whatever the editor
558
- * called the project, and the official examples are not even consistent with
559
- * themselves — `7-anticipation/export/` holds `sack-pro.json`, `spineboy/export/`
560
- * holds two skeletons and two atlases.
560
+ * ⭐ A build is a directory holding `skeleton.json` and `skeleton.atlas`, and
561
+ * `skeleton.model.json` beside them when rigc wrote it — or a `.json`
562
+ * skeleton named directly, a foreign export's. A directory holding no
563
+ * `skeleton.json`, or a path with nothing at it, is refused here, `nothing at
564
+ * <path>`, for every command alike: before this, `validate` read the missing
565
+ * file unguarded and died on an ENOENT and a stack (#1046), and `diff` on a
566
+ * directory on an EISDIR. What a command then needs beyond the skeleton is the
567
+ * command's to say — `spinePairOf` for the atlas, `resolveDrawable` for a
568
+ * rigc build drawn without one.
561
569
  *
562
- * ⚠️ When more than one atlas sits beside the skeleton, this refuses to choose.
563
- * Guessing by name would be wrong on the corpus that motivated it: `spineboy-ess`
564
- * shares a longer prefix with `spineboy-run.atlas` than with the `spineboy.atlas`
565
- * it actually uses, so the plausible heuristic picks the wrong file and every
566
- * attachment then resolves against the wrong pixels — silently, which is the
567
- * exact failure mode this tool exists to remove.
570
+ * Two shapes of target, because rigc's own output and a foreign export are
571
+ * named differently and both have to be gateable. rigc writes
572
+ * `skeleton.json` + `skeleton.atlas` into a directory. Everybody else writes
573
+ * whatever the editor called the project, and the official examples are not
574
+ * even consistent with themselves — `7-anticipation/export/` holds
575
+ * `sack-pro.json`, `spineboy/export/` holds two skeletons and two atlases.
576
+ *
577
+ * ⚠️ When more than one atlas sits beside a named skeleton, the atlas is not
578
+ * chosen: guessing by name would be wrong on the corpus that motivated it —
579
+ * `spineboy-ess` shares a longer prefix with `spineboy-run.atlas` than with
580
+ * the `spineboy.atlas` it actually uses, so the plausible heuristic picks the
581
+ * wrong file and every attachment then resolves against the wrong pixels —
582
+ * silently, which is the exact failure mode this tool exists to remove. The
583
+ * refusal is the atlas's (`atlasRefusal`), said by a command that reads one;
584
+ * `diff`, which reads the skeleton alone, is not refused for it.
568
585
  */
569
- export function resolveArtifacts(target: string, atlasFlag: string | undefined): { skeletonPath: string; atlasPath: string } {
586
+ export interface BuildFiles {
587
+ /** The Spine skeleton — `skeleton.json` in a directory, or the `.json` named — and there, or `resolveBuild` refuses. */
588
+ skeletonPath: string;
589
+ /** The atlas: `--atlas`, else `skeleton.atlas` in a directory, else the one `.atlas` beside a named `.json` (or `skeleton.atlas` there, when there is none). */
590
+ atlasPath: string;
591
+ /** `there` — a file at `atlasPath`; `absent` — none; `ambiguous` — several beside a named `.json` and no `--atlas`. */
592
+ atlas: 'there' | 'absent' | 'ambiguous';
593
+ /** What a command that reads the atlas says where `atlas` is not `there`. */
594
+ atlasRefusal: string;
595
+ /** `skeleton.model.json` beside the skeleton, or `null` where there is none (a Spine export). */
596
+ modelPath: string | null;
597
+ }
598
+
599
+ export function resolveBuild(target: string, atlasFlag: string | undefined): BuildFiles {
570
600
  const abs = resolve(target);
571
601
  if (!existsSync(abs)) throw new UsageError(`nothing at ${abs}`);
602
+ let skeletonPath: string;
603
+ let atlasPath: string;
604
+ let atlas: BuildFiles['atlas'];
605
+ let atlasRefusal: string;
572
606
  if (statSync(abs).isDirectory()) {
573
- return {
574
- skeletonPath: join(abs, 'skeleton.json'),
575
- atlasPath: atlasFlag ? resolve(atlasFlag) : join(abs, 'skeleton.atlas'),
576
- };
607
+ skeletonPath = join(abs, 'skeleton.json');
608
+ atlasPath = atlasFlag ? resolve(atlasFlag) : join(abs, 'skeleton.atlas');
609
+ atlas = existsSync(atlasPath) ? 'there' : 'absent';
610
+ atlasRefusal = `nothing at ${atlasPath}`;
611
+ } else {
612
+ if (!abs.endsWith('.json')) throw new UsageError(`${abs} is neither a directory nor a .json skeleton`);
613
+ skeletonPath = abs;
614
+ if (atlasFlag) {
615
+ atlasPath = resolve(atlasFlag);
616
+ atlas = existsSync(atlasPath) ? 'there' : 'absent';
617
+ atlasRefusal = `nothing at ${atlasPath}`;
618
+ } else {
619
+ const dir = dirname(abs);
620
+ const atlases = readdirSync(dir)
621
+ .filter((f) => f.endsWith('.atlas'))
622
+ .sort();
623
+ atlasPath = join(dir, atlases.length === 1 ? atlases[0] : 'skeleton.atlas');
624
+ atlas = atlases.length === 1 ? 'there' : atlases.length === 0 ? 'absent' : 'ambiguous';
625
+ atlasRefusal =
626
+ atlases.length === 0
627
+ ? `no .atlas beside ${abs}; name one with --atlas <path>`
628
+ : `${atlases.length} atlases beside ${abs} (${atlases.join(', ')}); name the right one with --atlas <path> ` +
629
+ '— guessing by filename is how an attachment quietly resolves against the wrong page';
630
+ }
577
631
  }
578
- if (!abs.endsWith('.json')) throw new UsageError(`${abs} is neither a directory nor a .json skeleton`);
579
- if (atlasFlag) return { skeletonPath: abs, atlasPath: resolve(atlasFlag) };
580
- const dir = dirname(abs);
581
- const atlases = readdirSync(dir)
582
- .filter((f) => f.endsWith('.atlas'))
583
- .sort();
584
- if (atlases.length === 1) return { skeletonPath: abs, atlasPath: join(dir, atlases[0]) };
585
- if (atlases.length === 0) throw new UsageError(`no .atlas beside ${abs}; name one with --atlas <path>`);
586
- throw new UsageError(
587
- `${atlases.length} atlases beside ${abs} (${atlases.join(', ')}); name the right one with --atlas <path> ` +
588
- '— guessing by filename is how an attachment quietly resolves against the wrong page',
589
- );
632
+ if (!existsSync(skeletonPath)) throw new UsageError(`nothing at ${skeletonPath}`);
633
+ const model = join(dirname(skeletonPath), MODEL_DOCUMENT_FILE);
634
+ return { skeletonPath, atlasPath, atlas, atlasRefusal, modelPath: existsSync(model) ? model : null };
635
+ }
636
+
637
+ /**
638
+ * The build's Spine pair, for a command that reads both files — the round
639
+ * trip (`validate`, `bench`), `bonedist`, `preview`, `vote` — refused naming
640
+ * the atlas where it is not there (`atlasRefusal`); the skeleton is
641
+ * `resolveBuild`'s.
642
+ */
643
+ export function spinePairOf(build: BuildFiles): { skeletonPath: string; atlasPath: string; atlasDir: string } {
644
+ if (build.atlas !== 'there') throw new UsageError(build.atlasRefusal);
645
+ return { skeletonPath: build.skeletonPath, atlasPath: build.atlasPath, atlasDir: dirname(build.atlasPath) };
590
646
  }
591
647
 
592
648
  /**
@@ -701,7 +757,7 @@ export function writeJson(target: string, body: unknown): void {
701
757
  // behind, and it keeps `--out` meaning one thing per command instead of naming
702
758
  // the build directory on the way in and the pictures on the way out.
703
759
 
704
- /** Both commands' shared front door: which artifact, and what is in it. */
760
+ /** `--candidate`, the one build `preview`, `bench` and `bonedist` read the Spine pair of (`spinePairOf`). */
705
761
  export function resolveViewable(flags: Record<string, string>): {
706
762
  skeletonPath: string;
707
763
  atlasPath: string;
@@ -710,11 +766,7 @@ export function resolveViewable(flags: Record<string, string>): {
710
766
  if (flags.candidate === undefined) {
711
767
  throw new UsageError('needs --candidate <dir | skeleton.json> — the directory `build --out` wrote');
712
768
  }
713
- const { skeletonPath, atlasPath } = resolveArtifacts(flags.candidate, flags.atlas);
714
- for (const path of [skeletonPath, atlasPath]) {
715
- if (!existsSync(path)) throw new UsageError(`nothing at ${path}`);
716
- }
717
- return { skeletonPath, atlasPath, atlasDir: dirname(atlasPath) };
769
+ return spinePairOf(resolveBuild(flags.candidate, flags.atlas));
718
770
  }
719
771
 
720
772
  /** What `render` and `check` say where a rigc build's atlas is not there (issue #1020). */
@@ -735,22 +787,18 @@ export const ATLAS_ABSENT = 'a build the core poses from a rigc-compiled/2 or /3
735
787
  * read, as it always was: the core holds it to the document's `pages` and
736
788
  * draws through spine-core, saying why, when it is not the one the build
737
789
  * wrote (#1016), and `check` reads its `scale:` lines for the texture note.
738
- * Everywhere else this is `resolveViewable`'s refusal, word for word.
790
+ * Everywhere else this is `resolveViewable`'s refusal, word for word — both
791
+ * read the build off `resolveBuild` (issue #1046).
739
792
  */
740
793
  export function resolveDrawable(target: string, atlasFlag: string | undefined): { skeletonPath: string; atlasPath: string; atlasText: string | null } {
741
- const abs = resolve(target);
742
- if (atlasFlag === undefined && existsSync(abs)) {
743
- const directory = statSync(abs).isDirectory();
744
- const skeletonPath = directory ? join(abs, 'skeleton.json') : abs;
745
- const dir = dirname(skeletonPath);
746
- const atlasPath = join(dir, 'skeleton.atlas');
747
- const noAtlas = directory ? !existsSync(atlasPath) : abs.endsWith('.json') && !readdirSync(dir).some((f) => f.endsWith('.atlas'));
748
- if (noAtlas && existsSync(join(dir, MODEL_DOCUMENT_FILE))) {
749
- if (!existsSync(skeletonPath)) throw new UsageError(`nothing at ${skeletonPath}`);
794
+ const build = resolveBuild(target, atlasFlag);
795
+ const { skeletonPath, atlasPath } = build;
796
+ if (atlasFlag === undefined && build.atlas === 'absent') {
797
+ if (build.modelPath !== null) {
750
798
  return { skeletonPath, atlasPath, atlasText: null };
751
799
  }
752
800
  }
753
- const { skeletonPath, atlasPath } = resolveViewable({ candidate: target, ...(atlasFlag === undefined ? {} : { atlas: atlasFlag }) });
801
+ if (build.atlas !== 'there') throw new UsageError(build.atlasRefusal);
754
802
  return { skeletonPath, atlasPath, atlasText: readFileSync(atlasPath, 'utf8') };
755
803
  }
756
804
 
@@ -1104,6 +1152,23 @@ const FLAG_VALUES: Record<string, string> = {
1104
1152
  dir: '<path>',
1105
1153
  };
1106
1154
 
1155
+ /**
1156
+ * A command whose body differs by entry (issue #1060): the body the entry that
1157
+ * links none of spine-core runs under the same name, as that entry's page
1158
+ * documents it. `runtime.for` still says what the full entry's body runs
1159
+ * through the runtime for, and the full entry's page is the command's own
1160
+ * fields, unchanged; the second entry's page takes `usage`, `flags` (the
1161
+ * command's own where this states none) and `notes` from here
1162
+ * (`entryCommands`). Registered by `./core_commands.ts`'s `CORE_ENTRY_RUNS`,
1163
+ * which only the second entry registers — `RC26` holds each body to the
1164
+ * closure of the module that registers it.
1165
+ */
1166
+ interface CoreBody {
1167
+ usage: string[];
1168
+ flags?: string[];
1169
+ notes: string[];
1170
+ }
1171
+
1107
1172
  interface CommandDoc {
1108
1173
  name: string;
1109
1174
  /** One or more invocation forms, each already spelling the command name. */
@@ -1156,7 +1221,7 @@ interface CommandDoc {
1156
1221
  * import graph of the module whose bodies register the command, and a
1157
1222
  * mark that disagrees with the graph is red by name.
1158
1223
  */
1159
- runtime: false | { for: string };
1224
+ runtime: false | { for: string; core?: CoreBody };
1160
1225
  /**
1161
1226
  * Whether the command exists for the Spine format — reads, writes, compares
1162
1227
  * or embeds Spine skeleton data as the whole of what it does with it — so
@@ -1169,7 +1234,32 @@ interface CommandDoc {
1169
1234
  export const COMMANDS: CommandDoc[] = [
1170
1235
  {
1171
1236
  name: 'build',
1172
- runtime: { for: 'the gate round-trips every build through it before anything is written' },
1237
+ runtime: {
1238
+ for: 'the gate round-trips every build through it before anything is written',
1239
+ core: {
1240
+ usage: [
1241
+ 'rigc build --rig <path> --motion <path> --out <dir> [--manifest <path>] [--images <dir>] [--profile spine|spine-html] [--copy-images] (the same files the round-tripped build writes; gated without spine-core — see build --help)',
1242
+ `rigc build … --pack [--page-size ${DEFAULT_PAGE_SIZE}] [--padding ${DEFAULT_PADDING}] [--page-edges pot|free] (parts onto shared pages, written into --out)`,
1243
+ 'rigc build … --atlas-in <skeleton.atlas> (resolve the parts against a pack somebody already made)',
1244
+ 'rigc build --cut <name> --cuts <cuts.json>',
1245
+ ],
1246
+ notes: [
1247
+ 'this entry\'s build writes what the entry that links spine-core writes — skeleton.json,',
1248
+ 'skeleton.atlas, skeleton.model.json and the pages --pack or --copy-images put in',
1249
+ '--out — by the same body, and only when no assertion fails. Its gate is not the round',
1250
+ 'trip through spine-core, which this entry links none of. What runs instead: the model',
1251
+ 'side over the document (every assertion moved off the round trip), and the round',
1252
+ 'trip\'s own rules restated over the text the emitter wrote — A01, A02, A05, A07, A16,',
1253
+ 'A31, A35, and A18 over a second, independent compile\'s skeleton, atlas and document.',
1254
+ 'What does not run is A00_ROUNDTRIP_PARSE, spine-core\'s parse: it reports SKIP naming',
1255
+ 'spine-core. The report\'s last line says which ran here and which did not. A00 runs',
1256
+ 'in build and validate on the entry that links spine-core: installed, the same `rigc`',
1257
+ 'runs that entry once @esotericsoftware/spine-core is installed beside the package',
1258
+ '(`rigc --version` names the entry that ran); from a source checkout, it is',
1259
+ '`bun cli.ts`.',
1260
+ ],
1261
+ },
1262
+ },
1173
1263
  spineFormat: true,
1174
1264
  usage: [
1175
1265
  'rigc build --rig <path> --motion <path> --out <dir> [--manifest <path>] [--images <dir>] [--profile spine|spine-html] [--copy-images]',
@@ -1485,9 +1575,9 @@ export const COMMANDS: CommandDoc[] = [
1485
1575
  /** Every command's name, in the order the usage lists them — what the full entry dispatches over. */
1486
1576
  export const KNOWN_COMMANDS = COMMANDS.map((c) => c.name);
1487
1577
 
1488
- /** `rigc <command> --help`: that command's own usage line(s) and flag table. */
1489
- export function commandHelp(name: string): string {
1490
- const doc = COMMANDS.find((c) => c.name === name);
1578
+ /** `rigc <command> --help`: that command's own usage line(s) and flag table, as the entry running it (`docs`) documents it. */
1579
+ export function commandHelp(name: string, docs: readonly CommandDoc[] = COMMANDS): string {
1580
+ const doc = docs.find((c) => c.name === name);
1491
1581
  if (!doc) throw new Error(`internal: no help text for command "${name}"`);
1492
1582
  const keys = [...doc.flags, 'help'];
1493
1583
  const value = (key: string): string | undefined => doc.overrides?.[key]?.value ?? FLAG_VALUES[key];
@@ -1650,7 +1740,8 @@ export function usageText(docs: readonly CommandDoc[], checkout: string, rules:
1650
1740
  '',
1651
1741
  checkout === 'cli.ts'
1652
1742
  ? '(from a source checkout: `bun cli.ts <command>` is the same as `rigc <command>`)'
1653
- : `(from a source checkout: \`bun ${checkout} <command>\` runs the commands below, which link nothing of spine-core; \`bun cli.ts <command>\` runs every command)`,
1743
+ : `(installed: \`rigc\` runs this entry where @esotericsoftware/spine-core is not installed beside the package, and every command once it is; ` +
1744
+ `from a source checkout: \`bun ${checkout} <command>\` runs the commands below, which link nothing of spine-core; \`bun cli.ts <command>\` runs every command)`,
1654
1745
  '',
1655
1746
  'usage:',
1656
1747
  ...docs.flatMap((c) => c.usage.map((u) => ` ${u}`)),
@@ -1672,6 +1763,417 @@ export function usageText(docs: readonly CommandDoc[], checkout: string, rules:
1672
1763
  return lines.join('\n');
1673
1764
  }
1674
1765
 
1766
+ // ---------------------------------------------------------------------------
1767
+ // what both `build` bodies share (issue #1060): the flags, the header and the compile's report
1768
+ // ---------------------------------------------------------------------------
1769
+
1770
+ /**
1771
+ * Read `--profile`, defaulting to `spine` — see `CLI_DEFAULT_PROFILE`.
1772
+ *
1773
+ * An unknown name is a usage error rather than a silent fallback, and that
1774
+ * matters in both directions: a typo used to re-apply the strictest rulebook to
1775
+ * data the caller was trying to exempt, and it would now drop the policy layer
1776
+ * from a caller who typed `--profile spine-htlm` and believes they asked for it.
1777
+ * Neither is something to discover from a green.
1778
+ */
1779
+ export function readProfile(flags: Record<string, string>): AssertionProfile {
1780
+ const raw = flags.profile;
1781
+ if (raw === undefined) return CLI_DEFAULT_PROFILE;
1782
+ const found = VALIDATE_PROFILES.find((p) => p === raw);
1783
+ if (!found) throw new UsageError(`--profile ${JSON.stringify(raw)}; known profiles: ${VALIDATE_PROFILES.join(', ')}`);
1784
+ return found;
1785
+ }
1786
+
1787
+ /**
1788
+ * Read one non-negative integer flag, or its default.
1789
+ *
1790
+ * A usage error rather than a `NaN` that reaches the packer: `--padding two`
1791
+ * would otherwise place every region at NaN and write a blank page, which is a
1792
+ * green build and an empty picture.
1793
+ */
1794
+ export function readIntFlag(flags: Record<string, string>, name: string, fallback: number): number {
1795
+ const raw = flags[name];
1796
+ if (raw === undefined) return fallback;
1797
+ if (!/^\d+$/.test(raw)) throw new UsageError(`--${name} takes a non-negative integer, got ${JSON.stringify(raw)}`);
1798
+ return Number(raw);
1799
+ }
1800
+
1801
+ /**
1802
+ * Read `--page-edges`, or its default — `pot`.
1803
+ *
1804
+ * An unknown value is a usage error for `readProfile`'s reason: a typo that fell
1805
+ * back to `pot` would hand the caller who asked for the smaller page the bigger
1806
+ * one, green, and say nothing.
1807
+ */
1808
+ export function readPageEdges(flags: Record<string, string>): PageEdges {
1809
+ const raw = flags['page-edges'];
1810
+ if (raw === undefined) return DEFAULT_PAGE_EDGES;
1811
+ const found = PAGE_EDGES.find((e) => e === raw);
1812
+ if (!found) throw new UsageError(`--page-edges ${JSON.stringify(raw)}; known values: ${PAGE_EDGES.join(', ')}`);
1813
+ return found;
1814
+ }
1815
+
1816
+ /**
1817
+ * The rectangle a region occupies **on its page**, for a line that has already
1818
+ * said where the region is — and the empty string where the page rectangle is
1819
+ * the one `bounds:` already states.
1820
+ *
1821
+ * ## The fact no surface an author reads carried (issue #718)
1822
+ *
1823
+ * The atlas line beside this clause prints the DRAWING's size, because that is
1824
+ * what an attachment's width and height mean. A packer that turned the drawing a
1825
+ * quarter to fit it wrote `bounds:` in the drawing's orientation too. So an
1826
+ * author holding the pack and the build report had neither end of the rectangle
1827
+ * they have to cut out of the page to measure a part against a rendered frame —
1828
+ * and the one place rigc printed it was `A06`'s overlap text, reachable only
1829
+ * under `--profile spine-html`. The knowledge was in the tree the whole time:
1830
+ * `pageFootprint` has derived this rectangle for every reader of it since issue
1831
+ * #579, and nothing an author reads said it.
1832
+ *
1833
+ * ⚠️ **The condition is `pageFootprint`'s own answer, not a second reading of
1834
+ * `degrees`.** Re-spelling that predicate here is the exact duplication #579 was
1835
+ * filed on — four readers derived this rectangle and two derived it wrongly — so
1836
+ * the clause asks the function whether its answer differs from the `bounds:`
1837
+ * line, and prints only then.
1838
+ *
1839
+ * 🔸 A consequence worth stating rather than leaving to be discovered: a region
1840
+ * whose KEPT rectangle is square is silent here, because a quarter turn leaves
1841
+ * its footprint the same two numbers and there is nothing the pack does not
1842
+ * already say. The general rule — `bounds` is the unturned size, the footprint
1843
+ * is its transpose at `rotate: 90` and `rotate: 270`, and which way to turn the
1844
+ * rectangle to recover the drawing — belongs to an author's own reading and is
1845
+ * stated in `docs/AUTHORING.md` §0.2, which holds for every region including
1846
+ * that one.
1847
+ */
1848
+ function pageRectangle(region: AtlasRegion): string {
1849
+ const foot = pageFootprint(region);
1850
+ return foot.width === region.width && foot.height === region.height
1851
+ ? ''
1852
+ : `, occupies ${foot.width}x${foot.height}`;
1853
+ }
1854
+
1855
+ /**
1856
+ * `build`'s invocation, read and refused before anything compiles — the cut,
1857
+ * the profile, and the flag combinations that disagree about one question —
1858
+ * for both bodies of the command (issue #1060): the one that writes the Spine
1859
+ * pair (`./spine_commands.ts`) and the one that writes the model document
1860
+ * alone (`./core_commands.ts`). Moved here unchanged from `cmdBuild`.
1861
+ */
1862
+ export function readBuildInvocation(flags: Record<string, string>): { label: string; opts: CompileOptions; profile: AssertionProfile; packing: boolean } {
1863
+ const { label, opts } = resolveCut(flags);
1864
+ const profile = readProfile(flags);
1865
+ const packing = flags.pack !== undefined;
1866
+ // Two combinations are refused rather than silently resolved, because in each
1867
+ // one the two flags disagree about a single question and there is no answer
1868
+ // that is not a guess about which the caller meant. (There were three until
1869
+ // issue #266 — see the note below the second.)
1870
+ if (packing && opts.atlasInPath !== undefined) {
1871
+ throw new UsageError(
1872
+ '--pack and --atlas-in are opposite directions through the same door: --pack MAKES an atlas out of the ' +
1873
+ 'loose parts, --atlas-in resolves the parts against one somebody already made. Pick one',
1874
+ );
1875
+ }
1876
+ if (packing && flags['copy-images'] !== undefined) {
1877
+ throw new UsageError(
1878
+ '--pack already writes self-contained pages into --out (that is what packing is), and --copy-images copies ' +
1879
+ 'the loose part PNGs, which a packed atlas does not reference. Drop --copy-images',
1880
+ );
1881
+ }
1882
+ // The copy itself happens after the gate (below). The header has to know NOW,
1883
+ // because the skeleton text the gate reads is the skeleton text that is written
1884
+ // — `skeleton.images` says where the parts will be (issue #370).
1885
+ if (flags['copy-images'] !== undefined) opts.copyImages = true;
1886
+ // `--pack --profile spine-html` used to be the third refusal here, because
1887
+ // A06's coverage clause was "one part per page" flat and a legitimate pack
1888
+ // arrived at the gate reading as a defect. Since issue #266's second follow-up
1889
+ // that clause is "one part per page OR a tiling page", so the combination is
1890
+ // now a build like any other — and it is the only one that puts the renderer's
1891
+ // own rulebook over shared-page sampling.
1892
+ if (!packing) {
1893
+ for (const name of ['page-size', 'padding', 'page-edges'] as const) {
1894
+ if (flags[name] !== undefined) throw new UsageError(`--${name} only means something with --pack`);
1895
+ }
1896
+ }
1897
+ return { label, opts, profile, packing };
1898
+ }
1899
+
1900
+ /** The lines a build opens on: the cut, then its two input files. */
1901
+ export function printBuildHeader(label: string, opts: CompileOptions): void {
1902
+ console.log(`rigc build ${label}`);
1903
+ // Named explicitly and on their own lines rather than folded into the header
1904
+ // above: with two input files, a header that names only one of them (the rig,
1905
+ // historically) reads as though it were the one at fault whenever the error
1906
+ // that follows actually comes from the other.
1907
+ console.log(` .. rig ${opts.rigPath}`);
1908
+ console.log(` .. motion ${opts.motionPath}`);
1909
+ }
1910
+
1911
+ /** What a compile measured, as `build` reports it before its gate: the parts, the dropped states, the absent parts, the meshes, the physics. */
1912
+ export function printCompiled(opts: CompileOptions, result: Pick<CompileResult, 'images' | 'droppedStates' | 'absentParts' | 'meshes' | 'rig' | 'physics'>): void {
1913
+ if (opts.atlasInPath !== undefined) console.log(` .. atlas-in ${opts.atlasInPath}`);
1914
+ console.log(` .. ${result.images.length} part page(s):`);
1915
+ for (const img of result.images) {
1916
+ // An imported part says where on the page it came from, because "resolved
1917
+ // against a region" is the claim `--atlas-in` makes and a line that only
1918
+ // repeated the page filename would look identical for all of them. A page
1919
+ // that declares a `scale:` also says so and shows the texels it was read
1920
+ // from: the size on the left is the DRAWING's and the rectangle is the
1921
+ // pack's, and issue #267 is the report that printed the second as the first.
1922
+ //
1923
+ // `pageRectangle` closes the line's last silence (issue #718), and it is
1924
+ // placed LAST rather than beside the turn it follows from, which is where
1925
+ // the card put it. The two clauses collide nowhere else, and the collision
1926
+ // is real: `scale 0.5 (373x106 texels)` is itself a size, so
1927
+ // `rotate 90, occupies 106x373 scale 0.5 (…)` reads as though the footprint
1928
+ // were the scaled quantity. As a trailing clause of the whole location
1929
+ // phrase it is unambiguous with a `scale:` line and identical to the card's
1930
+ // wording without one, which is every pack that has no `scale:` to state.
1931
+ const where =
1932
+ img.atlas === undefined
1933
+ ? img.page
1934
+ : `${img.page} @ ${img.atlas.x},${img.atlas.y}${img.atlas.degrees ? ` rotate ${img.atlas.degrees}` : ''}` +
1935
+ (img.atlasScale === undefined
1936
+ ? ''
1937
+ : ` scale ${img.atlasScale} (${img.atlas.originalWidth}x${img.atlas.originalHeight} texels)`) +
1938
+ pageRectangle(img.atlas);
1939
+ console.log(` .. ${img.region.padEnd(24)} ${img.width}x${img.height} <- ${where}`);
1940
+ }
1941
+ for (const d of result.droppedStates) console.log(dropLine(d));
1942
+ // "The optional slots are optional" is a claim about this code path, so this
1943
+ // code path says which ones it left out rather than being silently right.
1944
+ for (const a of result.absentParts) {
1945
+ console.log(` ABSENT ${a.slot}: ${a.why} — slot not emitted`);
1946
+ }
1947
+ for (const m of result.meshes) {
1948
+ console.log(
1949
+ ` MESH ${m.slot.padEnd(12)} ${m.kind.padEnd(8)} ${m.vertices} vertices / ${m.triangles} triangles ` +
1950
+ `${meshBudget(result.rig)} bones=[${m.bones.join(', ')}] attachments=[${m.attachments.join(', ')}]${meshFit(m)}` +
1951
+ meshDepthNote(m) +
1952
+ meshInfluenceNote(m),
1953
+ );
1954
+ }
1955
+ for (const ph of result.physics) {
1956
+ console.log(
1957
+ ` PHYS ${ph.name.padEnd(12)} bone=${ph.bone.padEnd(14)} components=[${ph.components.join(', ')}] ` +
1958
+ `mix=${ph.mix}${ph.drivesMesh ? ' <- drives a mesh: its canvas re-rasterises while the spring settles' : ''}`,
1959
+ );
1960
+ }
1961
+
1962
+ }
1963
+
1964
+ /**
1965
+ * An atlas text to gate INSTEAD of the compile's own, with the second, independent
1966
+ * emit A18 compares it against.
1967
+ *
1968
+ * `--pack` is the only caller. A packed build is gated twice on purpose — once as
1969
+ * compiled (which is the gate that reads the loose PNGs, so `A06`'s size-vs-file
1970
+ * clause still measures the art R5 measures) and once as packed (which is the pair
1971
+ * that actually ships). Handing the second pass its texts rather than re-deriving
1972
+ * them here keeps `runGate` ignorant of what a pack is.
1973
+ */
1974
+ interface AtlasOverride {
1975
+ text: string;
1976
+ again: string;
1977
+ }
1978
+
1979
+ /**
1980
+ * What one gate of `build` is handed (issue #1060): the compile, the atlas text
1981
+ * it gates (the compile's own, or the packed one), the directory its pages
1982
+ * resolve against, the document `build` writes, and a second, independent
1983
+ * compile's three texts for A18 — the same arguments `cli.ts build` has always
1984
+ * handed `validate()`, so each entry's gate reads one statement of them.
1985
+ */
1986
+ export interface BuildGateInput {
1987
+ result: CompileResult;
1988
+ atlasText: string;
1989
+ atlasDir: string;
1990
+ modelText: string;
1991
+ reEmit: { skeletonText: string; atlasText: string; modelText: string };
1992
+ profile: AssertionProfile;
1993
+ }
1994
+
1995
+ /**
1996
+ * The gate a `build` body runs, by entry (issue #1060): `cli.ts`'s is the
1997
+ * round trip through spine-core (`./spine_commands.ts`), `cli_core.ts`'s the
1998
+ * model side and the rules restated over the emitted text
1999
+ * (`./core_commands.ts`). Everything else `build` does — compile, the
2000
+ * document, `--copy-images`, `--pack`, the writes and their order — is
2001
+ * `runBuild`'s, written once, so the two entries write the same build.
2002
+ */
2003
+ export interface BuildGate {
2004
+ /** The line before the first gate's report, naming what judges it. */
2005
+ heading: (profile: AssertionProfile) => string;
2006
+ /** Run the gate, print its report, return the failure count. */
2007
+ run: (input: BuildGateInput) => number;
2008
+ /** The command a green build ends by naming, for its own `--out`. */
2009
+ look: (outDir: string) => string;
2010
+ }
2011
+
2012
+ function runGate(
2013
+ gate: BuildGate,
2014
+ result: CompileResult,
2015
+ modelText: string,
2016
+ opts: CompileOptions,
2017
+ profile: AssertionProfile,
2018
+ /** The atlas text `build` writes for a compile's own — `--copy-images` renames the pages — which the second compile's document is spelled from, as `modelText` was (issue #1016). */
2019
+ written: (atlasText: string) => string,
2020
+ atlas?: AtlasOverride,
2021
+ ): number {
2022
+ // The determinism check compares a second, independent compile — its model
2023
+ // document included, which is the text `build` writes beside the pair, and
2024
+ // which states where each region sits in the atlas written with it (`pages`,
2025
+ // issue #1016): so the second document is spelled from the second compile's
2026
+ // atlas as it would be written, or from the second, independent pack.
2027
+ const again = compile(opts);
2028
+ return gate.run({
2029
+ result,
2030
+ atlasText: atlas ? atlas.text : result.atlasText,
2031
+ atlasDir: opts.outDir,
2032
+ modelText,
2033
+ reEmit: { skeletonText: again.skeletonText, atlasText: atlas ? atlas.again : again.atlasText, modelText: modelDocument(again.model, again.skeletonText, atlas ? atlas.again : written(again.atlasText)) },
2034
+ profile,
2035
+ });
2036
+ }
2037
+
2038
+ /**
2039
+ * build — moved here unchanged from `./spine_commands.ts` (issue #1060) but
2040
+ * for its gate, which the entry hands in (`BuildGate`): both entries write
2041
+ * the Spine pair, the model document and the pages by this one body.
2042
+ */
2043
+ export function runBuild(flags: Record<string, string>, gate: BuildGate): void {
2044
+ const { label, opts, profile, packing } = readBuildInvocation(flags);
2045
+ printBuildHeader(label, opts);
2046
+ const result = compile(opts);
2047
+ printCompiled(opts, result);
2048
+
2049
+ // The model's document is spelled before the gate, so the text A18 compares
2050
+ // is the text written, and a model the document cannot carry is refused
2051
+ // before anything is (issue #922). It states where each region sits in the
2052
+ // atlas written beside it (`pages`, issue #1016), so it is spelled from that
2053
+ // atlas: under `--copy-images` the text with the copies' page names, planned
2054
+ // here from the text alone (`plannedPageCopies`) and copied after the gate.
2055
+ // Under `--pack` the pages move again after this gate, and the document
2056
+ // written is the one the packed gate below spells and compares.
2057
+ const copying = flags['copy-images'] !== undefined;
2058
+ const writtenAtlas = (atlasText: string): string => (copying ? plannedPageCopies(atlasText, opts.outDir).atlasText : atlasText);
2059
+ let modelText = modelDocument(result.model, result.skeletonText, writtenAtlas(result.atlasText));
2060
+ console.log(gate.heading(profile));
2061
+ const failures = runGate(gate, result, modelText, opts, profile, writtenAtlas);
2062
+ if (failures > 0) {
2063
+ console.error(`rigc: ${failures} assertion(s) failed — nothing written`);
2064
+ process.exit(1);
2065
+ }
2066
+
2067
+ mkdirSync(opts.outDir, { recursive: true });
2068
+
2069
+ // `--copy-images`: `--out` is otherwise NOT self-contained — a page's default
2070
+ // path is relative to the source art (often `../parts/foo.png`), which is
2071
+ // correct for a build sitting beside the project it came from and breaks the
2072
+ // moment the directory is zipped, committed or moved on its own (issue #217).
2073
+ // Opt-in only: the default stays exactly what it has always been.
2074
+ //
2075
+ // What is copied is what the ATLAS names, not what the image list holds: under
2076
+ // `--atlas-in` the two are different lists, and rebuilding the text from the
2077
+ // second wrote a file the pack never contained — zero bytes for a rig that
2078
+ // declares no parts, one fabricated page per part for a rig that does, both of
2079
+ // them green here because the gate above had already read the compile's own
2080
+ // text (issue #693, `src/emit.ts`).
2081
+ let atlasText = result.atlasText;
2082
+ if (flags['copy-images'] !== undefined) {
2083
+ const copied = copyAtlasPages(atlasText, opts.outDir);
2084
+ // The document's pages were spelled from the plan; the copy is held to it before anything of the pair is written.
2085
+ if (copied.atlasText !== writtenAtlas(result.atlasText)) {
2086
+ throw new Error(`internal: --copy-images wrote an atlas whose page names are not the ones ${MODEL_DOCUMENT_FILE} was spelled with — nothing of the pair was written`);
2087
+ }
2088
+ atlasText = copied.atlasText;
2089
+ console.log(` .. copy-images: ${copied.pages.length} page(s) copied into ${opts.outDir}`);
2090
+ for (const p of copied.pages) {
2091
+ const note = p.to === basename(p.from) ? '' : ' (renamed — basename collision)';
2092
+ console.log(` .. ${p.to.padEnd(24)} <- ${p.from} (${p.regions} region(s))${note}`);
2093
+ }
2094
+ }
2095
+
2096
+ // `--pack`: the parts go onto shared pages, which are written here as real
2097
+ // PNGs, so `--out` is self-contained by construction. The atlas above stays
2098
+ // the one the gate just read — packing changes only the ARRANGEMENT of the
2099
+ // bytes, and the sizes in `result.images` are still the ones measured off the
2100
+ // loose PNGs (see src/atlas.ts's header).
2101
+ if (packing) {
2102
+ const packOpts = {
2103
+ pageSize: readIntFlag(flags, 'page-size', DEFAULT_PAGE_SIZE),
2104
+ padding: readIntFlag(flags, 'padding', DEFAULT_PADDING),
2105
+ pageEdges: readPageEdges(flags),
2106
+ pageStem: 'skeleton',
2107
+ };
2108
+ const inputs = result.images.map((img) => ({
2109
+ region: img.region,
2110
+ absPath: img.absPath,
2111
+ width: img.width,
2112
+ height: img.height,
2113
+ }));
2114
+ const packed = packAtlas(inputs, packOpts);
2115
+ atlasText = packed.atlasText;
2116
+ for (const page of packed.pages) {
2117
+ page.plate.writePng(join(opts.outDir, page.name));
2118
+ console.log(
2119
+ ` .. pack: ${page.name} ${page.width}x${page.height}, ` +
2120
+ `${packed.placements.filter((p) => packed.pages[p.page].name === page.name).length} region(s), ` +
2121
+ `${(page.occupancy * 100).toFixed(1)}% covered, padding ${packed.padding}` +
2122
+ (packOpts.pageEdges === 'free' ? ', page edges free' : ''),
2123
+ );
2124
+ }
2125
+ for (const place of packed.placements) {
2126
+ console.log(
2127
+ ` .. ${place.region.padEnd(24)} ${place.width}x${place.height} -> ` +
2128
+ `${packed.pages[place.page].name} @ ${place.x},${place.y}`,
2129
+ );
2130
+ }
2131
+ // The pages are on disk now, so the packed pair can be gated as an artifact
2132
+ // rather than trusted as a construction: A17 stats every page, A06 reads its
2133
+ // IHDR back, A07 re-reads the text shape, A08 re-joins every attachment onto
2134
+ // a region, and A18 compares a second independent compile+pack. Two gates on
2135
+ // one build is the cost of shipping a second atlas shape.
2136
+ console.log(' .. validate (packed atlas, pages on disk)');
2137
+ const packAgain = packAtlas(
2138
+ compile(opts).images.map((img) => ({
2139
+ region: img.region,
2140
+ absPath: img.absPath,
2141
+ width: img.width,
2142
+ height: img.height,
2143
+ })),
2144
+ packOpts,
2145
+ );
2146
+ // The document written is spelled from the packed atlas, and this gate's A18 compares it with a
2147
+ // second compile's spelled from the second, independent pack (issue #1016).
2148
+ modelText = modelDocument(result.model, result.skeletonText, atlasText);
2149
+ const packFailures = runGate(gate, result, modelText, opts, profile, (text) => text, { text: atlasText, again: packAgain.atlasText });
2150
+ if (packFailures > 0) {
2151
+ console.error(
2152
+ `rigc: ${packFailures} assertion(s) failed on the PACKED atlas — the pages were written to ` +
2153
+ `${opts.outDir}, the skeleton/atlas pair was not`,
2154
+ );
2155
+ process.exit(1);
2156
+ }
2157
+ }
2158
+
2159
+ writeFileSync(join(opts.outDir, 'skeleton.json'), result.skeletonText);
2160
+ writeFileSync(join(opts.outDir, 'skeleton.atlas'), atlasText);
2161
+ // rigc's own record of the compiled rig (`rigc-compiled/3`, issue #922;
2162
+ // its `pages` the atlas just written, issue #1016), written with the pair
2163
+ // and only after the same gate — under `--pack`, the packed one. rigc's own posing core
2164
+ // reads it (`readModel`, `src/core/index.ts`, issue #380's step 2).
2165
+ writeFileSync(join(opts.outDir, MODEL_DOCUMENT_FILE), modelText);
2166
+ console.log(`rigc: wrote ${join(opts.outDir, 'skeleton.json')}`);
2167
+ console.log(`rigc: wrote ${join(opts.outDir, 'skeleton.atlas')}`);
2168
+ console.log(`rigc: wrote ${join(opts.outDir, MODEL_DOCUMENT_FILE)}`);
2169
+ // The next command is part of the message (issue #837). A green build is the
2170
+ // moment somebody wants to see what came out, and the one page rigc writes
2171
+ // for that is `preview` of exactly this directory — so the line names it,
2172
+ // with the path already resolved. Printed only here: a red build wrote
2173
+ // nothing, so there is nothing to look at and no line.
2174
+ console.log(gate.look(opts.outDir));
2175
+ }
2176
+
1675
2177
  /**
1676
2178
  * One `DROP` line, written once because two outcomes print it.
1677
2179
  *
@@ -1729,9 +2231,19 @@ export interface CliEntry {
1729
2231
  refusals?: readonly CliRefusal[];
1730
2232
  }
1731
2233
 
1732
- /** The commands an entry runs: every documented one, or — linking nothing of the runtime — those whose `runtime` is `false`. */
2234
+ /**
2235
+ * The commands an entry runs, as its page documents them: every documented
2236
+ * one, or — linking nothing of the runtime — those whose `runtime` is `false`
2237
+ * and those with a body of their own there (`runtime.core`, issue #1060),
2238
+ * documented by that body.
2239
+ */
1733
2240
  export function entryCommands(linksRuntime: boolean): CommandDoc[] {
1734
- return COMMANDS.filter((doc) => linksRuntime || doc.runtime === false);
2241
+ if (linksRuntime) return COMMANDS;
2242
+ return COMMANDS.flatMap((doc): CommandDoc[] => {
2243
+ if (doc.runtime === false) return [doc];
2244
+ const core = doc.runtime.core;
2245
+ return core === undefined ? [] : [{ ...doc, usage: core.usage, flags: core.flags ?? doc.flags, notes: core.notes }];
2246
+ });
1735
2247
  }
1736
2248
 
1737
2249
  /**
@@ -1745,7 +2257,8 @@ function commandNeedsRuntime(doc: CommandDoc, runs: readonly string[]): SpineRun
1745
2257
  const needs = doc.runtime === false ? 'nothing' : doc.runtime.for;
1746
2258
  return new SpineRuntimeError(
1747
2259
  `\`${doc.name}\` runs through spine-core (${needs}), and the runtime could not be used: ${SPINE_SIDE_ABSENT}. ` +
1748
- `The commands this entry runs are ${runs.join(', ')}; \`bun cli.ts ${doc.name}\` runs it`,
2260
+ `The commands this entry runs are ${runs.join(', ')}; the entry that links spine-core runs \`${doc.name}\` — ` +
2261
+ `installed, \`rigc ${doc.name}\` once @esotericsoftware/spine-core is installed beside the package; from a source checkout, \`bun cli.ts ${doc.name}\``,
1749
2262
  );
1750
2263
  }
1751
2264
 
@@ -1785,7 +2298,7 @@ export function runCli(entry: CliEntry, argv: readonly string[]): void {
1785
2298
 
1786
2299
  const { flags, lists, positional } = parseArgs([...rest], REPEATABLE_FLAGS[command]);
1787
2300
  if (flags.help !== undefined) {
1788
- console.log(commandHelp(command));
2301
+ console.log(commandHelp(command, docs));
1789
2302
  process.exit(0);
1790
2303
  }
1791
2304
  entry.runs[command]({ flags, lists, positional });