As simple as it gets code generator, which is a console application that runs code generation tasks written in C# using interpolated strings.
$ dotnet add package iQuarc.Geco.CSharpSimple code generator based on a console project, running on .Net Core and using C# interpolated strings.
Geco runs on .Net Core 9. Download and Install
What's the reasoning behind this utility?
One of the most popular code generation tools for Visual Studio over the years was T4 (Text Template Transformation Toolkit), which saw a great deal of success for simple to complex code generation tasks. Scott Hanselman has a nice blog post about it. However, since the advent of .Net Core and .Net going cross platform there is no simple way to generate code or other artifacts that would also work in Visual Studio Code (or other code editors).
This is where the idea for this simple utility came from. Have a small utility for code generation that is debuggable (one of the long standing issues of T4), provides some level of intellisense, can be run at build time, is higly customizable, generic and has an as small as possible footprint.
Geco can be installed as a Visual Studio Project Template or as dotnet new template.
To install Geco as a dotnet new template run the following command on a console or terminal:
dotnet new -i iQuarc.Geco.CSharp
Then create a new project using the newly installed template:
dotnet new geco
To create a Geco project when installed as a Visual Studio Project template, Select File -> New project select the Geco template.
Geco can also be installed as a Git submodule. Geco.Core is the git repository which should be added. The recomendation is to add it to a folder called .Tools
git submodule add https://github.com/iQuarc/Geco.Core.git .Tools
Bellow are some snippets from the included code generation tasks that are in the default Geco package.
Next snippet is from the included Entity Framework Core Reverse model generator EntityFrameworkCoreReverseModelGenerator.cs, which exemplifies part of the code generation:
This snippet is from the SQL Seed Scrip Generator SeedDataGenerator.cs, which generates SQL Merge scripts for Seed data:
Next screen shot shows Geco running in interactive mode (as a Console App), and the menu that is presented to the user:
Geco uses C# 13.0 string interpolation as a template engine for code generation, in order to allow:
IRunnable)Geco uses task discovery at runtime and each task is configured using Dependency Injection. The generation tasks can be run during build or from interactive mode (Debug Run).
The following generator tasks are included in current version.
appsettings.json configuration file. Each Geco task defines its own set of parameters (see the corresponding Options class):{
"ConnectionStrings": {
"DefaultConnection": "Integrated Security=True;Initial Catalog=AdventureWorks;Data Source=.\\SQLEXPRESS;"
},
"RunAtBuildTasks": [
"Generate EF Core model"
],
"Tasks": [
{
"Name": "Generate EF Core model",
"TaskClass": "Geco.Database.EntityFrameworkCoreReverseModelGenerator",
"BaseOutputPath": "..\\..\\Generated\\Model\\",
"OutputToConsole": "false",
"CleanFilesPattern": "*.cs",
"Options": {
"ConnectionName": "DefaultConnection",
"Namespace": "Model",
"OneFilePerEntity": true,
"JsonSerialization": true,
"GenerateComments": true,
"UseSqlServer": true,
"ConfigureWarnings": true,
"GeneratedCodeAttribute": false,
"NetCore": true,
"ContextName":"AdventureworksContext"
}
}
]
}
By customizing existing templates directly.
By creating new code generation tasks as a C# class which implements the IRunnable interface.
IRunnable is a single method interface containing the Run method:
/// <summary>
/// Represents a runable task, for code generation or purposes or others
/// </summary>
public interface IRunnable
{
/// <summary>
/// Invoked when this task is executed
/// </summary>
void Run();
}
or by deriving from one of the helper base classes BaseGenerator or BaseGeneratorWithMetadata.
A code generation class can be accompanied by an Options POCO class to hold customization parameters from appsettings.json
Example:
public class SeedDataGeneratorOptions
{
public string ConnectionName { get; set; }
public string OutputFileName { get; set; }
public List<string> Tables { get; } = new List<string>();
public string TablesRegex { get; set; }
public List<string> ExcludedTables { get; } = new List<string>();
public string ExcludedTablesRegex { get; set; }
public int ItemsPerStatement { get; set; } = 1000;
}
/// <summary>
/// Generates seed scripts with merge statements for (Sql Server)
/// </summary>
[Options(typeof(SeedDataGeneratorOptions))]
public class SeedDataGenerator : BaseGeneratorWithMetadata
{
// ...
}
Version 1.5.0
Version 1.0.9
Version 1.0.8
Version 1.0.7
Version 1.0.6
Version 1.0.5
Version 1.0.4
EnglishInflector with Humanizer based versionSeedScriptRunner error message when the seed file does not existVersion 1.0.3
Version 1.0.2
Version 1.0.1
Version 1.0.0.2
Version 1.0.0.1
nuget dotnet new templateVersion 1.0.0.0-beta