Path: redb-route/redb.Route.Xml/Packaging/RouteProjectScaffold.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Xml/Packaging/RouteProjectScaffold.cs
----
using System.Reflection;
namespace redb.Route.Xml.Packaging;
///
/// Scaffolds a route project (an ordinary .csproj — VS Code, Visual
/// Studio and Rider all open it — carrying the XML artifacts, the context document and the
/// layered configuration in OUR convention): the L4 config holds module identity ONLY, secrets
/// arrive through the merged context configuration at deploy time (the identity/context.json
/// conveyor, never the everything-in-the-package legacy style), the generated XSD wires editor
/// autocompletion with no extension of ours, and redb-route-xml pack turns the project
/// into the package layout with the packaging checks as the gate.
///
public static class RouteProjectScaffold
{
/// Creates a new route project in /.
/// Where to create the project directory.
/// Project and package name.
/// The route context name (defaults to the lowercased name).
///
/// A local redb.Route.Xml.csproj path to reference instead of the NuGet package —
/// for working against a checkout before the packages are published.
///
/// The project directory.
public static string Create(string parentDir, string name, string? contextName = null,
string? runtimeProjectPath = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(parentDir);
ArgumentException.ThrowIfNullOrWhiteSpace(name);
contextName ??= name.ToLowerInvariant();
var dir = Path.Combine(parentDir, name);
if (Directory.Exists(dir) && Directory.EnumerateFileSystemEntries(dir).Any())
throw new InvalidOperationException($"'{dir}' already exists and is not empty.");
Directory.CreateDirectory(dir);
Directory.CreateDirectory(Path.Combine(dir, RoutePackage.RoutesDir));
Directory.CreateDirectory(Path.Combine(dir, RoutePackage.ResourcesDir));
Directory.CreateDirectory(Path.Combine(dir, RoutePackage.ConfigDir));
Directory.CreateDirectory(Path.Combine(dir, "schema"));
Directory.CreateDirectory(Path.Combine(dir, ".vscode"));
var runtimeVersion = typeof(XmlRouteLoader).Assembly
.GetCustomAttribute()?.InformationalVersion
?.Split('+')[0] ?? "4.0.0";
var runtimeReference = runtimeProjectPath is null
? $""""""
: $"""""";
File.WriteAllText(Path.Combine(dir, $"{name}.csproj"), $"""
net10.0
enable
enable
{name}
{contextName}
true
{runtimeReference}
""");
File.WriteAllText(Path.Combine(dir, RoutePackage.ContextFile), """
""");
File.WriteAllText(Path.Combine(dir, RoutePackage.RoutesDir, "main.route.xml"), $"""
""");
// L4 — module identity ONLY. Everything else arrives through the merged
// context configuration at deploy time; secrets never ride inside the package.
File.WriteAllText(Path.Combine(dir, RoutePackage.ConfigDir, $"{name}.config.json"), $$"""
{
"//": "L4 (in-package): module identity ONLY. Settings and secrets come from the merged context configuration (see config/context.sample.json).",
"ContextName": "{{contextName}}",
"AutoStart": true
}
""");
File.WriteAllText(Path.Combine(dir, RoutePackage.ConfigDir, "context.sample.json"), $$$"""
{
"//": [
"L3 deploy sample — copy as context.json NEXT TO the package on the host; never commit real secrets here.",
"Merged configuration, later layers win:",
" L1 host appsettings: Contexts default section",
" L2 host appsettings: this context's section",
" L3 this file: Contexts.default + Contexts.{{{contextName}}}",
" L4 in-package: ContextName + AutoStart only (module identity)",
" L5 Override: environment variables (secrets live here)",
"Every {{key}} without a {{key:default}} in the XML must receive a value from",
"one of these layers; `redb-route-xml pack` lists them in manifest.json",
"under RequiredConfigKeys, and the module fails fast at init when one is missing."
],
"Contexts": {
"default": {},
"{{{contextName}}}": {
}
}
}
""");
File.WriteAllText(Path.Combine(dir, "schema", "redb-route-1.0.xsd"),
XmlRouteSchema.Generate(ElementRegistry.CreateDefault()).ToString());
File.WriteAllText(Path.Combine(dir, ".vscode", "settings.json"), """
{
"xml.fileAssociations": [
{ "pattern": "**/*.route.xml", "systemId": "./schema/redb-route-1.0.xsd" }
]
}
""");
File.WriteAllText(Path.Combine(dir, "README.md"), $"""
# {name}
A redb.Route XML route project. The layout is the packaging convention:
| Path | What |
|---|---|
| `routes/*.route.xml` | the route artifacts (explicit manifest order) |
| `context.xml` | components, context beans, the one-time init pipeline |
| `resources/` | XSLT, schemas — resolved from the package root |
| `config/{name}.config.json` | L4: module identity ONLY |
| `config/context.sample.json` | L3 sample for deployment |
| `schema/` | generated XSD — VS Code autocompletion via the RedHat XML extension |
Build the package (checks are the gate — a broken reference is a build error):
redb-route-xml pack . --version 1.0.0
Regenerate C# or a diagram from a route:
redb-route-xml csharp routes/main.route.xml --namespace {name}.Routes
redb-route-xml mermaid routes/main.route.xml
Configuration is layered; secrets never ride inside the package. See
`config/context.sample.json` for the conveyor and `manifest.json` of a built
package for the required keys.
""");
return dir;
}
}