Skip to main content

NTypewriter by NeVeSpl

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: NTypewriter

Package Description

Author: NeVeSpl

NuGet: https://www.nuget.org/packages/NTypewriter.SourceGenerator

You can find more details at https://github.com/NeVeSpl/NTypewriter

Source : https://github.com/NeVeSpl/NTypewriter

Original Readme

note

NTypewriter

ci Visual Studio Marketplace Version Nuget Nuget

NTypewriter LivePreview

Scriban templates + Roslyn C# code model => generated files

design/compile/run time == any time

For those who do not know Typewriter:

NTypewriter is files generator from text templates populated with meta-data about your C# code. It is like a specialized and more convenient T4 design-time template.

With NTypewriter you can:

  • auto-generate documentation for your C# code
  • create a typed TypeScript API client for your ASP.net web API

NTypewriter comes in many flavours, that can be used according to your needs:

  • NTypewriter editor for Visual Studio - extension for Visual Studio that adds support for editing *.nt templates, with syntax highlighting, code completion, live preview, design time rendering, available on vs marketplace
  • NTypewriter.SourceGenerator - nuget, Roslyn source generator that renders *.nt templates during compilation, since it is a compiler extension, it can be used with any IDE or CI/pipeline that supports source generators
  • NTypewriter.Online - blazor client side, online demo of NTypewriter capabilities
  • NTypewriter - nuget, library that enables you to create run time solution which will be able to render *.nt templates, for example: your own CLI
  • NTypewriter.CodeModel.Roslyn - nuget, library that exposes C# code model from an instance of Microsoft.CodeAnalysis.Compilation, useful if you would like to use a different template engine

more about NTypewriter architecture and all extension points that can be used, you will find here

For those who know Typewriter:

NTypewriter is a younger and more immature brother of beloved Typewriter. They share the same ideas but with a completely different implementation. NTypwriter uses Scriban as a template engine, thus template files are completely not interchangeable. While code model API is about 95% compatible between them, there are some differences. NTypewriter code model is 100% pure, without any amenities that help generate TS files. All things that help generate TypeScript from ASP.NET are located in built-in functions: Action, Type.

Oh, did I forget to mention that NTypewriter also solves most of the awaited issues of the Typewriter that were promised for 2.0 version:

  • support for attribute properties/values, statics, indexers, default parameters, nullable, records, constructors
  • output multiple types to a single file
  • include types in CodeModel from referenced assemblies/nugets
  • save generated file only when file content has changed
  • sharable custom functions between templates
  • full control over whitespaces
  • compile-time rendering, without any IDE needed
  • built-in support for getting all types used in type declaration (Type.AllReferencedTypes)
  • you can debug custom functions

Index

Typewriter vs NTypewriter

 TypewriterNTypewriter
Template file extension*.tst*.nt
Syntaxtypewriter syntaxscriban scripting language
Lambda filterspresentyes
Can be used from CLInoyes
Can be used in pipelinenoyes
Full control over whitespacesnopeyup
Mappingone input always produces one output fileyou can generate as many files as you want
Live previewnoyes
Code model
Unit of workfilethere is no concept of a file in NTypewriter, you work on compiled symbols
Access modifierscode model contains only public typescode model contains all types
Partial classestreated as separate unitsall parts of the class are treated as a whole unit
Automation
Auto-render template on saveyes (opt-out is possible)yes (opt-in is possible)
Auto-render when C# file changesyes (opt-out is possible)no
Auto-render on buildnoyes (opt-in is possible)
Custom functions
Placementinside template file (.tst)in separate file (*.nt.cs)
Can be sharedseparate for every templateshared between templates inside a project
Can be debugnoyes
Can be unit testednoyes
VS Integration
Supported versions of Visual Studio2015, 2017, 20192019 (min ver 16.11.x), 2022
Add generated files to VS projectyes (opt-out is possible)yes (opt-out is possible)
Sync deleted or renamed C# types with generated filesthere is a part of the code that should do that but it does not work anymoreyes (only when the above option is enabled)

Typewriter template:

module App { $Classes(*Model)[
export class $Name { $Properties[
public $name: $Type;]
}]
}

equivalent NTypewriter template will be: (open in NTypewriter.Online)

{{- for class in data.Classes | Symbols.WhereNameEndsWith "Model"
capture output -}}
module App {
export class {{ class.Name }} {
{{- for property in class.Properties | Symbols.ThatArePublic }}
public {{ property.Name | String.ToCamelCase }}: {{ property.Type | Type.ToTypeScriptType }};
{{- end }}
}
}
{{- end
filePath = class.BareName | String.Append ".ts"
Save output filePath
end }}

yes, it is more verbose, but maintaining it over time will be much easier. Both templates generate exactly the same output:

module App {
export class CustomerModel {
public id: number;
public name: string;
public orders: OrderModel[];
}
}

Examples

All Typewriter examples are available as .nt templates on github and also on NTypewriter.Online website.

Note nt. templates produce exactly the same output as .tst templates, even bad output formatting was preserved, to make them easier to compare.

