diff --git a/CHANGELOG.md b/CHANGELOG.md index e97919b685..164c367ba7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi assigned one (`row.Role = ...`) no longer compiles. The value belongs to the server: set it through a database default or EF Core (`db.Entry(row).Property(r => r.Role).CurrentValue`), which writes private setters. +- **Java and Kotlin: `` now narrow every generator.** The `codegen-spring` and + `codegen-kotlin` generators ignored a generator's `` and wrote every object; they now + honour them, as the `codegen-base` generators always did. A pom whose filters were inert + generates fewer files: remove the `` that no longer describe what you want. ### Security @@ -92,6 +96,21 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi the new `fixtures/writer-conformance/` corpus. No shipped generator uses the writer yet, so generated output does not change in this release. +- **Select objects to regenerate in every port.** C# `dotnet meta gen --entities A,B` (and + `verify --codegen --entities`) and, for Java and Kotlin, `mvn metaobjects:generate + -Dmeta.entities=A,B` regenerate only the named objects' files, like TypeScript's + `meta gen A B` and Python's `--entities`. Names are short object names. A file built from the + whole model (C#'s `AppDbContext`, the Java and Kotlin validator registry, report export writer + and binding provider) still renders from the whole model, a narrowed run neither deletes nor + forgets the other objects' files, and a blank selection or a name that matches no object is + refused (exit 2 in C#, a failed goal in Maven). +- **`meta gen ...` refuses a name that matches no object**, with exit 2 and nothing + written, as Python, C# and the Maven plugin do. It used to drop the unknown name silently + when another name matched, and only warn when none did. +- **The Java loader's initialization timeout is a setting.** It stays 30 seconds by default. Raise + it with `-Dmeta.loaderTimeoutMs` (or ``) on the Maven plugin, + `LoaderOptions.setInitTimeoutMs`, the loader argument `initTimeoutMs`, or the system property + `metaobjects.loader.initTimeoutMs`; the timeout error names them. - **A page size for generated list routes: `api.pagination`, in every port.** An external review of 1.2.0 found that list endpoints had no hard cap: the TypeScript and C# routes returned the whole table when `limit` was omitted, and `?limit=100000` was accepted in every port. The new @@ -322,6 +341,12 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi ### Fixed +- **The Java loader loads a large model.** Each child added to a node was checked against a + list of the keys already added, so a root with 1,160 objects took more than the 30-second + timeout to load and failed. The check is a set lookup now, and a reference resolves through + the per-type name index instead of a scan of the root. Twenty copies of the canonical + conformance model (1,160 objects) load in about 2 seconds of CPU, and `LargeModelLoadTest` + holds that to a budget. Java and Kotlin. - **`gen ` no longer shrinks the files rendered from the whole model.** Narrowing a run to some entities (`meta gen Post`, Python `metaobjects gen --entities Post`) handed every generator only the named entities, so a whole-model file (the TypeScript barrel and Cube @@ -329,8 +354,8 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi Python's entity and route generators resolve relations through) was rendered from just them. A narrowed run now writes only the named entities' own files, and every whole-model file it writes is byte-identical to a full run's. A generator sees the whole model in `ctx.entities` and the narrowing as `ctx.selected`; `perEntity` (`per_entity`) - applies it. A Python target's declared `entities` still bounds that target's model. C# and - Java take no entity filter and were not affected. Two consequences to expect: a narrowed run + applies it. A Python target's declared `entities` still bounds that target's model. The C# and + Maven entity selection added in this release follows the same rule. Two consequences to expect: a narrowed run checks the whole model as a full run does (a sourced object with no `dialect`, colliding collection names), because the files it writes are rendered from it; and an owned generator that writes one file per object by looping over `ctx.entities` itself, instead of through diff --git a/docs/features/metadata-dependencies.md b/docs/features/metadata-dependencies.md index f3694b0672..694a3183b6 100644 --- a/docs/features/metadata-dependencies.md +++ b/docs/features/metadata-dependencies.md @@ -708,8 +708,11 @@ the snapshots first under `dep:/` source ids. It never writes ei POCO of a *value object* (and the shared enums it uses) that a generated type reaches — an entity or local value object that holds it, or a local template whose `@payloadRef` / `@responseRef` names it — because that generated type needs it to compile. `inScope` is Python's formula (no `matchesScope` on the -project's own objects), and C# has no entity-name positional on `gen`, so the TypeScript -"refuse an explicitly named imported object" check has no C# surface. Known edge: a local entity +project's own objects). `dotnet meta gen --entities` resolves names against the in-scope +objects only, so naming an excluded import is refused the way TypeScript's "refuse an +explicitly named imported object" check refuses it (exit 2, `--entities names 'X', which +matches no object in the metadata.` — C#'s wording, not TypeScript's imported-specific +message). Known edge: a local entity that `extends` an imported concrete entity in a table-per-hierarchy mapping is not specially handled. ## Vendoring vs. depending diff --git a/docs/features/own-your-codegen.md b/docs/features/own-your-codegen.md index 858eea3805..f32d46fa77 100644 --- a/docs/features/own-your-codegen.md +++ b/docs/features/own-your-codegen.md @@ -593,7 +593,8 @@ handles those files by one rule: - **Only a full run cleans up.** A run of the project's whole generator list decides; a run narrowed to some generators (`--generators`), to some entities (`meta gen `, - `metaobjects gen --entities`), or to a target that shares its directory with a target left + `metaobjects gen --entities`, `dotnet meta gen --entities`, Maven `-Dmeta.entities`), or to + a target that shares its directory with a target left out, deletes nothing, because the files of the part it skipped would all look stale. So does `--baseline=adopt`, which writes nothing by definition. - **A candidate is a file the manifest says `gen` wrote.** It lies under an output directory @@ -614,8 +615,10 @@ handles those files by one rule: that name is not mistaken for generated output. - **A dry run** (`meta gen --dry-run`) reports what it would remove and removes nothing. -On **Java and Kotlin** every `mvn metaobjects:generate` execution is a full run, since its -`` list is always complete. Every manifest entry names its owner: the generator's +On **Java and Kotlin** every `mvn metaobjects:generate` execution runs the whole +`` list, since there is no `--generators` narrowing; a run narrowed by +`meta.entities` is the one exception, and it deletes no orphaned file (see +[Select objects](../ports/java.md#generate) on the Java page). Every manifest entry names its owner: the generator's registry id and its output directory within the module, not the Maven execution, so `default-cli`, a renamed execution and the lifecycle execution agree. A recorded file no generator emitted in this build is an orphan when this run's own generators own it, or when diff --git a/docs/ports/csharp.md b/docs/ports/csharp.md index 6d65b1ea9e..9e64f6c435 100644 --- a/docs/ports/csharp.md +++ b/docs/ports/csharp.md @@ -295,6 +295,21 @@ reference their output; `gen` warns when one is missing from the list, since the would not compile. `--generators ` narrows a run to the declared entries named; it never adds one, and naming an entry the list does not declare is refused. +**Select objects.** `dotnet meta gen --entities Product,Supplier` (comma-separated short object +names, the `name` of the object without its package, as TypeScript's positional names and +Python's `--entities` match) regenerates only those objects' files; `dotnet meta verify --codegen +--entities ...` checks only the same files. Only per-object output narrows. A file built from +the whole model, such as `AppDbContext.g.cs`, still renders from the whole model and is +byte-identical to an unfiltered run, and a shared file (a shared enum, a helper) is the +whole-model rendering too. A generator that reads the model root rather than the object list +(`api-docs`, `requirement-tests`, template entries) is not narrowed. A narrowed run never +deletes a stale file and never drops the manifest entry of a file it did not write, so the +unselected objects' files are left as they are; run `gen` without the flag to sweep. Two +refusals, both exit 2: `--entities` naming nothing (`error: dotnet meta gen: --entities names no +object.`) and a name that matches no object (`error: dotnet meta gen: --entities names 'X', +which matches no object in the metadata.`). An owned generator whose output is a single +whole-model file declares `public bool WholeModel => true;`. + **Own a generator.** `dotnet meta eject ...` copies a reference generator into `codegen/generators/`. Every generator `dotnet meta gen --list` shows is ejectable except `template`, the Mustache primitive, which has no emit logic of its own to own. An ejected diff --git a/docs/ports/java.md b/docs/ports/java.md index 90464aadac..69e5529364 100644 --- a/docs/ports/java.md +++ b/docs/ports/java.md @@ -295,6 +295,27 @@ mvn metaobjects:generate # or run it alone The generators write the DTO records, the Spring controller, the repository and the filter allowlist under `target/generated-sources/java`, which Maven compiles with your code. +**Select objects.** `mvn metaobjects:generate -Dmeta.entities=Program,Week` (or +`Program,Week` in the plugin ``) regenerates only those +objects' files: comma-separated short object names, the selection TypeScript's +`meta gen Program Week` and the `--entities` flag in Python and C# make. Only per-object output +narrows. A file built from the whole model (a validator registry, the report export writer, +the class-binding provider, a package's wire support classes) still renders from the whole model +and is byte-identical to an unnarrowed run. A narrowed run deletes no orphaned file and keeps +the manifest entry of every file it did not write; run without the property to sweep. Two +refusals fail the goal: a blank selection (`mvn metaobjects:generate: meta.entities names no +object.`) and a name that matches no object (`... meta.entities names 'X', which matches no +object in the metadata.`). Each generator's `` apply on top. Unlike the selection, a +generator's `` narrow its whole-model files too, since they configure that generator. +Through 8.2.x the `codegen-spring` and `codegen-kotlin` generators ignored ``; from +8.3.0 they honour them, so a pom whose filters were inert now generates less. + +**Large models.** The loader gives initialization 30 seconds by default. A model large enough +to need longer sets `-Dmeta.loaderTimeoutMs=120000` (or `` in the plugin +``). Outside Maven, the same setting is `LoaderOptions.setInitTimeoutMs`, the +loader argument `initTimeoutMs`, or the system property `metaobjects.loader.initTimeoutMs`. +The timeout error names all three. + ## Verify ```bash diff --git a/docs/ports/kotlin.md b/docs/ports/kotlin.md index 23d4af8b66..f208af6050 100644 --- a/docs/ports/kotlin.md +++ b/docs/ports/kotlin.md @@ -507,6 +507,12 @@ mvn metaobjects:generate # or run the codegen alone mvn metaobjects:verify # fails, naming the files, when generated code is stale against the model ``` +`-Dmeta.entities=Program,Week` regenerates only those objects' files, and each generator's +`` narrow what it writes; `-Dmeta.loaderTimeoutMs` raises the loader's 30-second +initialization timeout for a large model. Both are the Maven plugin's, shared with Java: see +[Select objects](java.md#generate) on the Java page for what narrows, what stays whole-model, +and the refusals. + Schema migrations are owned by the TypeScript toolchain — see the [Migrations section](../features/migrations-and-drift.md#kotlin) for the `meta migrate` commands. diff --git a/docs/ports/python.md b/docs/ports/python.md index 82dee379b0..5b198ec5d6 100644 --- a/docs/ports/python.md +++ b/docs/ports/python.md @@ -186,6 +186,17 @@ config is refused. Column naming is not a Python setting at all: it is `naming` ([physical-naming.md](../features/migrations/physical-naming.md)). See [the migration section](../features/migrations/docs-are-generators.md#python) and the [config reference](../features/cli.md). +**Select objects.** `metaobjects gen --entities Program,Week` (comma-separated short object +names) regenerates only those objects' files; a target that lists `entities` keeps only the +names in both. `metaobjects verify --codegen --entities ...` checks the same files. A narrowed +run deletes no orphaned file; run without the flag to sweep. Two refusals, both exit 2: +`--entities` naming nothing (`error: metaobjects gen: --entities names no object.`) and a +name that matches no object (`error: metaobjects gen: --entities names 'X', which matches no +object in the metadata.`). The same selection is a positional name list in TypeScript +(`meta gen Program Week`), `--entities` in C#, and `-Dmeta.entities` on the Maven plugin. +`gen` lists the files it changed and counts the rest separately +(`metaobjects gen: wrote 2 file(s) across 1 target(s). 40 file(s) left as they were.`). + **Start a project.** `metaobjects init` writes `metaobjects.config.yaml` (one `app` target with no generators) and an empty, loadable `metaobjects/meta.app.yaml`, and reports what it wrote. It never overwrites a file it finds; it says it kept it. `metaobjects init --with-docs` diff --git a/docs/ports/typescript.md b/docs/ports/typescript.md index 98c0ec3b6c..0fa58d429c 100644 --- a/docs/ports/typescript.md +++ b/docs/ports/typescript.md @@ -319,6 +319,12 @@ meta verify --codegen # committed output, docs included, vs a fresh regen meta verify --db # live DB schema vs metadata ``` +`meta gen Author Post` names objects by their short name (without the package). Each +generator's `filter` option applies on top. A name that matches no object is refused with +exit 2 before anything is written, even beside names that match (`meta gen: names 'Ordr', +which matches no object in the metadata.`). Every port has the same selection: `--entities` in Python and C#, +`-Dmeta.entities` on the Maven plugin for Java and Kotlin. + ### Schema — first migration on a brand-new database `meta migrate` diffs your metadata against a **committed schema snapshot**, diff --git a/server/csharp/MetaObjects.Cli.Tests/GenEntitiesSelectionTests.cs b/server/csharp/MetaObjects.Cli.Tests/GenEntitiesSelectionTests.cs new file mode 100644 index 0000000000..a953e7e49a --- /dev/null +++ b/server/csharp/MetaObjects.Cli.Tests/GenEntitiesSelectionTests.cs @@ -0,0 +1,179 @@ +// `dotnet meta gen --entities A,B` — the C# analogue of TypeScript's positional entity names +// and Python's `--entities`: only PER-OBJECT output narrows, whole-model files still render from +// the whole model, and a narrowed run neither deletes nor forgets the unselected objects' files. + +using MetaObjects.Codegen; +using Xunit; + +namespace MetaObjects.Cli.Tests; + +[Collection(OwnedRunnerCollection.Name)] +public sealed class GenEntitiesSelectionTests : IDisposable +{ + private readonly string _tmp = Path.Combine(Path.GetTempPath(), "meta-entities-" + Guid.NewGuid().ToString("N")); + private string MetaDir => Path.Combine(_tmp, "metaobjects"); + private string OutDir => Path.Combine(_tmp, "generated"); + private string Manifest => Path.Combine(_tmp, ".metaobjects", ".gen-state", ".hashes.json"); + + private static string Entity(string name, string table) => $$$""" + { "object.entity": { "name": "{{{name}}}", "children": [ + { "source.rdb": { "@table": "{{{table}}}" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "label" } }, + { "identity.primary": { "@fields": "id" } } + ]}} + """; + + public GenEntitiesSelectionTests() + { + Directory.CreateDirectory(MetaDir); + File.WriteAllText(Path.Combine(MetaDir, "meta.acme.json"), + "{ \"metadata.root\": { \"package\": \"acme\", \"children\": [" + + Entity("Product", "products") + "," + Entity("Supplier", "suppliers") + "," + Entity("Widget", "widgets") + "]}}"); + } + + public void Dispose() { try { Directory.Delete(_tmp, recursive: true); } catch { } } + + private static readonly string[] Suite = ["entity", "names", "db-context", "routes"]; + + private (int ExitCode, string Stdout, string Stderr) Gen(params string[] extra) => + OwnedRunner.Run(_tmp, Suite, ["gen", MetaDir, "--out", OutDir, "--namespace", "Acme.Generated", .. extra]); + + private string Read(string file) => File.ReadAllText(Path.Combine(OutDir, file)); + private bool Has(string file) => File.Exists(Path.Combine(OutDir, file)); + + [Fact] + public void Selection_writes_only_the_selected_objects_per_object_files() + { + var (exit, stdout, stderr) = Gen("--entities", "Product, Supplier"); + + Assert.True(exit == 0, $"exit={exit}\n{stdout}\n{stderr}"); + Assert.True(Has("Product.g.cs")); + Assert.True(Has("Supplier.g.cs")); + Assert.True(Has("ProductNames.g.cs")); + Assert.False(Has("Widget.g.cs")); + Assert.False(Has("WidgetNames.g.cs")); + Assert.DoesNotContain(Directory.GetFiles(OutDir, "*", SearchOption.AllDirectories), f => f.Contains("Widget")); + } + + [Fact] + public void A_whole_model_file_is_identical_to_the_unfiltered_run() + { + Assert.Equal(0, Gen().ExitCode); + var full = Read("AppDbContext.g.cs"); + Assert.Contains("Widget", full); + Directory.Delete(OutDir, recursive: true); + + Assert.Equal(0, Gen("--entities", "Product").ExitCode); + + Assert.Equal(full, Read("AppDbContext.g.cs")); + Assert.False(Has("Widget.g.cs")); + } + + [Fact] + public void Selecting_an_object_the_context_does_not_cover_still_renders_the_whole_context() + { + // A sourceless value object adds nothing to the DbContext, yet the context is whole-model. + File.WriteAllText(Path.Combine(MetaDir, "meta.vo.json"), + "{ \"metadata.root\": { \"package\": \"acme\", \"children\": [" + + "{ \"object.value\": { \"name\": \"Money\", \"children\": [ { \"field.string\": { \"name\": \"code\" } } ]}}]}}"); + Assert.Equal(0, Gen().ExitCode); + var full = Read("AppDbContext.g.cs"); + File.Delete(Path.Combine(OutDir, "AppDbContext.g.cs")); + + Assert.Equal(0, Gen("--entities", "Money").ExitCode); + + Assert.Equal(full, Read("AppDbContext.g.cs")); + } + + [Fact] + public void Shared_files_inside_a_per_object_generator_still_render_whole_when_the_selection_does_not_touch_them() + { + // A shared enum and a decimal wire helper are consumed only by Gadget; selecting Product + // contributes nothing to either, yet each is built from the whole model. + File.WriteAllText(Path.Combine(MetaDir, "meta.gadget.json"), + "{ \"metadata.root\": { \"package\": \"acme\", \"children\": [" + + "{ \"field.enum\": { \"name\": \"GadgetState\", \"abstract\": true, \"@values\": [\"ON\", \"OFF\"] } }," + + "{ \"object.entity\": { \"name\": \"Gadget\", \"children\": [" + + "{ \"source.rdb\": { \"@table\": \"gadgets\" } }," + + "{ \"field.long\": { \"name\": \"id\" } }," + + "{ \"field.decimal\": { \"name\": \"price\" } }," + + "{ \"field.enum\": { \"name\": \"state\", \"extends\": \"GadgetState\" } }," + + "{ \"identity.primary\": { \"@fields\": \"id\" } }]}}]}}"); + Assert.Equal(0, Gen().ExitCode); + var enums = Read("Enums.g.cs"); + var wire = Read("DecimalWire.g.cs"); + Assert.Contains("GadgetState", enums); + Directory.Delete(OutDir, recursive: true); + + var (exit, stdout, stderr) = Gen("--entities", "Product"); + + Assert.True(exit == 0, $"exit={exit}\n{stdout}\n{stderr}"); + Assert.Equal(enums, Read("Enums.g.cs")); + Assert.Equal(wire, Read("DecimalWire.g.cs")); + Assert.False(Has("Gadget.g.cs")); + Assert.True(Has("Product.g.cs")); + } + + [Fact] + public void A_narrowed_run_neither_deletes_nor_forgets_the_unselected_files() + { + Assert.Equal(0, Gen().ExitCode); + var widget = Read("Widget.g.cs"); + var manifestBefore = File.ReadAllText(Manifest); + Assert.Contains("Widget.g.cs", manifestBefore); + + // Metadata drops Widget: a full run would sweep its files, a narrowed one must not. + File.WriteAllText(Path.Combine(MetaDir, "meta.acme.json"), + "{ \"metadata.root\": { \"package\": \"acme\", \"children\": [" + Entity("Product", "products") + "]}}"); + var (exit, stdout, stderr) = Gen("--entities", "Product"); + + Assert.True(exit == 0, $"exit={exit}\n{stdout}\n{stderr}"); + Assert.Equal(widget, Read("Widget.g.cs")); + Assert.DoesNotContain("removed:", stdout); + var manifestAfter = File.ReadAllText(Manifest); + Assert.Contains("Widget.g.cs", manifestAfter); + Assert.Contains("WidgetNames.g.cs", manifestAfter); + Assert.Contains("Supplier.g.cs", manifestAfter); + } + + [Fact] + public void Verify_narrowed_to_entities_checks_only_the_selected_objects() + { + Assert.Equal(0, Gen("--entities", "Product").ExitCode); + + var verify = OwnedRunner.Run(_tmp, Suite, + "verify", "--codegen", MetaDir, "--out", OutDir, "--namespace", "Acme.Generated", "--entities", "Product"); + + Assert.True(verify.ExitCode == 0, $"exit={verify.ExitCode}\n{verify.Stdout}\n{verify.Stderr}"); + } + + [Fact] + public void A_blank_selection_is_refused() + { + var (exit, _, stderr) = Gen("--entities", " , "); + + Assert.Equal(2, exit); + Assert.Contains("error: dotnet meta gen: --entities names no object.", stderr); + Assert.False(Directory.Exists(OutDir)); + } + + [Fact] + public void A_name_matching_no_object_is_refused_and_writes_nothing() + { + var (exit, _, stderr) = Gen("--entities", "Product,Nope,Gone"); + + Assert.Equal(2, exit); + Assert.Contains("error: dotnet meta gen: --entities names 'Nope', 'Gone', which matches no object in the metadata.", stderr); + Assert.False(Directory.Exists(OutDir)); + } + + [Fact] + public void Without_the_flag_every_object_is_written() + { + Assert.Equal(0, Gen().ExitCode); + + Assert.True(Has("Widget.g.cs")); + Assert.True(Has("Supplier.g.cs")); + } +} diff --git a/server/csharp/MetaObjects.Cli/Program.cs b/server/csharp/MetaObjects.Cli/Program.cs index 85dd5acb5f..32b4f60c43 100644 --- a/server/csharp/MetaObjects.Cli/Program.cs +++ b/server/csharp/MetaObjects.Cli/Program.cs @@ -31,9 +31,10 @@ " codegen/Program.cs and codegen/Codegen.csproj;\n" + " --with-docs lists the api-docs generator in it\n" + " gen [] --out [--namespace ] [--emit-abstract-shapes]\n" + - " [--generators ] [--template-root ] [--strict]\n" + + " [--generators ] [--entities ] [--template-root ] [--strict]\n" + " run the generator list in codegen/Generators.cs;\n" + " --generators narrows it to the entries named;\n" + + " --entities narrows per-object output to the objects named;\n" + " --strict rejects an unknown @attr before writing\n" + " (as verify does, ADR-0023; default is a warning)\n" + " gen --list list available generators (stable names) and exit\n" + @@ -384,6 +385,7 @@ static int RunVerify(string[] rest) string? outDir = null; string? ns = null; string? generatorsCsv = null; + string? entitiesCsv = null; string? templateRoot = null; string? columnNamingRaw = null; bool templates = false, codegen = false, deps = false, db = false, lax = false, noFieldLint = false, requireImplementers = false; @@ -423,6 +425,7 @@ static int RunVerify(string[] rest) else if (a == "--out" && i + 1 < rest.Length) outDir = rest[++i]; else if (a == "--namespace" && i + 1 < rest.Length) { ns = rest[++i]; } else if (a == "--generators" && i + 1 < rest.Length) generatorsCsv = rest[++i]; + else if (a == "--entities" && i + 1 < rest.Length) entitiesCsv = rest[++i]; else if (a == "--template-root" && i + 1 < rest.Length) templateRoot = rest[++i]; // ADR-0058 — the flag is removed: column and table naming is one project setting // now, "naming" in .metaobjects/config.json, read by every port. Captured here (not @@ -526,6 +529,7 @@ static int RunVerify(string[] rest) if (metadataDir is not null) codegenArgs.Insert(2, metadataDir); if (ns is not null) codegenArgs.AddRange(["--namespace", ns]); if (generatorsCsv is not null) codegenArgs.AddRange(["--generators", generatorsCsv]); + if (entitiesCsv is not null) codegenArgs.AddRange(["--entities", entitiesCsv]); if (templateRoot is not null) codegenArgs.AddRange(["--template-root", templateRoot]); if (lax) codegenArgs.Add("--lax"); codegenHandedOff = CodegenHandoff.TryRun(projectRoot, codegenArgs.ToArray(), out codegenHandoffExit); @@ -716,26 +720,28 @@ static int RunDepsGate(string? metadataDir) partial class Program { static readonly string[] GenValueFlags = - ["--out", "--namespace", "--generators", "--template-root", "--template-spec", "--column-naming", "--baseline"]; + ["--out", "--namespace", "--generators", "--template-root", "--template-spec", "--column-naming", "--baseline", "--entities"]; static readonly string[] GenBoolFlags = ["--list", "--emit-abstract-shapes", "--strict"]; static readonly string[] VerifyValueFlags = - ["--prompts", "--out", "--namespace", "--generators", "--template-root", "--column-naming"]; + ["--prompts", "--out", "--namespace", "--generators", "--template-root", "--column-naming", "--entities"]; static readonly string[] VerifyBoolFlags = ["--templates", "--codegen", "--deps", "--db", "--lax", "--no-field-lint", "--require-implementers"]; /// Each command's usage, printed by dotnet meta <command> --help. static readonly Dictionary CommandUsage = new() { - ["gen"] = "usage: dotnet meta gen [] --out [--namespace ] [--generators ] [--template-root ] [--emit-abstract-shapes] [--baseline default|adopt] [--strict]\n" + + ["gen"] = "usage: dotnet meta gen [] --out [--namespace ] [--generators ] [--entities ] [--template-root ] [--emit-abstract-shapes] [--baseline default|adopt] [--strict]\n" + " dotnet meta gen --list\n" + " runs the generator list in codegen/Generators.cs (`dotnet meta init` writes it);\n" + " --generators narrows that list to the entries named, and never adds one.\n" + + " --entities narrows per-object output to the objects named (short names); whole-model files such as\n" + + " AppDbContext still render from the whole model, and nothing else is deleted or forgotten.\n" + " --strict: an unknown @attr is an error (exit 1, nothing written), as in verify (ADR-0023);\n" + " without it gen warns and generates. Owned copies identical to the shipped generators\n" + " get one advisory line naming `dotnet meta eject --undo`.\n" + " column/table naming: \"naming\" in .metaobjects/config.json (ADR-0058), not a flag here", ["init"] = "usage: dotnet meta init [--root ] [--with-docs]\n" + " --with-docs lists the api-docs generator in codegen/Generators.cs (written to docs/csharp)", - ["verify"] = "usage: dotnet meta verify [--templates [--prompts ]] [--codegen --out [--namespace ] [--generators ] [--template-root ]] [--deps] [--db] [--lax] [--no-field-lint] [--require-implementers]", + ["verify"] = "usage: dotnet meta verify [--templates [--prompts ]] [--codegen --out [--namespace ] [--generators ] [--entities ] [--template-root ]] [--deps] [--db] [--lax] [--no-field-lint] [--require-implementers]", ["fmt"] = "usage: dotnet meta fmt [] [--check]", ["eject"] = "usage: dotnet meta eject ... [--force] [--root ]\n" + " dotnet meta eject --undo [...] [--force] [--root ]\n" + diff --git a/server/csharp/MetaObjects.Codegen/CodegenCli.cs b/server/csharp/MetaObjects.Codegen/CodegenCli.cs index 0196264aae..2cb582893c 100644 --- a/server/csharp/MetaObjects.Codegen/CodegenCli.cs +++ b/server/csharp/MetaObjects.Codegen/CodegenCli.cs @@ -230,7 +230,8 @@ public static GenOutcome RunGen( IReadOnlyList generators, string? projectRoot = null, string baseline = "default", bool? includeNames = null, - bool sweepOrphans = false) + bool sweepOrphans = false, + IReadOnlyList? entities = null) { var loadErrors = load.Errors.Select(e => e.Code.ToString()).ToList(); if (loadErrors.Count > 0) return new GenOutcome(loadErrors, null); @@ -258,6 +259,7 @@ public static GenOutcome RunGen( // type, in a different assembly, once ejected — still satisfies the gate. IncludeNames = includeNames ?? generators.Any(g => g.Name == "names"), SweepOrphans = sweepOrphans, + EntitySelection = entities, Dialect = neutralConfig?.MigrateDialect, SkipRedundantChecks = neutralConfig?.MigrateSkipRedundantChecks ?? false, ForeignKeyIndexes = neutralConfig?.MigrateForeignKeyIndexes ?? false, @@ -453,10 +455,10 @@ public static int Run(string[] args, IReadOnlyList generators) if (args.Length == 0) { Console.Error.WriteLine( - "usage: gen [] --out [--namespace ] [--generators ]\n" + + "usage: gen [] --out [--namespace ] [--generators ] [--entities ]\n" + " [--emit-abstract-shapes] [--strict] [--baseline default|adopt] [--template-root ]\n" + " | verify --codegen [] --out [--namespace ] [--generators ]\n" + - " [--template-root ] [--lax]\n" + + " [--entities ] [--template-root ] [--lax]\n" + " (column/table naming: \"naming\" in .metaobjects/config.json — ADR-0058)"); return 2; } @@ -528,16 +530,16 @@ public static IReadOnlyList ComposeGenerators( } private static readonly string[] GenValueFlags = - ["--out", "--namespace", "--generators", "--template-root", "--template-spec", "--column-naming", "--baseline"]; + ["--out", "--namespace", "--generators", "--template-root", "--template-spec", "--column-naming", "--baseline", "--entities"]; private static readonly string[] GenBoolFlags = ["--emit-abstract-shapes", "--strict"]; private static readonly string[] VerifyValueFlags = - ["--out", "--namespace", "--generators", "--template-root", "--column-naming"]; + ["--out", "--namespace", "--generators", "--template-root", "--column-naming", "--entities"]; private static readonly string[] VerifyBoolFlags = ["--codegen", "--lax"]; /// The flags both commands share, parsed once. private sealed class OwnedArgs { - public string? MetadataDir, OutDir, TemplateRoot, TemplateSpecPath, ColumnNamingRaw, GeneratorsCsv; + public string? MetadataDir, OutDir, TemplateRoot, TemplateSpecPath, ColumnNamingRaw, GeneratorsCsv, EntitiesCsv; public string? Namespace; public string Baseline = "default"; public bool EmitAbstractShapes, Codegen, Lax, Strict; @@ -549,6 +551,11 @@ private sealed class OwnedArgs public IReadOnlyList? Selection => GeneratorsCsv ?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + /// The --entities short names, or null when the flag was not given. + /// Given but naming nothing is an empty list, which refuses. + public IReadOnlyList? Entities => EntitiesCsv + ?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + public static OwnedArgs Parse(string[] rest, string command, string[] valueFlags, string[] boolFlags) { var a = new OwnedArgs(); @@ -582,6 +589,7 @@ public static OwnedArgs Parse(string[] rest, string command, string[] valueFlags case "--template-root": a.TemplateRoot = value; break; case "--template-spec": a.TemplateSpecPath = value; break; case "--generators": a.GeneratorsCsv = value; break; + case "--entities": a.EntitiesCsv = value; break; } } else if (boolFlags.Contains(flag)) @@ -647,6 +655,24 @@ private static bool TryConfiguredOutDir(string? metadataDir, out string? outDir) private const string GenCommandName = "dotnet meta gen"; private const string VerifyCommandName = "dotnet meta verify --codegen"; + /// + /// The refusal an --entities selection earns, or null: nothing named, or a name + /// that is no object of the model. Names are short object names (MetaData.Name), as in + /// the TypeScript and Python ports. Pass null for the blank check + /// alone, before the metadata is loaded. + /// + private static string? RefuseEntities(OwnedArgs a, MetaRoot? root, string command) + { + if (a.Entities is not { } names) return null; + if (names.Count == 0) return $"error: {command}: --entities names no object."; + if (root is null) return null; + var known = root.Objects().Where(root.InScope).Select(o => o.Name).ToHashSet(StringComparer.Ordinal); + var unknown = names.Where(n => !known.Contains(n)).ToList(); + return unknown.Count == 0 ? null + : $"error: {command}: --entities names {string.Join(", ", unknown.Select(n => $"'{n}'"))}, " + + "which matches no object in the metadata."; + } + private static int RunGenArgs(string[] rest, IReadOnlyList declared) { var a = OwnedArgs.Parse(rest, GenCommandName, GenValueFlags, GenBoolFlags); @@ -663,6 +689,12 @@ private static int RunGenArgs(string[] rest, IReadOnlyList declared) return 2; } + if (RefuseEntities(a, null, GenCommandName) is { } blank) + { + Console.Error.WriteLine(blank); + return 2; + } + // ADR-0058 — column and table naming is one project setting now, read from // .metaobjects/config.json by every port; the flag is refused rather than silently // dropped, since the old value may have been right for that adopter's database. @@ -694,7 +726,7 @@ private static int RunGenArgs(string[] rest, IReadOnlyList declared) } if (a.OutDir is null) { - Console.Error.WriteLine("usage: dotnet meta gen [] --out [--namespace ] [--generators ] [...]"); + Console.Error.WriteLine("usage: dotnet meta gen [] --out [--namespace ] [--generators ] [--entities ] [...]"); Console.Error.WriteLine(" (or set \"csharp\": { \"outDir\": \"\" } in .metaobjects/config.json)"); return 2; } @@ -753,11 +785,17 @@ private static int RunGenArgs(string[] rest, IReadOnlyList declared) WarnUnknownAttrs(meta); genLoad = meta.Load(); } + if (RefuseEntities(a, genLoad.Root, GenCommandName) is { } unknownEntity) + { + Console.Error.WriteLine(unknownEntity); + return 2; + } var outcome = RunGen(genLoad, a.OutDir, a.Namespace ?? meta.Namespace, a.EmitAbstractShapes, suite, projectRoot, a.Baseline, includeNames: declared.Any(g => g.Name == "names"), // Only a run of the whole declared list can tell a stale file from one the - // narrowing left out. - sweepOrphans: a.Selection is not { Count: > 0 }); + // narrowing left out; --entities narrows the same way, so it never sweeps either. + sweepOrphans: a.Selection is not { Count: > 0 } && a.Entities is null, + entities: a.Entities); if (!outcome.Ok) { ReportFailure(outcome, GenCommandName); @@ -897,6 +935,11 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar Console.Error.WriteLine(ColumnNamingFlagRemoved(a.ColumnNamingRaw)); return 2; } + if (RefuseEntities(a, null, VerifyCommandName) is { } blankEntities) + { + Console.Error.WriteLine(blankEntities); + return 2; + } if (a.OutDir is null) { if (!TryConfiguredOutDir(a.MetadataDir, out var configuredOut)) return 2; @@ -918,6 +961,11 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar Console.Error.WriteLine($"{VerifyCommandName}: FAILED (metadata did not load cleanly)"); return 1; } + if (RefuseEntities(a, load.Root, VerifyCommandName) is { } unknownEntity) + { + Console.Error.WriteLine(unknownEntity); + return 2; + } // The same suite `gen` builds: verify must regenerate what gen wrote, or it convicts // gen's own output as drift. @@ -958,6 +1006,7 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar Namespace = a.Namespace ?? meta.Namespace, ColumnNamingStrategy = columnNaming, IncludeNames = declared.Any(g => g.Name == "names"), + EntitySelection = a.Entities, // The manifest gen keeps: an extra file is classified by what gen recorded writing. GenStateDir = Path.Combine(projectRoot, ".metaobjects", ".gen-state"), ProjectRoot = projectRoot, @@ -968,7 +1017,7 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar ForeignKeyIndexes = neutralConfig?.MigrateForeignKeyIndexes ?? false, SchemaConventions = neutralConfig?.MigrateConventions, }.WithCSharpLayout(neutralConfig); - var result = CodegenDrift.Compute(config, load.Root, suite, narrowed: a.Selection is { Count: > 0 }); + var result = CodegenDrift.Compute(config, load.Root, suite, narrowed: a.Selection is { Count: > 0 } || a.Entities is not null); if (result.Error is not null) { Console.Error.WriteLine($" {result.Error}"); @@ -985,7 +1034,7 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar // every column and table name, so regenerating would commit the wrong names. When // another naming reproduces the committed tree exactly, say that instead of "regenerate". var naming = load.Root.PhysicalNaming; - if (MatchingNaming(config, () => meta.Load(strict: !a.Lax).Root, suite, a.Selection is { Count: > 0 }, naming) is { } committed) + if (MatchingNaming(config, () => meta.Load(strict: !a.Lax).Root, suite, a.Selection is { Count: > 0 } || a.Entities is not null, naming) is { } committed) { Console.Error.WriteLine(NamingDiffers(committed, naming)); return 1; @@ -993,7 +1042,7 @@ private static int RunVerifyArgs(string[] rest, IReadOnlyList declar // The same for the wire setting: it reaches every decimal property's converter and every // value object's null handling, so a regen under the wrong one changes the API's JSON. var wire = load.Root.WireFormat; - if (MatchingWire(config, () => meta.Load(strict: !a.Lax).Root, suite, a.Selection is { Count: > 0 }, wire) is { } committedWire) + if (MatchingWire(config, () => meta.Load(strict: !a.Lax).Root, suite, a.Selection is { Count: > 0 } || a.Entities is not null, wire) is { } committedWire) { Console.Error.WriteLine(WireDiffers(committedWire, wire)); return 1; diff --git a/server/csharp/MetaObjects.Codegen/CodegenRunner.cs b/server/csharp/MetaObjects.Codegen/CodegenRunner.cs index c55090c5f3..02aa16bc21 100644 --- a/server/csharp/MetaObjects.Codegen/CodegenRunner.cs +++ b/server/csharp/MetaObjects.Codegen/CodegenRunner.cs @@ -92,13 +92,16 @@ internal static RunResult Run( // per recorded file. using var manifestBatch = HashManifest.Batch(config.GenStateDir); + var selection = config.EntitySelection is { } names + ? names.ToHashSet(StringComparer.Ordinal) : null; + foreach (var generator in generators) { var genRoot = outRoot(generator); // An entry with its own directory reports its files by that directory, so a reader // can tell the api reference's README.md from one under --out. var shown = generator.OutDir is { Length: > 0 } own ? own.TrimEnd('/', '\\') + "/" : ""; - foreach (var generated in generator.Generate(ctx)) + foreach (var generated in Emit(generator, ctx, selection)) { var file = FrameworkQualifier.Apply(config, generated); var full = Path.GetFullPath(Path.Combine(genRoot, file.Path)); @@ -173,6 +176,38 @@ internal static RunResult Run( return new RunResult(results, warnings) { OverwritePaths = overwritePaths }; } + /// + /// What emits, narrowed to (the + /// --entities short names) when there is one. + /// + /// The rule: only PER-OBJECT output narrows; a file built from the whole model is rendered from + /// the whole model. A generator already knows which of its files belong to which object (it + /// builds them from ), so the runner does not guess from file + /// names. It asks twice: once with the whole model, and once with a context reduced to the + /// selected objects (, the mechanism an entry's + /// Include uses). The files written are the WHOLE-model run's, restricted to the paths the + /// narrowed run also produced. So an unselected object's file (absent when narrowed) is dropped; + /// a selected object's file is kept; and a shared file, such as the AppDbContext, a shared + /// enum or a helper, is the whole-model rendering, never the narrowed one. A generator that walks + /// the model root rather than the entity list (template, docs, requirement tests) ignores the + /// narrowing, produces the same paths both times, and is not narrowed. + /// + /// + /// A generator answering skips the probe: its single file would + /// otherwise vanish from the narrowed run whenever the selection contributes nothing to it. A shared + /// file inside a per-object generator () is kept the same way. + /// + /// + private static IEnumerable Emit(IGenerator generator, GenContext ctx, HashSet selection) + { + if (selection is null) return generator.Generate(ctx); + var all = generator.Generate(ctx).ToList(); + if (generator.WholeModel) return all; + var kept = generator.Generate(ctx.NarrowForProbe(o => selection.Contains(o.Name))) + .Select(f => f.Path).ToHashSet(StringComparer.Ordinal); + return all.Where(f => f.WholeModel || kept.Contains(f.Path)).ToList(); + } + /// /// Decide and perform the write for one file. /// diff --git a/server/csharp/MetaObjects.Codegen/Generator.cs b/server/csharp/MetaObjects.Codegen/Generator.cs index 43124727ba..f239041b04 100644 --- a/server/csharp/MetaObjects.Codegen/Generator.cs +++ b/server/csharp/MetaObjects.Codegen/Generator.cs @@ -8,7 +8,15 @@ namespace MetaObjects.Codegen; /// A file a generator produced. is relative to the out dir. -public sealed record EmittedFile(string Path, string Content); +public sealed record EmittedFile(string Path, string Content) +{ + /// + /// true for a file built from the whole model (a shared enum, a wire helper, the report + /// export writer): an --entities selection never drops it, because no selected object owns + /// it. false, the default, is a per-object file the selection narrows. + /// + public bool WholeModel { get; init; } +} /// Resolved generation config. public sealed record GenConfig @@ -187,6 +195,18 @@ public string Namespace /// public bool SweepOrphans { get; init; } = false; + /// + /// --entities: the short names () of the objects + /// this run emits PER-OBJECT files for. null, the default, emits every object. + /// + /// Only per-object output narrows (see ): a file of a generator that + /// builds from the whole model, such as AppDbContext, is rendered from the WHOLE model, + /// byte-identical to an unfiltered run. A narrowed run never sweeps orphans and never touches the + /// manifest entries of files it did not write, so the unselected objects' files stay as they are. + /// + /// + public IReadOnlyList? EntitySelection { get; init; } + /// /// The C# type name of the EF Core DbContext that callable (stored-proc / table-function) /// wrappers take as their first parameter. Defaults to AppDbContext (the name the @@ -517,6 +537,24 @@ public GenContext Narrow(Func? include) => Applicability = Applicability, Scope = Scope is null ? include : o => Scope(o) && include(o), }; + + /// + /// for a probe run: the warnings are dropped and the name claims are the + /// probe's own, so asking a generator what it would emit for a selection neither repeats its + /// warnings nor records claims in the real run's ledger. + /// + internal GenContext NarrowForProbe(Func include) => + new() + { + Entities = Entities.Where(include).ToList(), + Root = Root, + Config = Config, + Warn = _ => { }, + Claims = new NameClaims(), + Generators = Generators, + Applicability = Applicability, + Scope = Scope is null ? include : o => Scope(o) && include(o), + }; } /// @@ -582,6 +620,16 @@ public interface IGenerator /// ?format=csv|json|xml branch: see . /// int? ReportExportMaxRows => null; + + /// + /// true when this entry's files are built from the WHOLE model (the AppDbContext), + /// so an --entities selection never narrows them: they render from every object and are + /// written even when no selected object contributes to them. false, the default, has the + /// runner ask the entry for its output twice, once for the whole model and once for the + /// selection, and keep the files the selection also produces (). An + /// owned copy of a whole-model generator keeps the override when it is copied. + /// + bool WholeModel => false; } /// A generator entry's handEdits setting: what a run does with a hand-edited file. @@ -616,6 +664,8 @@ internal sealed class HandEditsGenerator(IGenerator inner, HandEdits handEdits) /// public int? ReportExportMaxRows => Inner.ReportExportMaxRows; /// + public bool WholeModel => Inner.WholeModel; + /// public IEnumerable Generate(GenContext ctx) => Inner.Generate(ctx); } diff --git a/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs b/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs index cd08e36539..0e86d0e0bc 100644 --- a/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs +++ b/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs @@ -30,6 +30,9 @@ public class DbContextGenerator : IGenerator { public virtual string Name => "dbcontext-generator"; + /// One context for the whole model: an --entities selection does not narrow it. + public bool WholeModel => true; + /// /// Whether OnModelCreating carries the explicit 1:N relationship configuration for each /// enforced identity.reference (ADR-0047 / #294). true by default — that is what diff --git a/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs b/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs index e279c959b3..56c7fb3545 100644 --- a/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs +++ b/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs @@ -1161,7 +1161,7 @@ protected virtual IEnumerable EmitSharedEnumFiles(GenContext ctx, I sb.AppendLine(); sb.Append(SharedEnumDeclaration(e)); var fileName = $"{e.Name}.g.cs"; - return new EmittedFile(e.Declaration is { } decl ? OutputPaths.Of(decl, ctx, fileName) : fileName, sb.ToString()); + return new EmittedFile(e.Declaration is { } decl ? OutputPaths.Of(decl, ctx, fileName) : fileName, sb.ToString()) { WholeModel = true }; }).ToList(); var flat = new StringBuilder(); @@ -1191,7 +1191,7 @@ protected virtual IEnumerable EmitSharedEnumFiles(GenContext ctx, I flat.AppendLine(); } } - return [new EmittedFile("Enums.g.cs", flat.ToString())]; + return [new EmittedFile("Enums.g.cs", flat.ToString()) { WholeModel = true }]; } /// @@ -2130,7 +2130,7 @@ public override void Write(Utf8JsonWriter writer, IList value, JsonSeri """; private static EmittedFile EmitDecimalWireFile(string ns, string fileName) => - new(fileName, DecimalWireSourceTemplate.Replace("__NS__", ns, StringComparison.Ordinal) + "\n"); + new(fileName, DecimalWireSourceTemplate.Replace("__NS__", ns, StringComparison.Ordinal) + "\n") { WholeModel = true }; // field.duration — the duration wire form (fixtures/duration-wire/). This text is the // Runtime/DurationWire.cs source with its namespace as __NS__; DurationWireTests gates the two @@ -2398,7 +2398,7 @@ public override void Write(Utf8JsonWriter writer, IList value, JsonSer """; private static EmittedFile EmitDurationWireFile(string ns, string fileName) => - new(fileName, DurationWireSourceTemplate.Replace("__NS__", ns, StringComparison.Ordinal) + "\n"); + new(fileName, DurationWireSourceTemplate.Replace("__NS__", ns, StringComparison.Ordinal) + "\n") { WholeModel = true }; // true iff FIELD is a field.duration (single or @isArray): a System.TimeSpan property carrying // the per-property ISO-8601 converter. @@ -2544,7 +2544,7 @@ private static EmittedFile EmitNetValidationFile(string ns, string fileName) sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine("}"); - return new EmittedFile(fileName, sb.ToString()); + return new EmittedFile(fileName, sb.ToString()) { WholeModel = true }; } // One property's declaration line, from the two hooks: `public { }` diff --git a/server/csharp/MetaObjects.Codegen/Generators/ReportExportGenerator.cs b/server/csharp/MetaObjects.Codegen/Generators/ReportExportGenerator.cs index 097f601945..25f12d8dbc 100644 --- a/server/csharp/MetaObjects.Codegen/Generators/ReportExportGenerator.cs +++ b/server/csharp/MetaObjects.Codegen/Generators/ReportExportGenerator.cs @@ -80,7 +80,7 @@ public IEnumerable Generate(GenContext ctx) .ToList(); if (served.Count == 0) yield break; - yield return new EmittedFile("ReportExportWriter.g.cs", SharedWriter(ctx, HasDurationColumn(served))); + yield return new EmittedFile("ReportExportWriter.g.cs", SharedWriter(ctx, HasDurationColumn(served))) { WholeModel = true }; foreach (var report in served) yield return new EmittedFile(OutputPaths.Of(report, ctx, CSharpNaming.Pascal(report.Name) + "Export.g.cs"), ReportModule(report, ctx)); } diff --git a/server/java/codegen-base/src/main/java/com/metaobjects/generator/GeneratorBase.java b/server/java/codegen-base/src/main/java/com/metaobjects/generator/GeneratorBase.java index 98d06d2752..6c2b64fe58 100644 --- a/server/java/codegen-base/src/main/java/com/metaobjects/generator/GeneratorBase.java +++ b/server/java/codegen-base/src/main/java/com/metaobjects/generator/GeneratorBase.java @@ -3,6 +3,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.metaobjects.MetaData; +import com.metaobjects.generator.util.GeneratorUtil; + import java.io.File; import java.util.*; @@ -34,6 +37,7 @@ public abstract class GeneratorBase implements Generator { private Map args = new HashMap<>(); private MetaDataFilters filters = new MetaDataFilters(); private List scripts = new ArrayList<>(); + private Set entities = Set.of(); @Override public GeneratorBase setArgs(Map argMap) { @@ -85,6 +89,50 @@ protected List getFilters() { return filters.getFilters(); } + /** + * The run's entity selection: short object names ({@code Program}, not + * {@code fitness::Program}), as {@code meta gen Program} takes them in TypeScript and + * {@code --entities} in Python and C#. Empty, the default, selects every object. Set by + * the Maven plugin from {@code } / {@code -Dmeta.entities}. + * + *

Unlike {@code }, which an adopter configures per generator to decide what it + * covers, this is a choice about one run: regenerate these objects' files and leave the + * rest alone. So it narrows per-object output only. A file built from the whole model (a + * registry, a shared support class, a validator listing every table) still renders from + * the whole model, or a narrowed run would rewrite it without the other objects.

+ */ + public GeneratorBase setEntities(List entities) { + this.entities = entities == null ? Set.of() : Set.copyOf(entities); + return this; + } + + public Set getEntities() { + return entities; + } + + /** + * True when {@code md} passes this generator's {@code }. A loop that builds one + * whole-model file from a list of objects asks this; a per-object loop asks + * {@link #isSelected}. + */ + protected boolean passesFilters(MetaData md) { + return GeneratorUtil.passesFilters(md, filters); + } + + /** + * True when this generator should write {@code md}'s own files this run: it passes the + * generator's {@code } and the run's entity selection ({@link #setEntities}). + * Every per-object loop asks this of the object it is about to emit. + */ + protected boolean isSelected(MetaData md) { + return passesFilters(md) && inEntitySelection(md); + } + + /** True when the run selects no entities, or selects {@code md} by its short name. */ + protected boolean inEntitySelection(MetaData md) { + return entities.isEmpty() || entities.contains(md.getShortName()); + } + @Override public GeneratorBase setScripts(List scripts) { this.scripts = scripts; @@ -124,6 +172,7 @@ public String toString() { sb.append("args=").append(getArgs()); sb.append(", filters=").append(getFilters()); sb.append(", scripts=").append(getScripts()); + if (!entities.isEmpty()) sb.append(", entities=").append(new TreeSet<>(entities)); sb.append('}'); return sb.toString(); } diff --git a/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/MultiFileDirectGeneratorBase.java b/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/MultiFileDirectGeneratorBase.java index 07bad951b8..42281529b3 100644 --- a/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/MultiFileDirectGeneratorBase.java +++ b/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/MultiFileDirectGeneratorBase.java @@ -50,6 +50,10 @@ public void execute( MetaDataLoader loader ) { // Write each File for( M md : metadata ) { + // already narrowed `metadata`; the run's entity selection narrows + // only these per-object files. The final file below still sees the whole set. + if ( !inEntitySelection( md )) continue; + filename = md.getName(); String path = getSingleOutputFilePath( md ); diff --git a/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/object/javacode/ExtractorCodeGenerator.java b/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/object/javacode/ExtractorCodeGenerator.java index aaa48bb669..2703343195 100644 --- a/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/object/javacode/ExtractorCodeGenerator.java +++ b/server/java/codegen-base/src/main/java/com/metaobjects/generator/direct/object/javacode/ExtractorCodeGenerator.java @@ -93,7 +93,7 @@ public void execute(MetaDataLoader loader) { try { for (MetaObject mo : GeneratorUtil.getFilteredMetaData( loader, MetaObject.class, getMetaDataFilters())) { - if (IOUtil.isAbstract(mo)) { + if (IOUtil.isAbstract(mo) || !inEntitySelection(mo)) { continue; } emit(outputDir, namer.getLanguagePackage(mo), namer.getClassName(mo), mo.getName()); diff --git a/server/java/codegen-base/src/main/java/com/metaobjects/generator/template/TemplateScopeGenerator.java b/server/java/codegen-base/src/main/java/com/metaobjects/generator/template/TemplateScopeGenerator.java index 06ed7635c2..a9bfb2d3cf 100644 --- a/server/java/codegen-base/src/main/java/com/metaobjects/generator/template/TemplateScopeGenerator.java +++ b/server/java/codegen-base/src/main/java/com/metaobjects/generator/template/TemplateScopeGenerator.java @@ -15,6 +15,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.stream.Collectors; /** * Maven-wirable declarative Mustache template generator (SP-1b). Reads the @@ -59,10 +60,13 @@ public void execute(MetaDataLoader loader) { File outDir = getOutputDir(); List objects = com.metaobjects.generator.util.GeneratorUtil.ownMetaObjects(loader); + List walked = ScopeWalk.PER_ENTITY.equals(scope) + ? objects.stream().filter(this::inEntitySelection).collect(Collectors.toList()) + : objects; List files = TemplateGenerator.generate( scope, template, - root -> ScopeWalk.forScope(scope, outputPattern).apply(objects), + root -> ScopeWalk.forScope(scope, outputPattern).apply(walked), provider, format, objects); diff --git a/server/java/codegen-base/src/main/java/com/metaobjects/generator/util/GeneratorUtil.java b/server/java/codegen-base/src/main/java/com/metaobjects/generator/util/GeneratorUtil.java index a10cfb9a4b..21d731e014 100644 --- a/server/java/codegen-base/src/main/java/com/metaobjects/generator/util/GeneratorUtil.java +++ b/server/java/codegen-base/src/main/java/com/metaobjects/generator/util/GeneratorUtil.java @@ -74,47 +74,33 @@ public static boolean isReport(MetaData md) { return md instanceof MetaObject && MetaObject.SUBTYPE_REPORT.equals(md.getSubType()); } - private static Collection filterMetaData( Collection in, Class clazz, MetaDataFilters filtersIn ) { - - List out = new ArrayList<>(); - - List filters = filtersIn==null?null:filtersIn.getFilters(); - - if ( filters == null || filters.isEmpty() ) { - out.addAll(in); - } - else { - List incFilters = filters.stream() - .filter( f -> !f.startsWith("!")) - .collect(Collectors.toList()); - List excFilters = filters.stream() - .filter( f -> f.startsWith("!")) - .collect(Collectors.toList()); - - for (T md : in) { - - boolean shouldAdd = incFilters.isEmpty(); - - // Check inclusive filters - for (String f : incFilters) { - if ( filterMatch(md, f)) { - shouldAdd = true; - break; - } - } - - // Check inclusive filters - for (String f : excFilters) { - if ( filterMatch(md, f.substring(1))) { - shouldAdd = false; - break; - } - } - - if (shouldAdd) out.add(md); + /** + * True when {@code md} passes {@code filtersIn}: no inclusive filter, or one that matches, + * and no {@code !}-exclusion that matches. The per-object test {@link #getFilteredMetaData} + * applies to a whole collection. + */ + public static boolean passesFilters( MetaData md, MetaDataFilters filtersIn ) { + List filters = filtersIn == null ? null : filtersIn.getFilters(); + if ( filters == null || filters.isEmpty() ) return true; + + boolean included = filters.stream().allMatch( f -> f.startsWith("!") ); + for (String f : filters) { + if ( !f.startsWith("!") && filterMatch(md, f) ) { + included = true; + break; } } + for (String f : filters) { + if ( f.startsWith("!") && filterMatch(md, f.substring(1)) ) return false; + } + return included; + } + private static Collection filterMetaData( Collection in, Class clazz, MetaDataFilters filtersIn ) { + List out = new ArrayList<>(); + for (T md : in) { + if ( passesFilters(md, filtersIn) ) out.add(md); + } return out; } diff --git a/server/java/codegen-base/src/test/java/com/metaobjects/generator/template/TemplateScopeGeneratorSelectionTest.java b/server/java/codegen-base/src/test/java/com/metaobjects/generator/template/TemplateScopeGeneratorSelectionTest.java new file mode 100644 index 0000000000..a5e9eb68db --- /dev/null +++ b/server/java/codegen-base/src/test/java/com/metaobjects/generator/template/TemplateScopeGeneratorSelectionTest.java @@ -0,0 +1,68 @@ +package com.metaobjects.generator.template; + +import com.metaobjects.generator.GeneratorBase; +import com.metaobjects.loader.MetaDataLoader; +import com.metaobjects.loader.MetaDataLoaderTestBase; +import org.junit.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** + * Oracle: the shared template-codegen corpus, whose {@code expected/} tree names the file each + * scope writes for the full model. A per-entity scope narrowed to one object writes that + * object's file only; per-package and per-model files stay whole. + */ +public class TemplateScopeGeneratorSelectionTest extends MetaDataLoaderTestBase { + + private static Path corpus() { + return Path.of(System.getProperty("user.dir")).resolve("../../..") + .resolve("fixtures/template-codegen-conformance").normalize(); + } + + private List run(String scope, String pattern, List entities) throws Exception { + Path corpus = corpus(); + MetaDataLoader loader = initLoader(List.of(corpus.resolve("metadata/meta.shop.json").toUri())); + Path out = Files.createTempDirectory("tmpl-select-java"); + String template = switch (scope) { + case ScopeWalk.PER_ENTITY -> "entity"; + case ScopeWalk.PER_PACKAGE -> "package"; + default -> "model"; + }; + TemplateScopeGenerator gen = new TemplateScopeGenerator(); + gen.setArgs(Map.of( + TemplateScopeGenerator.ARG_TEMPLATE, template, + TemplateScopeGenerator.ARG_SCOPE, scope, + TemplateScopeGenerator.ARG_OUTPUT_PATTERN, pattern, + TemplateScopeGenerator.ARG_TEMPLATES_DIR, corpus.resolve("templates").toString(), + GeneratorBase.ARG_OUTPUTDIR, out.toString())); + gen.setEntities(entities); + gen.execute(loader); + try (Stream s = Files.walk(out)) { + return s.filter(Files::isRegularFile) + .map(p -> out.relativize(p).toString()) + .sorted() + .collect(Collectors.toList()); + } + } + + @Test public void perEntityScopeWritesOnlyTheSelectedObject() throws Exception { + assertEquals(List.of("Product.txt"), run(ScopeWalk.PER_ENTITY, "{name}.txt", List.of("Product"))); + } + + @Test public void perEntityScopeWithNoSelectionWritesEveryObject() throws Exception { + assertEquals(List.of("Order.txt", "Product.txt"), run(ScopeWalk.PER_ENTITY, "{name}.txt", List.of())); + } + + @Test public void wholeModelScopesStayWholeUnderASelection() throws Exception { + assertEquals(List.of("_model.txt"), run(ScopeWalk.PER_MODEL, "_model.txt", List.of("Product"))); + assertEquals(List.of("shop/_package.txt"), + run(ScopeWalk.PER_PACKAGE, "{package}/_package.txt", List.of("Product"))); + } +} diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt index 9edb8085c4..05686395ed 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt @@ -113,6 +113,7 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase() { // Local-only abstract check (own attribute, not inherited) so concrete subtypes // extending an abstract base still emit normally. for (declared in loader.metaObjects) { + if (!isSelected(declared)) continue // FR-044: a served report is emitted from its read model (one field per derived // column); any other report maps to null and emits nothing. val obj = RestSurfaceGate.restShapeOf(declared) ?: continue diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt index 6046f1a96e..99ebd08d80 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt @@ -184,6 +184,7 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase() for (entity in GeneratorUtil.ownMetaObjects(loader)) { + if (!isSelected(entity)) continue // FR-044: a served report (a view-backed one) gets a names artifact built from its // READ MODEL, so the derived columns have constants for hand-written SQL over the // view to use; its Exposed table still binds by literal. A report that is not diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRelationsGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRelationsGenerator.kt index 07fbf9de42..8c1833ef5d 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRelationsGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRelationsGenerator.kt @@ -95,6 +95,7 @@ open class KotlinRelationsGenerator : MultiFileDirectGeneratorBase() val outRoot = Paths.get(outDir.absolutePath) for (entity in GeneratorUtil.ownMetaObjects(loader)) { + if (!isSelected(entity)) continue if (entity.subType != MetaObject.SUBTYPE_ENTITY) continue // Abstract entities are inheritance scaffolding — never emit relation helpers. if (KotlinGenUtil.isAbstractEntity(entity)) continue diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinReportExportGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinReportExportGenerator.kt index 9d649e485e..3c85329c36 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinReportExportGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinReportExportGenerator.kt @@ -85,6 +85,9 @@ open class KotlinReportExportGenerator : MultiFileDirectGeneratorBase() for (declared in loader.metaObjects) { + // The writer is shared by every served report, so it is built from all of them: + // narrow that set, the run's entity selection only the per-report files. + if (!passesFilters(declared)) continue // A served report is emitted from its read model; any other report has no shape. val entity = RestSurfaceGate.restShapeOf(declared) ?: continue if (!GeneratorUtil.isReport(entity)) continue @@ -104,6 +107,7 @@ open class KotlinReportExportGenerator : MultiFileDirectGeneratorBase( val outRoot = Paths.get(outDir.absolutePath) for (entity in GeneratorUtil.ownMetaObjects(loader)) { + if (!isSelected(entity)) continue if (entity.subType != MetaObject.SUBTYPE_ENTITY) continue if (KotlinGenUtil.isAbstractEntity(entity)) continue // TODO(FR-035 Phase 2): a TPH subtype folds into its base's table — the base's diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt index d0838ce5c4..2fbe752b15 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt @@ -195,6 +195,7 @@ open class KotlinSpringControllerGenerator : MultiFileDirectGeneratorBase( val outRoot = Paths.get(outDir.absolutePath) for (entity in GeneratorUtil.ownMetaObjects(loader)) { + if (!isSelected(entity)) continue // FR-024: a stored-proc binding is gated on the SOURCE @kind, not the object // subtype — a proc-backed object.projection (the post-B4b spelling) emits a // callable just like the legacy proc-backed entity did. diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinValidatorGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinValidatorGenerator.kt index 90241aade0..53022c151d 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinValidatorGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinValidatorGenerator.kt @@ -52,7 +52,10 @@ open class KotlinValidatorGenerator : MultiFileDirectGeneratorBase() ?: throw GeneratorException("KotlinValidatorGenerator (\"validator\"): packageName is required") val outRoot = Paths.get(outDir.absolutePath) + // One registry of every table, so it is built from the whole model: narrow it, + // the run's entity selection does not (a narrowed run would drop the other tables). val entries = GeneratorUtil.ownMetaObjects(loader) + .filter { passesFilters(it) } .filter { it.subType == MetaObject.SUBTYPE_ENTITY } // Abstract entities are inheritance scaffolding — never register a validator for them. .filter { !KotlinGenUtil.isAbstractEntity(it) } diff --git a/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinSelectionTest.kt b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinSelectionTest.kt new file mode 100644 index 0000000000..96dcb10ea7 --- /dev/null +++ b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinSelectionTest.kt @@ -0,0 +1,131 @@ +package com.metaobjects.generator.kotlin + +import com.metaobjects.generator.Generator +import com.metaobjects.generator.GeneratorBase +import com.metaobjects.generator.util.GeneratedFileWriter +import com.metaobjects.metadata.ktx.loadDirectory +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.isRegularFile +import kotlin.io.path.readText +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * Selection in the Kotlin port: a generator's `` and the run's entity selection + * (`-Dmeta.entities`) both narrow the objects it writes files for, over the shared canonical + * model. + * + * Until 1.3.0 every Kotlin generator iterated the whole model and ignored ``. The + * selection names an entity (`Person`), a value object (`Settings`) and a served report + * (`ProgramMinutes`), so each generator has something to emit. + */ +class KotlinSelectionTest { + + private val selected = listOf("Person", "Settings", "ProgramMinutes") + private val filters = selected.map { "fitness::$it" } + + /** Objects outside the selection: no file may be named after one in a narrowed run. */ + private val unselected = listOf("Week", "Tag", "AllTypes", "Gadget", "Post", "Asset", "Session") + + private fun generators(): List> = listOf( + "entity" to KotlinEntityGenerator(), + "table" to KotlinExposedTableGenerator(), + "names" to KotlinNamesGenerator(), + "relations" to KotlinRelationsGenerator(), + "filter-allowlist" to KotlinFilterAllowlistGenerator(), + "validator" to KotlinValidatorGenerator(), + "repository" to KotlinRepositoryGenerator(), + "controller" to KotlinSpringControllerGenerator(), + "stored-proc" to KotlinStoredProcGenerator(), + "report-export" to KotlinReportExportGenerator(), + ) + + private fun findCorpusRoot(): Path { + var cur: Path? = Paths.get("").toAbsolutePath() + while (cur != null) { + val candidate = cur.resolve("fixtures/persistence-conformance") + if (Files.isDirectory(candidate)) return candidate + cur = cur.parent + } + throw IllegalStateException("Could not locate fixtures/persistence-conformance") + } + + /** Run every generator over the canonical model; return each file's relative path and content. */ + private fun generate(narrow: (Generator) -> Unit): Map { + val canonical = findCorpusRoot().resolve("canonical") + val outDir = Files.createTempDirectory("kotlin-selection-") + try { + // One loader name for every run: some output embeds it. + val loader = loadDirectory("fitness", canonical) + GeneratedFileWriter.beginRun().use { run -> + for ((key, gen) in generators()) { + run.attributeTo(gen.javaClass.simpleName) + gen.setArgs(mapOf( + // Its own directory each: this compares outputs, not a project. + "outputDir" to outDir.resolve(key).toString(), + "packageName" to "fitness", + )) + narrow(gen) + gen.execute(loader) + } + } + return Files.walk(outDir).use { s -> + s.filter { it.isRegularFile() }.toList() + .associate { outDir.relativize(it).toString() to it.readText() } + .toSortedMap() + } + } finally { + outDir.toFile().deleteRecursively() + } + } + + /** + * [wholeModelUnchanged] is true for the entity selection, which must leave every + * whole-model file exactly as a full run writes it, and false for ``, which + * narrow what a whole-model file covers as well. + */ + private fun assertNarrowed(how: String, full: Map, narrowed: Map, + wholeModelUnchanged: Boolean) { + assertFalse(narrowed.isEmpty(), "$how: wrote nothing") + assertTrue(narrowed.size < full.size, "$how: wrote ${narrowed.size} of ${full.size} files; expected fewer") + for ((path, content) in narrowed) { + assertTrue(full.containsKey(path), "$how: wrote $path, which a full run does not") + val file = Path.of(path).fileName.toString() + if (wholeModelUnchanged || selected.any { file.startsWith(it) }) { + assertEquals(full[path], content, "$how: $path differs from a full run") + } + for (other in unselected) { + assertFalse(file.startsWith(other), "$how: wrote $path for an object outside the selection") + } + } + for (name in selected) { + assertTrue(narrowed.keys.any { Path.of(it).fileName.toString().startsWith(name) }, + "$how: wrote no file for $name") + } + assertTrue(narrowed.keys.any { it.endsWith("PersonController.kt") }, "$how: a per-object controller is missing") + } + + @Test + fun filtersNarrowEveryGenerator() { + val full = generate { } + val filtered = generate { it.setFilters(filters) } + assertNarrowed("", full, filtered, wholeModelUnchanged = false) + } + + @Test + fun theEntitySelectionNarrowsPerObjectOutput() { + val full = generate { } + val narrowed = generate { (it as GeneratorBase).setEntities(selected) } + assertNarrowed("entities", full, narrowed, wholeModelUnchanged = true) + } + + @Test + fun anEmptySelectionSelectsEverything() { + val full = generate { } + assertEquals(full, generate { (it as GeneratorBase).setEntities(emptyList()) }) + } +} diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/LlmTraceHelperGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/LlmTraceHelperGenerator.java index c4ef5bc5c1..603b733cd8 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/LlmTraceHelperGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/LlmTraceHelperGenerator.java @@ -134,7 +134,7 @@ public void execute(MetaDataLoader loader) { // Stable name order — deterministic emission, matching the other generators. java.util.List entities = new java.util.ArrayList<>(); for (MetaObject mo : GeneratorUtil.ownMetaObjects(loader)) { - if (!appliesTo(mo)) continue; + if (!appliesTo(mo) || !isSelected(mo)) continue; entities.add(mo); } entities.sort(Comparator.comparing(MetaObject::getName)); diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java index 2adee59155..6fb4d47b0b 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java @@ -169,6 +169,7 @@ public void execute(MetaDataLoader loader) { this.loader = loader; Path outRoot = Paths.get(outDir.getAbsolutePath()); for (MetaObject declared : loader.getMetaObjects()) { + if (!isSelected(declared)) continue; // FR-044: a served report is emitted from its read model; any other report // has no shape and emits nothing. MetaObject entity = RestSurfaceGate.restShapeOf(declared); diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringDtoGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringDtoGenerator.java index b0698847be..84d7ad0491 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringDtoGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringDtoGenerator.java @@ -131,6 +131,7 @@ public void execute(MetaDataLoader loader) { Fr019SharedEnum.emitMaterialized(loader, outRoot); boolean emitAbstractShapes = Boolean.parseBoolean(getArg("emitAbstractShapes", "false")); for (MetaObject declared : loader.getMetaObjects()) { + if (!isSelected(declared)) continue; // FR-044: a served report's row DTO is emitted from its read model (one // component per derived field) and nothing else is: no Patch, no stamping // helper, no builder. Any other report has no shape and emits nothing. diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringFilterAllowlistGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringFilterAllowlistGenerator.java index b89019041a..af907a56d5 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringFilterAllowlistGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringFilterAllowlistGenerator.java @@ -82,6 +82,7 @@ public void execute(MetaDataLoader loader) { parseArgs(); Path outRoot = Paths.get(outDir.getAbsolutePath()); for (MetaObject declared : loader.getMetaObjects()) { + if (!isSelected(declared)) continue; // FR-044: a served report is emitted from its read model, whose derived fields // all carry @filterable (Table C), so there is no report branch below. Any // other report has no shape and emits nothing. diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNamesGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNamesGenerator.java index 0173de7d6b..2b6c90139f 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNamesGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNamesGenerator.java @@ -135,6 +135,7 @@ public void execute(MetaDataLoader loader) { // Pass 1 — every object that participates in the database (#248). Set emitted = new HashSet<>(); for (MetaObject entity : GeneratorUtil.ownMetaObjects(loader)) { + if (!isSelected(entity)) continue; // FR-044 Plan 1: object.report has no output until its lowering lands (Plan 2/3). if (GeneratorUtil.isReport(entity)) continue; if (emit(entity, outRoot, strategy, false)) emitted.add(entity.getName()); diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringReportExportGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringReportExportGenerator.java index 0130cb7b90..9b6cdba789 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringReportExportGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringReportExportGenerator.java @@ -152,6 +152,9 @@ public void execute(MetaDataLoader loader) { List served = new ArrayList<>(); for (MetaObject declared : loader.getMetaObjects()) { + // The writer is shared by every served report, so it is built from all of them: + // narrow that set, the run's entity selection only the per-report files. + if (!passesFilters(declared)) continue; // A served report is emitted from its read model; any other report has no shape. MetaObject entity = RestSurfaceGate.restShapeOf(declared); if (entity == null || !GeneratorUtil.isReport(entity)) continue; @@ -166,6 +169,7 @@ public void execute(MetaDataLoader loader) { write(outRoot, writerPackage, WRITER_NAME, writerSource(writerPackage, xmlNulls, csvBom, decimalsAsString, usesDurationCell(served))); for (MetaObject entity : served) { + if (!inEntitySelection(entity)) continue; String[] split = SpringNaming.splitFqn(entity.getName()); write(outRoot, split[0], split[1] + "Export", exportSource(entity, split[0], split[1], writerPackage, maxRows)); diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringRepositoryGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringRepositoryGenerator.java index e2bd22cd9d..e4af346d9a 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringRepositoryGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringRepositoryGenerator.java @@ -90,6 +90,7 @@ public void execute(MetaDataLoader loader) { this.loader = loader; Path outRoot = Paths.get(outDir.getAbsolutePath()); for (MetaObject declared : loader.getMetaObjects()) { + if (!isSelected(declared)) continue; // FR-044: a served report is emitted from its read model; any other report // has no shape and emits nothing. MetaObject entity = RestSurfaceGate.restShapeOf(declared); diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringValueObjectGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringValueObjectGenerator.java index 984b71f5fb..75aa7ef581 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringValueObjectGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringValueObjectGenerator.java @@ -108,6 +108,7 @@ public void execute(MetaDataLoader loader) { } requestBodies = RestSurfaceGate.requestBodyShapes(loader); for (MetaObject vo : valueShapes(loader)) { + if (!isSelected(vo)) continue; emit(vo, outRoot); } } diff --git a/server/java/codegen-spring/src/test/java/com/metaobjects/generator/spring/SpringSelectionTest.java b/server/java/codegen-spring/src/test/java/com/metaobjects/generator/spring/SpringSelectionTest.java new file mode 100644 index 0000000000..8fd4509a65 --- /dev/null +++ b/server/java/codegen-spring/src/test/java/com/metaobjects/generator/spring/SpringSelectionTest.java @@ -0,0 +1,144 @@ +package com.metaobjects.generator.spring; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.metaobjects.generator.Generator; +import com.metaobjects.generator.GeneratorBase; +import com.metaobjects.generator.direct.object.javacode.ExtractorCodeGenerator; +import com.metaobjects.generator.direct.object.javacode.JavaObjectCodeGenerator; +import com.metaobjects.generator.util.GeneratedFileWriter; +import com.metaobjects.loader.MetaDataLoader; +import com.metaobjects.registry.SharedRegistryTestBase; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Consumer; +import java.util.function.Supplier; +import java.util.stream.Stream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Selection in the Java port: a generator's {@code } and the run's entity selection + * ({@code -Dmeta.entities}) both narrow the objects it writes files for, over the shared + * canonical model. + * + *

Until 1.3.0 every generator in this module iterated the whole model and ignored + * {@code }: only the older generators in {@code codegen-base} honoured them. The + * selection here names an entity ({@code Person}), a value object ({@code Settings}) and a + * served report ({@code ProgramMinutes}), so each generator has something to emit.

+ */ +public class SpringSelectionTest extends SharedRegistryTestBase { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private static final List SELECTED = List.of("Person", "Settings", "ProgramMinutes"); + private static final List FILTERS = + List.of("fitness::Person", "fitness::Settings", "fitness::ProgramMinutes"); + + /** Objects outside the selection: no file may be named after one in a narrowed run. */ + private static final List UNSELECTED = List.of("Week", "Tag", "AllTypes", "Gadget", "Post", "Asset", "Session"); + + private static Map> generators() { + Map> g = new LinkedHashMap<>(); + g.put("entity", () -> new JavaObjectCodeGenerator().setArgs(Map.of("type", "class", "flavor", "pojoAware"))); + g.put("extractor", ExtractorCodeGenerator::new); + g.put("value-object", SpringValueObjectGenerator::new); + g.put("dto", SpringDtoGenerator::new); + g.put("repository", SpringRepositoryGenerator::new); + g.put("filter-allowlist", SpringFilterAllowlistGenerator::new); + g.put("names", SpringNamesGenerator::new); + g.put("controller", SpringControllerGenerator::new); + g.put("report-export", SpringReportExportGenerator::new); + return g; + } + + /** Run every generator over the canonical model; return each file's relative path and content. */ + private Map generate(String label, Consumer narrow) throws Exception { + String canonicalMeta = Files.readString( + SpringTestFixtures.findCorpusRoot().resolve("canonical/meta.fitness.json"), StandardCharsets.UTF_8); + Path outDir = tempFolder.newFolder("gen-" + label).toPath(); + MetaDataLoader loader = SpringTestFixtures.loadFixture( + tempFolder.newFolder("ws-" + label).toPath(), "fitness", canonicalMeta); + try (GeneratedFileWriter.Run run = GeneratedFileWriter.beginRun()) { + for (Map.Entry> e : generators().entrySet()) { + Generator gen = e.getValue().get(); + run.attributeTo(gen.getClass().getSimpleName()); + // Its own directory each: the entity and value-object tiers both write a + // class per value object, and this test compares outputs, not a project. + gen.setArgs(Map.of("outputDir", outDir.resolve(e.getKey()).toString())); + narrow.accept(gen); + gen.execute(loader); + } + } + Map out = new TreeMap<>(); + try (Stream s = Files.walk(outDir)) { + for (Path p : (Iterable) s.filter(Files::isRegularFile)::iterator) { + out.put(outDir.relativize(p).toString(), Files.readString(p, StandardCharsets.UTF_8)); + } + } + return out; + } + + /** + * @param wholeModelUnchanged true for the entity selection, which must leave every + * whole-model file exactly as a full run writes it; false for {@code }, + * which narrow what a whole-model file covers as well + */ + private static void assertNarrowed(String how, Map full, Map narrowed, + boolean wholeModelUnchanged) { + assertFalse(how + ": wrote nothing", narrowed.isEmpty()); + assertTrue(how + ": wrote " + narrowed.size() + " of " + full.size() + " files; expected fewer", + narrowed.size() < full.size()); + for (Map.Entry e : narrowed.entrySet()) { + assertTrue(how + ": wrote " + e.getKey() + ", which a full run does not", full.containsKey(e.getKey())); + String file = Path.of(e.getKey()).getFileName().toString(); + if (wholeModelUnchanged || SELECTED.stream().anyMatch(file::startsWith)) { + assertEquals(how + ": " + e.getKey() + " differs from a full run", full.get(e.getKey()), e.getValue()); + } + for (String other : UNSELECTED) { + assertFalse(how + ": wrote " + e.getKey() + " for an object outside the selection", + file.startsWith(other)); + } + } + for (String name : SELECTED) { + assertTrue(how + ": wrote no file for " + name, + narrowed.keySet().stream().anyMatch(k -> Path.of(k).getFileName().toString().startsWith(name))); + } + // The export writer is shared by every served report, so a narrowed run still writes it whole. + assertTrue(how + ": the shared ReportExportWriter is missing", + narrowed.keySet().stream().anyMatch(k -> k.endsWith("ReportExportWriter.java"))); + // So are the controller's per-package wire support classes. + assertTrue(how + ": a per-object controller is missing", + narrowed.keySet().stream().anyMatch(k -> k.endsWith("PersonController.java"))); + } + + @Test + public void filtersNarrowEveryGenerator() throws Exception { + Map full = generate("full", g -> { }); + Map filtered = generate("filtered", g -> g.setFilters(FILTERS)); + assertNarrowed("", full, filtered, false); + } + + @Test + public void theEntitySelectionNarrowsPerObjectOutput() throws Exception { + Map full = generate("full", g -> { }); + Map selected = generate("selected", g -> ((GeneratorBase) g).setEntities(SELECTED)); + assertNarrowed("entities", full, selected, true); + } + + @Test + public void anEmptySelectionSelectsEverything() throws Exception { + Map full = generate("full", g -> { }); + Map unnarrowed = generate("unnarrowed", g -> ((GeneratorBase) g).setEntities(List.of())); + assertEquals(full, unnarrowed); + } +} diff --git a/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/AbstractMetaDataMojo.java b/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/AbstractMetaDataMojo.java index b71c5497f9..04e37e81ed 100644 --- a/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/AbstractMetaDataMojo.java +++ b/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/AbstractMetaDataMojo.java @@ -427,6 +427,21 @@ public void setGenerators(List generators) { */ @Parameter(property = "meta.lax", defaultValue = "false") private boolean lax; + + /** + * How long the metadata load may take, in milliseconds, before the goal fails with a + * timeout ({@code LoaderOptions.setInitTimeoutMs}). Unset keeps the loader's default of + * 30 seconds. Raise it for a very large model on a slow or busy machine: + * {@code -Dmeta.loaderTimeoutMs=120000}. + */ + @Parameter(property = "meta.loaderTimeoutMs") + private Long loaderTimeoutMs; + public Long getLoaderTimeoutMs() { + return loaderTimeoutMs; + } + public void setLoaderTimeoutMs(Long loaderTimeoutMs) { + this.loaderTimeoutMs = loaderTimeoutMs; + } public boolean isLax() { return lax; } @@ -839,10 +854,13 @@ protected MetaDataLoader createLoader(ClassLoader projectClassLoader) { * wins (advanced override). Kept separate from {@link #mergeAndOverwriteArgs} so the * injected flag never pollutes generator args. */ - private Map loaderArgs(boolean strict) { + Map loaderArgs(boolean strict) { Map args = new HashMap<>(); if (globals != null) args.putAll(globals); args.putIfAbsent(LoaderConfigurationConstants.ARG_STRICT, String.valueOf(strict)); + if (loaderTimeoutMs != null) { + args.put(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS, String.valueOf(loaderTimeoutMs)); + } return args; } diff --git a/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/MetaDataGeneratorMojo.java b/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/MetaDataGeneratorMojo.java index 4e343b630d..9201a8e835 100644 --- a/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/MetaDataGeneratorMojo.java +++ b/server/java/maven-plugin/src/main/java/com/metaobjects/mojo/MetaDataGeneratorMojo.java @@ -38,6 +38,20 @@ public class MetaDataGeneratorMojo extends AbstractMetaDataMojo @Parameter(property = "metaobjects.allowUnmetRequires", defaultValue = "false") boolean allowUnmetRequires; + /** + * Regenerate only these objects' files: short object names, comma-separated + * ({@code -Dmeta.entities=Program,Week}, or {@code Program,Week}). The + * same selection as {@code meta gen Program Week} in TypeScript and {@code --entities} in + * Python and C#. Each generator still applies its own {@code } on top. + * + *

It narrows per-object output only: a file built from the whole model (a validator + * listing every table, a shared support class) still renders from the whole model. A + * narrowed run deletes no orphaned file and keeps every other file's manifest entry, since + * it did not try to produce them. A name that matches no object fails the goal.

+ */ + @Parameter(property = "meta.entities") + String entities; + /** How this goal is named in a message telling the adopter what to run. */ static final String OWNER = "mvn metaobjects:generate"; @@ -73,6 +87,20 @@ protected void executeGenerators(MetaDataLoader loader, List generato // metadata — nothing to collide-check in that case. if (loader != null) refuseModelCollisions(loader); + List selected = selectedEntities(loader); + if (selected != null) { + getLog().info("MetaData Mojo > Generating only " + String.join(", ", selected) + + " (meta.entities); files built from the whole model still see every object."); + for (Generator gen : generatorImpls) { + if (gen instanceof GeneratorBase) { + ((GeneratorBase) gen).setEntities(selected); + } else { + getLog().warn("MetaData Mojo > " + gen.getClass().getName() + + " cannot narrow to meta.entities; it renders every object."); + } + } + } + // Where this EXECUTION's codegen hash manifest lives: anchored on the module // basedir, so two executions in one module share it (merged — every entry names // the generator and output dir that emitted it, and a sweep leaves another @@ -132,6 +160,10 @@ protected void executeGenerators(MetaDataLoader loader, List generato if (migrating) { getLog().info("MetaData Mojo > Recorded the generator and output dir of every generated " + "file in `.metaobjects/.gen-state/.hashes.json`; this build deletes no orphaned file."); + } else if (selected != null) { + // A narrowed run did not try to produce the other objects' files, so not + // emitting them says nothing about whether they are orphans. + getLog().info("MetaData Mojo > Deleted no orphaned file: meta.entities narrowed this build."); } else if (genStateDir != null) { sweepOrphans(genStateDir, projectRoot, handEditsByDir, emittedThisBuild, runOwners); } @@ -299,6 +331,35 @@ private void reportManifestNotices(GeneratedFileWriter.Run run, Path genStateDir } } + /** + * The {@link #entities} selection as a list, or {@code null} when none is set. Refuses a + * selection that names nothing (a blank value, usually an unset shell variable, would + * otherwise regenerate everything) and a name that matches no object. + */ + List selectedEntities(MetaDataLoader loader) { + if (entities == null) return null; + List names = new ArrayList<>(); + for (String raw : entities.split(",")) { + String name = raw.trim(); + if (!name.isEmpty() && !names.contains(name)) names.add(name); + } + if (names.isEmpty()) { + throw new MetaDataException(OWNER + ": meta.entities names no object."); + } + if (loader == null) return names; + Set known = new HashSet<>(); + for (com.metaobjects.object.MetaObject o : loader.getMetaObjects()) known.add(o.getShortName()); + List unknown = new ArrayList<>(); + for (String name : names) { + if (!known.contains(name)) unknown.add("'" + name + "'"); + } + if (!unknown.isEmpty()) { + throw new MetaDataException(OWNER + ": meta.entities names " + String.join(", ", unknown) + + ", which matches no object in the metadata."); + } + return names; + } + /** Fail the goal, naming each missing companion, when the list does not declare what its * generators' output references; with {@link #allowUnmetRequires}, warn instead. */ void refuseUnmetRequires(List generatorImpls) { diff --git a/server/java/maven-plugin/src/test/java/com/metaobjects/mojo/MetaDataGeneratorMojoEntitiesTest.java b/server/java/maven-plugin/src/test/java/com/metaobjects/mojo/MetaDataGeneratorMojoEntitiesTest.java new file mode 100644 index 0000000000..f43fe84063 --- /dev/null +++ b/server/java/maven-plugin/src/test/java/com/metaobjects/mojo/MetaDataGeneratorMojoEntitiesTest.java @@ -0,0 +1,138 @@ +package com.metaobjects.mojo; + +import com.metaobjects.MetaDataException; +import com.metaobjects.generator.Generator; +import com.metaobjects.generator.GeneratorBase; +import com.metaobjects.generator.util.HashManifest; +import com.metaobjects.loader.LoaderConfigurationConstants; +import com.metaobjects.loader.MetaDataLoader; +import com.metaobjects.mojo.MetaDataGeneratorMojoManifestTest.ConfigurableFileGenerator; +import org.apache.maven.project.MavenProject; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +/** + * {@code -Dmeta.entities}: which names it accepts, what it refuses, that it reaches every + * generator, and that a narrowed run leaves the files it did not regenerate alone. What a + * narrowed generator writes is covered beside the generators (SpringSelectionTest, + * KotlinSelectionTest). + */ +public class MetaDataGeneratorMojoEntitiesTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private static final String BODY = "/** GENERATED */\npublic class X {}\n"; + + private MetaDataGeneratorMojo mojo; + private Path projectRoot; + + @Before + public void setUp() { + mojo = new MetaDataGeneratorMojo(); + mojo.setLoader(new LoaderParam()); + MavenProject project = Mockito.mock(MavenProject.class); + projectRoot = tempFolder.getRoot().toPath(); + Mockito.when(project.getBasedir()).thenReturn(tempFolder.getRoot()); + mojo.project = project; + } + + private static MetaDataLoader canonicalModel() { + Path dir = Paths.get("").toAbsolutePath(); + while (dir != null) { + Path candidate = dir.resolve("fixtures/persistence-conformance/canonical/meta.fitness.json"); + if (Files.isRegularFile(candidate)) { + return MetaDataLoader.fromUris("entities-test", List.of(candidate.toUri())); + } + dir = dir.getParent(); + } + throw new AssertionError("fixtures/persistence-conformance/canonical/meta.fitness.json not found"); + } + + private List build(String outDir, String filename) { + GeneratorParam p = new GeneratorParam(); + p.setClassname(ConfigurableFileGenerator.class.getName()); + p.setArgs(Map.of(GeneratorBase.ARG_OUTPUTDIR, outDir, "filename", filename, "content", BODY)); + mojo.setGenerators(List.of(p)); + return mojo.buildGenerators(getClass().getClassLoader(), null); + } + + @Test + public void noSelectionSelectsEverything() { + assertNull(mojo.selectedEntities(canonicalModel())); + } + + @Test + public void namesAreTrimmedAndCountedOnce() { + mojo.entities = " Person , Week,Person "; + assertEquals(List.of("Person", "Week"), mojo.selectedEntities(canonicalModel())); + } + + @Test + public void aBlankSelectionIsRefused() { + mojo.entities = " , "; + MetaDataException e = assertThrows(MetaDataException.class, () -> mojo.selectedEntities(null)); + assertEquals("mvn metaobjects:generate: meta.entities names no object.", e.getMessage()); + } + + @Test + public void aNameMatchingNoObjectIsRefused() { + mojo.entities = "Person,Nope,fitness::Week"; + MetaDataException e = assertThrows(MetaDataException.class, () -> mojo.selectedEntities(canonicalModel())); + assertEquals("mvn metaobjects:generate: meta.entities names 'Nope', 'fitness::Week'," + + " which matches no object in the metadata.", e.getMessage()); + } + + @Test + public void theSelectionReachesEveryGenerator() throws IOException { + mojo.entities = "Person"; + List gens = build(tempFolder.newFolder("gen").getAbsolutePath(), "Person.java"); + mojo.executeGenerators(null, gens); + assertEquals(java.util.Set.of("Person"), ((GeneratorBase) gens.get(0)).getEntities()); + } + + /** A narrowed run did not try to produce the other files, so it neither deletes nor forgets them. */ + @Test + public void aNarrowedRunDeletesNoOrphan() throws IOException { + String outDir = tempFolder.newFolder("gen").getAbsolutePath(); + Path week = Path.of(outDir, "Week.java"); + mojo.executeGenerators(null, build(outDir, "Week.java")); + assertTrue(Files.exists(week)); + + mojo.entities = "Person"; + mojo.executeGenerators(null, build(outDir, "Person.java")); + + assertTrue("a narrowed run deletes nothing", Files.exists(week)); + Map manifest = HashManifest.load(projectRoot.resolve(".metaobjects").resolve(".gen-state")); + assertTrue("and forgets nothing", manifest.containsKey("gen/Week.java")); + assertTrue(manifest.containsKey("gen/Person.java")); + + // The same change without a selection is a real orphan, and is swept. + mojo.entities = null; + mojo.executeGenerators(null, build(outDir, "Person.java")); + assertFalse(Files.exists(week)); + } + + @Test + public void theLoaderTimeoutReachesTheLoader() { + assertFalse(mojo.loaderArgs(true).containsKey(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS)); + mojo.setLoaderTimeoutMs(120_000L); + assertEquals("120000", mojo.loaderArgs(true).get(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS)); + } +} diff --git a/server/java/metadata/src/main/java/com/metaobjects/MetaData.java b/server/java/metadata/src/main/java/com/metaobjects/MetaData.java index adb4ac9809..b939535ef1 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/MetaData.java +++ b/server/java/metadata/src/main/java/com/metaobjects/MetaData.java @@ -525,6 +525,11 @@ public Optional findChildByNameAndType(String name, String metaDataTyp return children.findByNameAndType(name, metaDataType); } + /** The types of this node's own children (not inherited), each named once. */ + public Set getChildTypes() { + return children.getSupportedTypes(); + } + /** * Require child by name (throws if not found) */ @@ -1451,7 +1456,7 @@ public List getChildren(Class c, boolean includeParen /** Retrieve the first matching child metadata */ /*private T firstChild( String type, Class c, boolean includeParentData ) { - List keys = new ArrayList<>(); + Set keys = new HashSet<>(); List items = new ArrayList<>(); addChildren( keys, items, type, c, includeParentData, false, true ); return items.iterator().next(); @@ -1460,7 +1465,9 @@ public List getChildren(Class c, boolean includeParen /** Retrieve all matching child metadata */ private List addChildren( String type, Class c, boolean includeParentData ) { - List keys = new ArrayList<>(); + // A hash set, not a list: the dedup check runs once per child, and a list made + // every walk over a large root quadratic (1,160 objects took longer than 30 s). + Set keys = new HashSet<>(); List items = new ArrayList<>(); addChildren( keys, items, type, c, includeParentData, false, false ); return items; @@ -1468,7 +1475,7 @@ private List addChildren( String type, Class c, boole /** Add all the matching children to the map - refactored for better maintainability */ @SuppressWarnings("unchecked") - private void addChildren( List keys, List items, String type, Class c, boolean includeParentData, boolean isParent, boolean firstOnly ) { + private void addChildren( Set keys, List items, String type, Class c, boolean includeParentData, boolean isParent, boolean firstOnly ) { addLocalChildren(keys, items, type, c, isParent, firstOnly); addParentChildren(keys, items, type, c, includeParentData, firstOnly); } @@ -1477,7 +1484,7 @@ private void addChildren( List keys, List items, * Adds matching local children to the results */ @SuppressWarnings("unchecked") - private void addLocalChildren(List keys, List items, String type, Class c, boolean isParent, boolean firstOnly) { + private void addLocalChildren(Set keys, List items, String type, Class c, boolean isParent, boolean firstOnly) { children.stream() .filter(child -> !shouldStopEarly(firstOnly, items)) .filter(child -> matchesSearchCriteria(child, type, c)) @@ -1488,7 +1495,7 @@ private void addLocalChildren(List keys, List it /** * Recursively adds children from parent metadata */ - private void addParentChildren(List keys, List items, String type, Class c, boolean includeParentData, boolean firstOnly) { + private void addParentChildren(Set keys, List items, String type, Class c, boolean includeParentData, boolean firstOnly) { if (getSuperData() != null && includeParentData) { getSuperData().addChildren(keys, items, type, c, true, true, firstOnly); } @@ -1522,7 +1529,7 @@ private boolean matchesSearchCriteria(MetaData child, Strin /** * Determines if a child should be included based on parent filtering and uniqueness */ - private boolean shouldIncludeChild(MetaData child, boolean isParent, List keys) { + private boolean shouldIncludeChild(MetaData child, boolean isParent, Set keys) { if (isParent && filterWhenParentData(child)) { return false; } @@ -1535,16 +1542,15 @@ private boolean shouldIncludeChild(MetaData child, boolean isParent, List void addChildToResults(MetaData child, List keys, List items) { - String key = createChildKey(child); - keys.add(key); + private void addChildToResults(MetaData child, Set keys, List items) { + keys.add(createChildKey(child)); items.add((T) child); } diff --git a/server/java/metadata/src/main/java/com/metaobjects/collections/IndexedMetaDataCollection.java b/server/java/metadata/src/main/java/com/metaobjects/collections/IndexedMetaDataCollection.java index 3d450f4ef1..52dd9961c9 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/collections/IndexedMetaDataCollection.java +++ b/server/java/metadata/src/main/java/com/metaobjects/collections/IndexedMetaDataCollection.java @@ -173,8 +173,10 @@ public MetaData replace(MetaData newChild) { * @return Optional containing the child if found */ public Optional findByNameAndType(String name, String type) { - ConcurrentHashMap typeSpecificIndex = getNamespaceIndexForType(type); - return Optional.ofNullable(typeSpecificIndex.get(name)); + // A read: look the namespace up without creating it, so an empty one never appears + // in getSupportedTypes(). + ConcurrentHashMap typeSpecificIndex = typeNamespaces.get(type); + return typeSpecificIndex == null ? Optional.empty() : Optional.ofNullable(typeSpecificIndex.get(name)); } /** diff --git a/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderConfigurationConstants.java b/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderConfigurationConstants.java index efce3ddc7d..f1d6bea1d8 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderConfigurationConstants.java +++ b/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderConfigurationConstants.java @@ -24,6 +24,8 @@ public final class LoaderConfigurationConstants { public static final String ARG_REGISTER = "register"; public static final String ARG_VERBOSE = "verbose"; public static final String ARG_STRICT = "strict"; + /** {@link LoaderOptions#setInitTimeoutMs}: how long {@code init()} waits, in milliseconds. */ + public static final String ARG_INIT_TIMEOUT_MS = "initTimeoutMs"; private LoaderConfigurationConstants() { // Utility class - no instances diff --git a/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderOptions.java b/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderOptions.java index 74ce2192e3..24ba143f83 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderOptions.java +++ b/server/java/metadata/src/main/java/com/metaobjects/loader/LoaderOptions.java @@ -17,6 +17,17 @@ public class LoaderOptions { private boolean shouldRegister = false; private boolean verbose = true; private boolean strict = true; + private long initTimeoutMs = defaultInitTimeoutMs(); + + /** How long {@code init()} waits for the load when nothing sets it: 30 seconds. */ + public static final long DEFAULT_INIT_TIMEOUT_MS = 30_000L; + + /** + * The JVM system property that changes the default for every loader in the process, for an + * embedder that builds its loaders where no option reaches (a Spring context, a test runner): + * {@code -Dmetaobjects.loader.initTimeoutMs=120000}. + */ + public static final String INIT_TIMEOUT_PROPERTY = "metaobjects.loader.initTimeoutMs"; /** Extra vocabulary providers composed into the loader's registry; empty = the shared default. */ private List providers = List.of(); @@ -61,6 +72,39 @@ public boolean isStrict() { return strict; } + /** + * How long {@link MetaDataLoader#init()} waits for the load to finish, in milliseconds, + * before it fails with a timeout. The default is {@link #DEFAULT_INIT_TIMEOUT_MS} unless the + * {@link #INIT_TIMEOUT_PROPERTY} system property says otherwise. The load time grows with + * the model, and a busy build machine stretches it, so a very large model may need more. + * + * @param initTimeoutMs a positive number of milliseconds + */ + public T setInitTimeoutMs(long initTimeoutMs) { + if (initTimeoutMs <= 0) { + throw new IllegalArgumentException("initTimeoutMs must be a positive number of milliseconds, got " + initTimeoutMs); + } + this.initTimeoutMs = initTimeoutMs; + return (T) this; + } + + public long getInitTimeoutMs() { + return initTimeoutMs; + } + + /** {@link #DEFAULT_INIT_TIMEOUT_MS}, or the {@link #INIT_TIMEOUT_PROPERTY} system property when set. */ + static long defaultInitTimeoutMs() { + String raw = System.getProperty(INIT_TIMEOUT_PROPERTY); + if (raw == null || raw.isBlank()) return DEFAULT_INIT_TIMEOUT_MS; + try { + long value = Long.parseLong(raw.trim()); + if (value > 0) return value; + } catch (NumberFormatException ignored) { + // fall through to the refusal below + } + throw new IllegalArgumentException("-D" + INIT_TIMEOUT_PROPERTY + " must be a positive number of milliseconds, got \"" + raw + "\""); + } + /** * A project's own vocabulary: the loader composes * {@link com.metaobjects.registry.RegistryManifest#composeLoaderRegistry(Collection)} with these @@ -88,6 +132,7 @@ public boolean equals(Object o) { return shouldRegister == that.shouldRegister && verbose == that.verbose && strict == that.strict && + initTimeoutMs == that.initTimeoutMs && providers.equals(that.providers); } @@ -99,7 +144,8 @@ public int hashCode() { protected String getToStringOptions() { return "shouldRegister=" + shouldRegister + ", verbose=" + verbose + - ", strict=" + strict; + ", strict=" + strict + + ", initTimeoutMs=" + initTimeoutMs; } @Override diff --git a/server/java/metadata/src/main/java/com/metaobjects/loader/MetaDataLoader.java b/server/java/metadata/src/main/java/com/metaobjects/loader/MetaDataLoader.java index 302565e20a..c7a37d4035 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/loader/MetaDataLoader.java +++ b/server/java/metadata/src/main/java/com/metaobjects/loader/MetaDataLoader.java @@ -89,7 +89,6 @@ public class MetaDataLoader implements LoaderConfigurable { // Concurrent loading protection private static final ConcurrentHashMap> activeLoaders = new ConcurrentHashMap<>(); - private static final long DEFAULT_LOADING_TIMEOUT_MS = 30000; // 30 seconds public final static String TYPE_LOADER = "loader"; public final static String SUBTYPE_MANUAL = "manual"; @@ -1518,6 +1517,15 @@ protected void processArguments(Map args) { if (args.get(LoaderConfigurationConstants.ARG_STRICT) != null) { getLoaderOptions().setStrict(Boolean.parseBoolean(args.get(LoaderConfigurationConstants.ARG_STRICT))); } + String timeout = args.get(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS); + if (timeout != null) { + try { + getLoaderOptions().setInitTimeoutMs(Long.parseLong(timeout.trim())); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Loader argument '" + LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS + + "' must be a positive number of milliseconds, got \"" + timeout + "\"", e); + } + } } //////////////////////////////////////////////////////////////////////////////////////////// @@ -1539,11 +1547,12 @@ protected void initDefaultRegistries() { /** * Initialize the MetaDataLoader with enhanced thread-safe state management and concurrent protection. + * Waits up to {@link LoaderOptions#getInitTimeoutMs()} for the load. * @return This MetaDataLoader - * @throws MetaDataLoadingException if initialization fails + * @throws MetaDataLoadingException if initialization fails or times out */ public MetaDataLoader init() { - return initWithConcurrencyProtection(DEFAULT_LOADING_TIMEOUT_MS); + return initWithConcurrencyProtection(loaderOptions.getInitTimeoutMs()); } /** @@ -1576,7 +1585,10 @@ private MetaDataLoader initWithConcurrencyProtection(long timeoutMs) { } catch (TimeoutException e) { activeLoaders.remove(loaderKey); throw new MetaDataLoadingException( - "Loader initialization timeout after " + timeoutMs + "ms: " + loaderKey, + "Loader initialization timeout after " + timeoutMs + "ms: " + loaderKey + + ". A large model can need longer: raise LoaderOptions.setInitTimeoutMs, the loader" + + " argument '" + LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS + "' (Maven: -Dmeta.loaderTimeoutMs)," + + " or -D" + LoaderOptions.INIT_TIMEOUT_PROPERTY + ".", getName(), LoadingState.Phase.INITIALIZING, timeoutMs, e); } catch (InterruptedException | ExecutionException e) { activeLoaders.remove(loaderKey); diff --git a/server/java/metadata/src/main/java/com/metaobjects/loader/References.java b/server/java/metadata/src/main/java/com/metaobjects/loader/References.java index 0cc81bbcc7..8c03796b33 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/loader/References.java +++ b/server/java/metadata/src/main/java/com/metaobjects/loader/References.java @@ -90,8 +90,17 @@ public static List ownerCandidateKeys(String owner, String referrerPkg) return pkg.isEmpty() ? List.of(owner) : List.of(pkg + SEP + owner, owner); } - /** Bind an owner among {@code candidates} by exact resolution key. */ - private static Resolution bind(Iterable candidates, String owner, String referrerPkg) { + /** + * Bind an owner among the root's top-level nodes of {@code type} ({@code null} = any type + * but an attribute) by exact resolution key: the first key that names a node wins, and + * within one key the node declared first. + * + *

A typed lookup goes through the root's per-type name index, so resolving every + * reference in a model is linear in its size rather than quadratic. An untyped lookup also + * uses the index, and walks the root in declaration order only when one key names nodes of + * more than one type.

+ */ + private static Resolution bind(MetaData root, String type, String owner, String referrerPkg) { List keys; try { keys = ownerCandidateKeys(owner, referrerPkg); @@ -99,31 +108,40 @@ private static Resolution bind(Iterable candidates, String owner, Stri return new Resolution(null, e.getMessage()); } for (String key : keys) { - for (MetaData c : candidates) { - if (key.equals(c.getName())) return new Resolution(c, null); - } + MetaData found = type != null + ? root.findChildByNameAndType(key, type).orElse(null) + : firstTopLevelNamed(root, key); + if (found != null) return new Resolution(found, null); } return Resolution.NONE; } - /** The root's direct children of {@code type} ({@code null} = any). ADR-0039: the root has no super. */ - private static List topLevelOfType(MetaData root, String type) { - List out = new ArrayList<>(); - for (MetaData c : root.getChildren(MetaData.class, false)) { - if (c instanceof MetaAttribute) continue; - if (type == null || type.equals(c.getType())) out.add(c); + /** The first-declared top-level node named {@code key} of any type but an attribute. */ + private static MetaData firstTopLevelNamed(MetaData root, String key) { + MetaData only = null; + for (String type : root.getChildTypes()) { + if (MetaAttribute.TYPE_ATTR.equals(type)) continue; + MetaData hit = root.findChildByNameAndType(key, type).orElse(null); + if (hit == null) continue; + if (only != null) { + // Two types share the name: declaration order decides, as it always has. + for (MetaData c : root.getChildren(MetaData.class, false)) { + if (!(c instanceof MetaAttribute) && key.equals(c.getName())) return c; + } + } + only = hit; } - return out; + return only; } /** Resolve an OBJECT reference (no child tail) under R2. */ public static Resolution resolveObjectRef(MetaData root, String ref, String referrerPkg) { - return bind(topLevelOfType(root, MetaObject.TYPE_OBJECT), ref, referrerPkg); + return bind(root, MetaObject.TYPE_OBJECT, ref, referrerPkg); } /** Resolve an owner among the root's top-level nodes of {@code type} (any type when null). */ public static Resolution resolveOwner(MetaData root, String owner, String referrerPkg, String type) { - return bind(topLevelOfType(root, type), owner, referrerPkg); + return bind(root, type, owner, referrerPkg); } /** diff --git a/server/java/metadata/src/test/java/com/metaobjects/loader/LargeModelLoadTest.java b/server/java/metadata/src/test/java/com/metaobjects/loader/LargeModelLoadTest.java new file mode 100644 index 0000000000..a5a86a7b01 --- /dev/null +++ b/server/java/metadata/src/test/java/com/metaobjects/loader/LargeModelLoadTest.java @@ -0,0 +1,137 @@ +package com.metaobjects.loader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import com.metaobjects.object.MetaObject; +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadMXBean; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * A large model must load, and load in time that scales with its size. + * + *

The model is the canonical persistence-conformance model copied {@value #COPIES} times, + * each copy in its own package, so every top-level object appears {@value #COPIES} times and + * every reference still resolves inside its copy (the canonical model names no other package). + * That is roughly 1,200 top-level objects, the scale at which the loader used to hit its + * initialization timeout.

+ * + *

The budget is on the loading thread's CPU time, not wall-clock, so a machine busy with + * other work does not fail it: the load runs on the calling thread (no sources are queued for + * {@code init()}). It is deliberately loose: the linear loader needs a few seconds of CPU here, + * and the quadratic one it guards against needed over 30. Override it with + * {@code -Dmetaobjects.test.largeModelBudgetMs=...} when profiling.

+ */ +public class LargeModelLoadTest { + + static final int COPIES = 20; + private static final long BUDGET_MS = Long.getLong("metaobjects.test.largeModelBudgetMs", 15_000L); + + private static Path modelDir; + private static int objectsPerCopy; + + private static Path canonicalModel() { + Path dir = Paths.get("").toAbsolutePath(); + while (dir != null) { + Path candidate = dir.resolve("fixtures/persistence-conformance/canonical/meta.fitness.json"); + if (Files.isRegularFile(candidate)) return candidate; + dir = dir.getParent(); + } + throw new AssertionError("fixtures/persistence-conformance/canonical/meta.fitness.json not found"); + } + + @BeforeClass + public static void writeModel() throws IOException { + String canonical = Files.readString(canonicalModel(), StandardCharsets.UTF_8); + String packageDecl = "\"package\": \"fitness\""; + assertTrue("canonical model declares package fitness", canonical.contains(packageDecl)); + modelDir = Files.createTempDirectory("mo-large-model"); + for (int i = 0; i < COPIES; i++) { + Files.writeString(modelDir.resolve("meta.fitness" + i + ".json"), + canonical.replace(packageDecl, "\"package\": \"fitness" + i + "\""), + StandardCharsets.UTF_8); + } + MetaDataLoader one = MetaDataLoader.fromUris("large-model-baseline", + List.of(canonicalModel().toUri())); + objectsPerCopy = one.getRoot().getChildren(MetaObject.class, false).size(); + } + + @AfterClass + public static void deleteModel() throws IOException { + if (modelDir == null) return; + try (var files = Files.list(modelDir)) { + for (Path p : (Iterable) files::iterator) Files.deleteIfExists(p); + } + Files.deleteIfExists(modelDir); + } + + @Test + public void loadsALargeModelWithinBudget() throws IOException { + List uris = modelUris(); + ThreadMXBean threads = ManagementFactory.getThreadMXBean(); + long cpuStart = threads.getCurrentThreadCpuTime(); + long start = System.nanoTime(); + MetaDataLoader loader = MetaDataLoader.fromUris("large-model", uris); + long wallMs = (System.nanoTime() - start) / 1_000_000; + long cpuMs = (threads.getCurrentThreadCpuTime() - cpuStart) / 1_000_000; + + assertTrue("no load errors: " + loader.getErrors(), loader.getErrors().isEmpty()); + assertEquals(objectsPerCopy * COPIES, loader.getRoot().getChildren(MetaObject.class, false).size()); + String report = objectsPerCopy * COPIES + " objects loaded in " + cpuMs + " ms CPU (" + wallMs + " ms wall)"; + System.out.println("[LargeModelLoadTest] " + report); + assertTrue(report + "; budget " + BUDGET_MS + " ms CPU", cpuMs <= BUDGET_MS); + } + + private List modelUris() throws IOException { + List uris = new ArrayList<>(); + try (var files = Files.list(modelDir)) { + files.sorted().forEach(p -> uris.add(p.toUri())); + } + return uris; + } + + @Test + public void theInitTimeoutDefaultsToThirtySeconds() { + assertEquals(30_000L, new LoaderOptions().getInitTimeoutMs()); + assertEquals(LoaderOptions.DEFAULT_INIT_TIMEOUT_MS, new LoaderOptions().getInitTimeoutMs()); + } + + @Test + public void theInitTimeoutRejectsANonPositiveValue() { + assertThrows(IllegalArgumentException.class, () -> new LoaderOptions().setInitTimeoutMs(0)); + assertThrows(IllegalArgumentException.class, () -> new LoaderOptions().setInitTimeoutMs(-5)); + } + + @Test + public void theLoaderArgumentSetsTheInitTimeout() { + MetaDataLoader loader = MetaDataLoader.createManual(false, "timeout-arg"); + loader.processArguments(Map.of(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS, " 90000 ")); + assertEquals(90_000L, loader.getLoaderOptions().getInitTimeoutMs()); + assertThrows(IllegalArgumentException.class, () -> + loader.processArguments(Map.of(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS, "soon"))); + } + + /** A load that outlasts the setting fails, and the failure names how to raise it. */ + @Test + public void aLoadOutlastingTheTimeoutNamesTheSetting() throws IOException { + MetaDataLoader loader = MetaDataLoader.createManual(false, "timeout-short"); + loader.getLoaderOptions().setInitTimeoutMs(1); + loader.setSourceURIs(modelUris()); + MetaDataLoadingException e = assertThrows(MetaDataLoadingException.class, loader::init); + assertTrue(e.getMessage(), e.getMessage().contains(LoaderConfigurationConstants.ARG_INIT_TIMEOUT_MS)); + assertTrue(e.getMessage(), e.getMessage().contains(LoaderOptions.INIT_TIMEOUT_PROPERTY)); + } +} diff --git a/server/typescript/packages/cli/src/commands/gen.ts b/server/typescript/packages/cli/src/commands/gen.ts index f1a70f8ea1..20044529c1 100644 --- a/server/typescript/packages/cli/src/commands/gen.ts +++ b/server/typescript/packages/cli/src/commands/gen.ts @@ -233,13 +233,20 @@ export async function genCommand(args: string[], cwd: string, fmt: OutputFormat return 2; } + // A positional that matches no loaded object is refused, exit 2, even beside names that + // do match: the selection would silently generate less than was asked for. Every port + // refuses it in the same words (Python and C# `--entities`, Maven `-Dmeta.entities`). + const unknownRefusal = refuseUnknownPositional(cliConfig.entities, metadata); + if (unknownRefusal !== undefined) { + log.error(unknownRefusal); + return 2; + } + // FR-023 §11.1 item 2 — refuse a positional that names ONLY objects imported // from a dependency and excluded from output by the default exclusion rule. - // Distinct from the runner's existing "no entities matched" WARNING - // (runner.ts): that path covers a name matching nothing loaded at all; this + // The refusal above covers a name matching nothing loaded at all; this // one covers a name that matches something real, just not something this - // project generates. A name matching nothing falls through untouched — the - // runner's own warning still covers it. + // project generates. const importedRefusal = refuseImportedPositional(cliConfig.entities, metadata, genCollection); if (importedRefusal !== undefined) { log.error(importedRefusal); @@ -437,7 +444,7 @@ function refuseImportedPositional( const allObjects = metadata.objects(); for (const name of entityNames) { const matches = allObjects.filter((o) => o.name === name); - if (matches.length === 0) continue; // nothing loaded by this name — the runner's own warning covers it + if (matches.length === 0) continue; // nothing loaded by this name — refuseUnknownPositional has already refused it const allExcludedImports = matches.every((o) => { const fqn = o.resolutionKey(); return collection.imported(fqn) && !collection.inScope(fqn); @@ -454,6 +461,19 @@ function refuseImportedPositional( return undefined; } +/** + * The refusal for positionals that match no loaded object by short name, or `undefined` + * when every name matches (or none is given). Names are matched as `refuseImportedPositional` + * matches them: against `entity.name`, never a qualified name. + */ +function refuseUnknownPositional(entityNames: readonly string[], metadata: MetaRoot): string | undefined { + if (entityNames.length === 0) return undefined; + const known = new Set(metadata.objects().map((o) => o.name)); + const unknown = entityNames.filter((n) => !known.has(n)); + if (unknown.length === 0) return undefined; + return `meta gen: names ${unknown.map((n) => `'${n}'`).join(", ")}, which matches no object in the metadata.`; +} + /** * Run the advisory verify-as-teacher scan (same pass `meta verify` runs): surface * authored source that hand-rolls what the metadata could model. `gen` is the diff --git a/server/typescript/packages/cli/test/gen-imported-nodes.test.ts b/server/typescript/packages/cli/test/gen-imported-nodes.test.ts index b6ad6b03fe..b5b1361c1f 100644 --- a/server/typescript/packages/cli/test/gen-imported-nodes.test.ts +++ b/server/typescript/packages/cli/test/gen-imported-nodes.test.ts @@ -187,6 +187,19 @@ describe("meta gen — imported nodes are excluded by default (FR-023)", () => { } }); + test("(c2) a name matching no object is refused with exit 2, even beside a known one", async () => { + const root = setupRepo(); + try { + const exit = await run(["gen", "Order", "Ordr", "--cwd", root]); + expect(exit).toBe(2); + expect(err.join("\n")).toContain("meta gen: names 'Ordr', which matches no object in the metadata."); + // Refused before anything is written. + expect(() => readdirSync(genOutDir(root))).toThrow(); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + test("(d) verify --codegen is green afterward and reports no drift for the absent import", async () => { const root = setupRepo(); try {