CodeTemplateForge 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeTemplateForge --version 1.0.1
                    
NuGet\Install-Package CodeTemplateForge -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeTemplateForge" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeTemplateForge" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CodeTemplateForge" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeTemplateForge --version 1.0.1
                    
#r "nuget: CodeTemplateForge, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeTemplateForge@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeTemplateForge&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CodeTemplateForge&version=1.0.1
                    
Install as a Cake Tool

TemplateForge

A lightweight and extensible template-based code generation framework for .NET.

Features

  • Multiple Template Engines - Built-in Scriban support, extensible for others
  • Flexible Package Formats - Binary packages, directory-based, or custom formats
  • Customizable Magic Numbers - Define your own package format identifier
  • Value Processors - Built-in hex, bool-to-int, formatting, and more
  • Output Processors - Auto-formatting, line ending normalization
  • Event System - Progress tracking, logging, and cancellation support
  • Fluent API - Chain configuration calls for clean code

Quick Start

Basic Usage

using TemplateForge;
using TemplateForge.Core;

// Create generator
var generator = new CodeGenerator();

// Load templates
await generator.LoadPackageAsync("templates.bin");

// Create context with variables
var context = new TemplateContext()
    .Set("project_name", "MyProject")
    .Set("version", "1.0.0")
    .Set("author", "John Doe");

// Generate code
var result = await generator.GenerateAsync(context, outputPath);

Custom Magic Number

// Using hex value
var generator = new CodeGenerator(0x4D595047); // "MYPG"

// Using string
var generator = new CodeGenerator("MYPG");

// Using helper
using TemplateForge.Utilities;
var magic = MagicNumberHelper.FromString("CHIP");
var generator = new CodeGenerator(magic);

Creating Template Packages

using TemplateForge.Packaging;

// Build package programmatically
var builder = new TemplatePackageBuilder()
    .WithId("my-templates")
    .WithName("My Template Package")
    .WithVersion("1.0.0")
    .WithMagicNumber("MYPG")  // Custom magic number
    .AddTemplatesFromDirectory("./templates");

await builder.SaveAsync("my-templates.bin");

// Or quick pack
await PackageBuilderExtensions.PackDirectoryAsync(
    "./templates", 
    "output.bin",
    "*.template",
    MagicNumberHelper.FromString("MYPG")
);

Using Extension Methods

using TemplateForge.Extensions;

var context = new TemplateContext()
    .SetHex("config_reg", 0x1A, 2)      // "1a"
    .SetBool("feature_enabled", true)    // Sets 3 vars: feature_enabled, feature_enabled_int, is_feature_enabled
    .SetDouble("timeout", 2.5)           // Smart formatting
    .SetEnum("mode", MyEnum.Option1);    // Sets mode, mode_value, is_mode_option1, etc.

Event Handling

var generator = new CodeGenerator();

generator.ProgressChanged += (s, e) => 
    Console.WriteLine($"[{e.ProgressPercentage:F0}%] {e.CurrentTemplate}");

generator.Log += (s, e) => 
    Console.WriteLine($"[{e.Level}] {e.Message}");

generator.BeforeRendering += (s, e) => 
{
    if (e.TemplateName.Contains("debug"))
        e.Cancel = true; // Skip debug templates
};

generator.Completed += (s, e) => 
    Console.WriteLine($"Done! {e.FilesGenerated} files in {e.Duration.TotalSeconds:F2}s");

Configuration Options

var generator = new CodeGenerator()
    .WithDefaults()
    .WithDirectories("obj", "build")
    .Configure(options => 
    {
        options.OverwriteExisting = true;
        options.ContinueOnError = false;
        options.ValidateTemplates = true;
        options.OutputEncoding = Encoding.UTF8;
    });

Template Syntax (Scriban)

{{ variable_name }}
{{ if condition }}...{{ end }}
{{ for item in collection }}...{{ end }}
{{ variable | string.upcase }}

Project Structure

TemplateForge/
├── Core/                    # Template engine interfaces
├── Packaging/               # Package formats and loaders
├── Processing/              # Value and output processors
├── Configuration/           # Generator options
├── Extensions/              # Extension methods
├── Utilities/               # Helper classes
└── CodeGenerator.cs         # Main entry point

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 414 12/11/2025
1.0.1 427 12/10/2025
1.0.0 429 12/10/2025

v1.0.1:
- Initial release
- Scriban template engine support
- Binary and directory package formats
- Custom magic number support
- AES-256 and XOR encryption support
- Value processors (hex, bool, formatting)
- Output processors (trim, normalize)
- Event system (progress, log, completion)
- Fluent API configuration