Skip to content

dantorquato/codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ CodeGen

.NET License Tests

Generate code automatically from templates. Simple, fast, and works on any project!

✨ Why Use CodeGen?

  • ⚑ Super Fast - Small (~2MB) and instant startup
  • πŸ”’ Safe - Never overwrites your existing files
  • 🌍 Works Everywhere - Windows, Linux, macOS (no installation needed)
  • πŸ“ Any Language - Works with C#, TypeScript, Python, Go, anything!
  • 🎨 Smart Variables - Auto-converts names (UserProfile β†’ userProfile β†’ user-profile)

πŸ“¦ Installation

Download for Your Platform

Go to Releases and download the latest version:

  • Windows: codegen-win-x64.zip or codegen-win-arm64.zip
  • Linux: codegen-linux-x64.tar.gz or codegen-linux-arm64.tar.gz
  • macOS: codegen-osx-x64.tar.gz or codegen-osx-arm64.tar.gz

πŸ’‘ Can't find downloads? Check all releases here

Choose: Local or Global?

Option 1: Install Globally (Recommended)

Use codegen from anywhere on your system.

macOS/Linux:

tar -xzf codegen-*.tar.gz
sudo mv codegen /usr/local/bin/codegen

# Now use from any directory
cd ~/my-project
codegen User

Windows:

# Extract zip, then move to a permanent location like:
move codegen.exe C:\bin\codegen.exe

# Add C:\bin to your PATH environment variable
# Then use from any directory:
cd C:\my-project
codegen User

Option 2: Use Locally in Your Project

Keep CodeGen in your project directory.

macOS/Linux:

tar -xzf codegen-*.tar.gz
mv codegen my-project/codegen

# Use with relative path
cd my-project
./codegen User

Windows:

# Extract and move to project
move codegen.exe my-project\codegen.exe

# Use with relative path
cd my-project
codegen.exe User

πŸš€ Quick Start

1. Create a Template

In your project, create templates/model.template.txt:

// META: output=Models/{{EntityName}}.cs

public class {{EntityName}}
{
    public int Id { get; set; }
    public string Name { get; set; }
}

2. Generate Code

# If installed globally:
codegen User

# If using locally:
./codegen User        # macOS/Linux
codegen.exe User      # Windows

3. Done! ✨

βœ… File generated: Models/User.cs

That's it! CodeGen created Models/User.cs with your template.

πŸ“ Creating Templates

Templates are text files with special comments that tell CodeGen where to save the generated file:

// META: output=path/to/{{EntityName}}.cs
// META: tags=entity, model

// Your code here with {{EntityName}} variable
public class {{EntityName}} { }

Template Metadata

Add metadata at the top of your template files:

// META: output=Models/{{EntityName}}.cs
// META: description=Domain entity class
// META: tags=entity, model, domain

Available Metadata:

  • output (required) - Path where the generated file will be saved
  • description (optional) - Description of what the template generates
  • tags (optional) - Comma-separated tags for filtering templates

Available Variables

When you run codegen UserProfile, these variables are replaced:

Variable Result Example
{{EntityName}} UserProfile Class name
{{entityName}} userProfile Variable name
{{ENTITY_NAME}} USERPROFILE Constant
{{entity-name}} user-profile File/URL slug

πŸ’‘ Examples

Generate Multiple Files

# Global installation
codegen User
codegen Product
codegen Order

# Local installation
./codegen User        # macOS/Linux
codegen.exe User      # Windows

Filter by Tags

Generate only templates with specific tags:

# Generate only model (with "model" tag)
codegen --entity User --tags model

# Short form
codegen -e User -g model

# Multiple tags (generates templates with ANY of these tags)
codegen -e Product -g entity,service,controller

# Legacy format also works
codegen User templates model

How Tags Work:

  • If you don't specify tags, ALL templates are generated
  • If you specify tags, only templates with at least ONE matching tag are generated
  • Templates can have multiple tags: // META: tags=entity, model, domain

Example:

// Template 1: model.template.cs
// META: tags=entity, model

// Template 2: service.template.cs  
// META: tags=entity, service

// Template 3: controller.template.cs
// META: tags=entity, controller, api
codegen -e User -g model         # Generates only template 1
codegen -e User -g service       # Generates only template 2
codegen -e User -g entity        # Generates all 3 (all have "entity")
codegen -e User                  # Generates all 3 (no filter)

Use Custom Template Folder

# Named arguments
codegen --entity User --templates my-templates
codegen -e User -t my-templates

# Legacy format
codegen User my-templates

Generate Multiple Entities at Once

# macOS/Linux
for entity in User Product Order; do
  codegen $entity
done

# Windows (PowerShell)
foreach ($entity in "User","Product","Order") {
  codegen $entity
}

Different Template Sets

project/
β”œβ”€β”€ templates-api/     # REST API templates
β”œβ”€β”€ templates-domain/  # Domain models
└── templates-tests/   # Unit tests
codegen User templates-api
codegen Product templates-domain
codegen Order templates-tests

πŸ”§ CLI Reference

Command Syntax

# Named arguments (recommended)
codegen --entity <name> [--templates <path>] [--tags <tags>]
codegen -e <name> [-t <path>] [-g <tags>]

# Legacy format (still supported)
codegen <EntityName> [TemplatesFolder] [Tags]

Arguments

Argument Short Description Default
--entity -e Entity name (required) -
--templates -t Templates folder path templates
--tags -g Filter by tags (comma-separated) All templates
--help -h Show help message -

Examples

# Basic usage
codegen --entity Product
codegen -e User

# Custom templates folder
codegen --entity Order --templates my-templates
codegen -e Customer -t ./api-templates

# Filter by tags
codegen --entity Invoice --tags entity
codegen -e Product -g model,service

# Combine all options
codegen -e Order -t templates -g entity,controller,service

# Legacy format (still works)
codegen Product
codegen User templates
codegen Order templates entity,service

❓ FAQ

Global vs Local - Which should I use?

Global (Recommended for personal use):

  • βœ… Use codegen from anywhere
  • βœ… Simpler commands
  • ❌ Team members need to install it

Local (Recommended for teams):

  • βœ… Commit to version control
  • βœ… Everyone has the same version
  • βœ… Works in CI/CD pipelines
  • ❌ Need to use ./codegen or full path

Does it overwrite my files?

No! CodeGen never overwrites existing files. It's safe to run multiple times.

What languages does it support?

Any! CodeGen works with templates, so you can generate C#, TypeScript, Python, Go, or any other language.

Do I need .NET installed?

No! The standalone executable includes everything needed.

Can I use custom template folders?

Yes! Just run codegen EntityName my-folder

πŸ› οΈ For Developers

Requirements

  • .NET 9.0 SDK (for building from source)

Build

./build-standalone.sh    # macOS/Linux
build-standalone.bat     # Windows

Test

dotnet test

πŸ’¬ Support

πŸ“ License

MIT License - feel free to use in your projects!


Made with ❀️ using .NET 9.0

About

A powerful, cross-platform code generator built with .NET 9.0. Generate boilerplate code from templates with metadata - no .NET runtime required on target machines!

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors