CodegenCS 1.0.4

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
Suggested Alternatives

CodegenCS.Core

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodegenCS --version 1.0.4
NuGet\Install-Package CodegenCS -Version 1.0.4
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="CodegenCS" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodegenCS --version 1.0.4
#r "nuget: CodegenCS, 1.0.4"
#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.
// Install CodegenCS as a Cake Addin
#addin nuget:?package=CodegenCS&version=1.0.4

// Install CodegenCS as a Cake Tool
#tool nuget:?package=CodegenCS&version=1.0.4

CodegenCS (Core Library)

C# Library for Code Generation

Description

CodegenCS is a class library for code generation:

  • Input can be a database (SQL Server or any other type), a NoSQL database, JSON, YAML, XML, or any kind of structured data that you can read using C#.
  • Output can be C# code, CSHTML, HTML, XML, Javascript, Java, Python, or any other text-based output.

Basically it provides a custom TextWriter tweaked to solve common code generation difficulties:

  • Keeps track of current Indent level.
    When you write new lines it will automatically indent the line according to current level.
  • Helpers to concisely write indented blocks (C-style, Java-style or Python-style) using a Fluent API (IDisposable context will automatically close blocks)
  • Helpers to write multi-line blocks without having to worry about different indentations for control logic and output code (you can align the multi-line blocks anywhere where it fits better inside your control code).
  • Allows writing Interpolated strings (FormattableString) and will process any kind of arguments (can be strings or Action delegates (callbacks)), while "keeping cursor position" of inline arguments.

Besides the TextWriter, there are some helpers for common code generation tasks:

  • Keeps all code in memory until you can save all files at once (no need to save anything if something fails)
  • Adding generated files to old csproj (non-SDK style), with the option to nest the generated files under a single file
  • Adding generated files to new csproj (SDK style) nested under a single file

Besides CodegenCS Core Library, there are some other related projects here, including Scripts to reverse engineer a SQL Server database into JSON schema, and templates to build C# POCOs or EF Entities from that JSON schema.

This class library targets both netstandard2.0 and net472 and therefore it can be used both in .NET Framework or .NET Core.

This project contains C# code and a CSX (C# Script file) which executes the C# code. There's also a PowerShell Script which helps to launch the CSX script.
This is cross-platform code and can be embedded into any project (even a class library, there's no need to build an exe since CSX is just invoked by a scripting runtime).

Actually the scripts are executed using CSI (C# REPL), which is a scripting engine - the CSPROJ just helps us to test/compile, use NuGet packages, etc.

Installation

Just install nuget package CodegenCS, add using CodegenCS, and start using (see below).

Documentation

Creating a TextWriter, writing lines, saving to file

var w = new CodegenTextWriter();
w.WriteLine("Line1");
w.SaveToFile("File1.cs");

Creating a Context to keep track of multiple files, and save all files at once

var ctx = new CodegenContext();

var f1 = ctx["File1.cs"];
var f2 = ctx["File2.cs"];

f1.WriteLine("Line1");
f2.WriteLine("Line1");

ctx.SaveFiles(outputFolder);

Writing C-like block using FluentAPI and WithCBlock()

var w = new CodegenTextWriter();
w
    .WriteLine("// Testing FluentAPI")
    .WithCBlock("void MyMethod()", () =>
    {
        w.WriteLine("OtherMethod();");
    });
    
w.SaveToFile("File1.cs"); 

... will output this:

// Testing FluentAPI
void MyMethod()
{
    OtherMethod();
}

... while WithJavaBlock() would output this:

// Testing FluentAPI
void MyMethod() {
    OtherMethod();
}

Writing Python-like block using FluentAPI and WithPythonBlock()

var w = new CodegenTextWriter();
w
    .WriteLine("# Testing FluentAPI")
    .WithPythonBlock("if a == b", () =>
    {
        w.WriteLine("print b");
    });

... will output this:

# Testing FluentAPI
if a == b :
    print b

Using interpolated strings with variables

string ns = "myNamespace";
string cl = "myClass";
string method = "MyMethod";

w.WithCurlyBraces($"namespace {ns}", () =>
{
  w.WithCurlyBraces($"public class {cl}", () => {
    w.WithCurlyBraces($"public void {method}()", () =>
    {
      w.WriteLine(@"test");
    });
  });
});

... will output this:

namespace myNamespace
{
    public class myClass
    {
        public vod MyMethod()
	{
	    test
	}
    }
}

Writing multi-line blocks without worrying about mixed indentation

w.WithCurlyBraces($"public void MyMethod()", () =>
{
    w
      .WriteLine("// I can add one-line text")
      .WriteLine(@"
        // And I can write multi-line texts
	// which can be indented wherever it fits best (according to the outer control logic)
	// ... and in the end, it will be "realigned to the left" (left padding trimmed, docking the longest line to the margin)
	// so that the extra spaces are all ignored
        ")
      .WriteLine("// No more worrying about mixed-indentations between literals and control logic");
});

... will output this:

public void MyMethod()
{
    // I can add one-line text
    // And I can write multi-line texts
    // which can be indented wherever it fits best (according to the outer control logic)
    // ... and in the end, it will be "realigned to the left" (left padding trimmed, docking the longest line to the margin)
    // so that the extra spaces are all ignored
    // No more worrying about mixed-indentations between literals and control logic
}

Another example of how multi-line blocks are realigned to the left (docking the longest line to the margin):

if (something)
{
    // As you can see below, I can add any number of whitspace before all my lines, and that will be removed
    // The final block will respect the current indentation level of the TextWriter.
    w.WriteLine(@"
        namespace codegencs
        {
            public class Test1
            {
                // etc..
            }
        }");
}

// In other code-generation engines (including T4 templates) you would have to code like this:

if (something)
{
    // Mixed indentation levels can get pretty confusing. 
    // And if the outer indentation level is changed (e.g. if this is put inside an if block) 
    // you would have to add more spaces to each line, since the TextWriter does not have any context information about the current indentation level
    w.WriteLine(@"namespace codegencs
{
    public class Test1
    {
        // etc..
    }
}");
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CodegenCS:

Package Downloads
CodegenCS.DbSchema The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

CodegenCS.DbSchema represents the schema (in json format) of a relational database (MSSQL or PostgreSQL)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 1,678 8/1/2022
1.2.0 504 7/17/2022
1.1.2 954 3/13/2022
1.1.1 2,489 10/30/2021
1.0.7 20,631 7/20/2021
1.0.6 780 7/18/2021
1.0.5 1,081 7/16/2020
1.0.4 715 7/16/2020
1.0.3 629 7/14/2020
1.0.2 620 7/12/2020
1.0.1 683 2/5/2020
1.0.0 700 11/4/2019