AiACodeGenerator 1.0.6
dotnet tool install --global AiACodeGenerator --version 1.0.6
dotnet new tool-manifest
dotnet tool install --local AiACodeGenerator --version 1.0.6
#tool dotnet:?package=AiACodeGenerator&version=1.0.6
nuke :add-package AiACodeGenerator --version 1.0.6
CodeGen
CodeGen is a powerful, flexible C# and XAML source code generation tool driven by YAML/JSON configuration files. It is designed around Domain-Driven Design (DDD) principles and uses Microsoft Roslyn for safe, AST-based code manipulation and Scriban for rich templating.
Features
- YAML/JSON Driven: Define your features, screens, and data models in simple YAML or JSON files.
- Scriban Templates: Uses the robust Scriban engine for rendering C# and XAML templates. Supports custom filters (
pascal_case,camel_case,snake_case) and{{ include }}directives. - Roslyn AST Manipulation: Safely insert statements into existing methods or add/overwrite properties and methods in existing C# classes without breaking formatting.
- XAML Marker Insertion: Intelligently insert XAML snippets into existing files using comment markers.
- Flexible Path Templating: Output paths support dynamic tokens like
{{feature.name}},{{feature.name.camel}},{{screen.name}}, and any custom data variables. - Shared vs Screen Files: Generate files that belong to the entire feature (e.g., Utils, Models) or files specific to individual screens (e.g., ViewModels, Pages).
Architecture
Built with modern C# 10+ standards, the tool follows Clean Architecture and DDD:
- Domain: Pure logic, Aggregates (
Feature,Screen,FileAction), Value Objects (FeatureName), and domain abstractions. - Application: Vertical Slices containing Use Cases (e.g.,
GenerateFeatureHandler). - Infrastructure: Implementations of config parsing (
YamlDotNet), templating (Scriban), Roslyn editors, and I/O. - Console: A lightweight CLI entry point built with
Microsoft.Extensions.Hosting.
CLI Usage
Run the tool using the .NET CLI:
dotnet run --project src/CodeGen.Console -- --config <path_to_yaml> [options]
Options
| Option | Alias | Description | Required |
|---|---|---|---|
--config |
-c |
Path to the feature YAML/JSON config file. | Yes |
--output |
-o |
Override output root directory (defaults to appsettings.yaml). |
No |
--dry-run |
-d |
Preview actions in the console without writing any files to disk. | No |
--help |
-h |
Show CLI help. | No |
Example Command
dotnet run --project src/CodeGen.Console -- -c ./configs/features/user_management.yaml -o ./output
Configuration Schema
A feature configuration file describes the code generation pipeline. It defines shared files and screen-specific files.
Example: user_management.yaml
feature:
name: "UserManagement"
namespace: "MyApp.Features.UserManagement"
# ─── FEATURE-LEVEL FILES (shared across all screens) ─────────────────────────
files:
- action: create_class
output: "Features/{{feature.name}}/Utils/{{feature.name}}Helper.cs"
template: "csharp/utility_class.sbn"
data:
class_name: "UserManagementHelper"
# ─── SCREEN-LEVEL FILES ───────────────────────────────────────────────────────
screens:
- name: "UserListScreen"
files:
- action: create_class
output: "Features/{{feature.name}}/ViewModels/UserListViewModel.cs"
template: "csharp/viewmodel.sbn"
data:
class_name: "UserListViewModel"
base_class: "ObservableObject"
controls:
- name: "UserName"
type: "string"
- name: "Role"
type: "string"
Action Types
The action field in a file definition dictates how the processor handles the file:
create_class: Renders a.sbntemplate and writes it to theoutputpath. Overwrites if the file exists.create_xaml: Similar tocreate_class, but used for XAML generation.insert_method: Uses Roslyn to add a new method/property to an existing C# class defined intarget. Overwrites if a member with the same name exists. Requiresdata.insert_in_classorposition.in_class.insert_code: Uses Roslyn to safely insert a statement inside the body of an existing method. Requiresposition(InClass, InMethod).insert_xaml: Inserts a rendered XAML snippet into an existing file using text-based comment markers. Requiresposition.
Positioning Types
When modifying existing files (insert_* actions), you can control exactly where the code goes using the position object:
| Position Type | Description | Required Field |
|---|---|---|
after_marker |
Inserts immediately after a specific comment marker. | marker: "text" |
before_marker |
Inserts immediately before a specific comment marker. | marker: "text" |
before_line |
Inserts before the first line containing the specified text. | contains: "text" |
after_line |
Inserts after the first line containing the specified text. | contains: "text" |
inside_class |
Target a specific class name (for Roslyn insertion). | in_class: "ClassName" |
inside_method |
Target a specific method name (for Roslyn insertion). | in_method: "MethodName" |
after_last_member |
Inserts after the last member in a class (no marker needed). | in_class: "ClassName" |
before_first_member |
Inserts before the first member in a class (no marker needed). | in_class: "ClassName" |
inside_namespace |
Adds a type declaration into a namespace in an existing file. | in_namespace: "My.Namespace" (optional — defaults to first found) |
inside_enum |
Appends a new value to an existing enum. Roslyn handles commas. | in_enum: "EnumName" |
inside_interface |
Adds a method signature to an existing interface. | in_interface: "IMyInterface" |
inside_xaml_element |
Inserts a XAML snippet before the closing tag of a named element. | element_name: "TagName", optionally element_x_name: "xName" |
Example: Roslyn Statement Insertion
- action: insert_code
target: "Program.cs"
template: "csharp/di_registration.sbn"
position:
type: "inside_method"
in_class: "Program"
in_method: "ConfigureServices"
type: "after_marker"
marker: "// [CODEGEN:Services]"
Templating (Scriban)
Templates are stored in the folder configured via appsettings.yaml (default: ./templates).
Built-in Custom Filters
{{ my_var | pascal_case }}:user_name→UserName{{ my_var | camel_case }}:UserName→userName{{ my_var | snake_case }}:UserName→user_name
Includes
You can break templates into partials and include them recursively (useful for XAML layout builders):
{{- include "xaml/" + (child.type | string.downcase) + ".sbn" child -}}
Global Configuration (appsettings.yaml)
codegen:
templatesDir: "./templates"
outputRoot: "./output"
dryRun: false
(You can also use environment variables prefixed with CODEGEN_, e.g., CODEGEN_TemplatesDir)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
v1.0.6 – Added before_marker and after_marker support for class-level insertions. Preserved existing comments during member overwrite.