exampleNTypewriterTypewriterOnline
CreateYourFirstTemplateCreateYourFirstTemplate.ntCreateYourFirstTemplate.tstopen
ExtensionsExtensions.ntExtensions.tstopen
ModelInterfacesModelInterfaces.ntModelInterfaces.tstopen
KnockoutModelsKnockoutModels.ntKnockoutModels.tstopen
AngularWebAPIServiceAngularWebAPIService.ntAngularWebAPIService.tstopen

Known issues

NTypewriter does not have own a lexer/parser as Typewriter has, and uses Scriban instead to do heavy work. Scriban works very well with fully correct templates, but with incomplete templates during editing not so much. It is the source of the most glitches in the Editor. Scriban language is also typeless, thus doing code completion is challenging.

About

note

Generating code with a template from classes in project

How to use

Example ( source csproj, source files )

This is the CSharp Project that references NTypewriter

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="test.nt" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="test.nt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NTypewriter.SourceGenerator" Version="0.5.9" />
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// NTypewriter.SourceGenerator                      0.5.9.0  C:\Users\ignat\AppData\Local\Temp\VBCSCompiler\AnalyzerAssemblyLoader\3c1ac59792da4d3297a073f33c6e2daf\4a40c9db-c376-41d2-8a7c-26eda7296396\NTypewriter.SourceGenerator.dll NTypewriter.SourceGenerator, Version=0.5.9.0, Culture=neutral, PublicKeyToken=null
// NTypewriter 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.v0.5.9.0.dll NTypewriter, Version=0.5.9.0, Culture=neutral, PublicKeyToken=686471615d7a8f08
// NTypewriter.CodeModel 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.CodeModel.v0.5.9.0.dll NTypewriter.CodeModel, Version=0.5.9.0, Culture=neutral, PublicKeyToken=9bd097c4961606db
// NTypewriter.CodeModel.Functions 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.CodeModel.Functions.v0.5.9.0.dll NTypewriter.CodeModel.Functions, Version=0.5.9.0, Culture=neutral, PublicKeyToken=64a6b4cdbb438ab5
// NTypewriter.CodeModel.Roslyn 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.CodeModel.Roslyn.v0.5.9.0.dll NTypewriter.CodeModel.Roslyn, Version=0.5.9.0, Culture=neutral, PublicKeyToken=81d8e46ee60c9c4c
// NTypewriter.Editor.Config 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.Editor.Config.v0.5.9.0.dll NTypewriter.Editor.Config, Version=0.5.9.0, Culture=neutral, PublicKeyToken=b9f8710003231974
// NTypewriter.Runtime 0.5.9.0 C:\Users\ignat\AppData\Local\Temp\NTSG\NTypewriter.Runtime.v0.5.9.0.dll NTypewriter.Runtime, Version=0.5.9.0, Culture=neutral, PublicKeyToken=4fac57df20922078
// Scriban.Signed 5.0.0.0 C:\Users\ignat\AppData\Local\Temp\NTSG\Scriban.Signed.v5.10.0.0.dll Scriban.Signed, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5675fb69b15f2433
// System.Text.Json 9.0.0.0 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.2\System.Text.Json.dll System.Text.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// System.Text.RegularExpressions 9.0.0.0 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.2\System.Text.RegularExpressions.dll System.Text.RegularExpressions, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// Microsoft.CodeAnalysis 4.13.0.0 C:\Program Files\dotnet\sdk\9.0.200\Roslyn\bincore\Microsoft.CodeAnalysis.dll Microsoft.CodeAnalysis, Version=4.13.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.CodeAnalysis.CSharp 4.13.0.0 C:\Program Files\dotnet\sdk\9.0.200\Roslyn\bincore\Microsoft.CodeAnalysis.CSharp.dll Microsoft.CodeAnalysis.CSharp, Version=4.13.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.CodeAnalysis.Scripting 4.0.0.0 C:\Users\ignat\AppData\Local\Temp\NTSG\Microsoft.CodeAnalysis.Scripting.v4.0.1.0.dll Microsoft.CodeAnalysis.Scripting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.CodeAnalysis.CSharp.Scripting 4.0.0.0 C:\Users\ignat\AppData\Local\Temp\NTSG\Microsoft.CodeAnalysis.CSharp.Scripting.v4.0.1.0.dll Microsoft.CodeAnalysis.CSharp.Scripting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.CodeAnalysis.Workspaces 4.0.0.0 C:\Users\ignat\AppData\Local\Temp\NTSG\Microsoft.CodeAnalysis.Workspaces.v4.0.1.0.dll Microsoft.CodeAnalysis.Workspaces, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// CurrentDomain: VBCSCompiler

Usefull

Download Example (.NET C# )

Share NTypewriter

https://ignatandrei.github.io/RSCG_Examples/v2/docs/NTypewriter

In the same category (Templating) - 11 other generators

Gobie

InterceptorTemplate

JKToolKit.TemplatePropertyGenerator

Microsoft.NET.Sdk.Razor.SourceGenerators

Minerals.AutoMixins

MorrisMoxy

RazorBlade

RazorSlices

RSCG_IFormattable

RSCG_Templating

spreadcheetah