dotnet-t4 3.0.0

dotnet tool install --global dotnet-t4 --version 3.0.0                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local dotnet-t4 --version 3.0.0                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=dotnet-t4&version=3.0.0                
nuke :add-package dotnet-t4 --version 3.0.0                

dotnet-t4

dotnet-t4 is a command-line tool for processing T4 templates, a general-purpose way to generate text or code files using C#.

It's part of Mono.TextTemplating, a modern open-source reimplementation of the Visual Studio T4 text templating engine.

Example

A T4 template file contains text interleaved with C# or VB.NET code blocks, which is used to generate a template class, then optionally compiled and executed to generate textual output.

Here is an example T4 template, powers.tt. It generates a Markdown table of squares and cubes for numbers up to the value specified by the parameter Max.

<#@ output extension=".md" #>
<#@ parameter name="Max" type="int" #>
<#@ import namespace="System.Linq" #>
# Table of Powers
Number | Square | Cube
--- | ---
<# foreach(int i in Enumerable.Range(2,Max)) {#>
<#= i #> | <#= i*i #> | <#= i*i*i #>
<#}#>

It can be executed by running t4 powers.tt -p:Max=6, which produces the following powers.md markdown file:

# Table of Powers
Number | Square | Cube
--- | ---
2 | 4 | 8
3 | 9 | 27
4 | 16 | 64
5 | 25 | 125
6 | 36 | 216

Alternatively, invoking t4 powers.tt -c MyApp.Powers will produce a powers.cs file containing the runtime template class, which you can compile into your app and execute at runtime with new parameter values:

var template = new MyApp.Powers {
    Session = new Dictionary<string, object> {
        { "Max", 10 }
    }
};
template.Initialize();
string powersTableMarkdown = template.TransformText();

To learn more about the T4 language, see the Visual Studio T4 documentation.

Usage

t4 is a CLI tool and may be invoked as follows:

t4 [options] [template-file]

The template-file argument is required unless the template text is piped in via stdin.

Option Description
-o, --out=<file> Set the name or path of the output <file>. It defaults to the input filename with its extension changed to .txt, or to match the generated code when preprocessing, and may be overridden by template settings. Use - instead of a filename to write to stdout.
-r=<assembly> Add an <assembly> reference by path or assembly name. It will be resolved from the framework and assembly directories.
-u<br/> --using=<namespace> Import a <namespace> by generating a using statement.
-I=<directory> Add a <directory> to be searched when resolving included files
-P=<directory> Add a <directory> to be searched when resolving assemblies.
-c<br/>--class=<name> Preprocess the template into class <name> for use as a runtime template. The class name may include a namespace.
-l<br/>--useRelativeLinePragmas Use relative paths in line pragmas.
-p, --parameter=<name>=<value> Set session parameter <name> to <value>. The value is accessed from the template's Session dictionary, or from a property declared with a parameter directive: <#@ parameter name='<name>' type='<type>' #>. <br/> If the <name> matches a parameter with a non-string type, the <value> will be converted to that type.
--debug Generate debug symbols and keep temporary files.
-v <br/> --verbose Output additional diagnostic information to stdout.
-h, -?, --help Show help
--dp=<directive>!<class>!<assembly> Set <directive> to be handled by directive processor <class> in <assembly>.
-a=<processor>!<directive>!<name>!<value> Set host parameter <name> to <value>. It may optionally be scoped to a <directive> and/or <processor>. The value is accessed from the host's ResolveParameterValue() method or from a property declared with a parameter directive: <#@ parameter name='<name>' #>.

Differences from VS T4

The Mono.TextTemplating engine contains many improvements over the original Visual Studio T4 implementation, including:

  • It supports the latest .NET APIs and C# language version
  • The engine and the code it generates are compatible with .NET Core and .NET 5+
  • Parameter directives may use primitive types: <#@ parameter name="Foo" type="int" #>
  • Parameter values passed on the CLI will be automatically converted to the type specified in the parameter directive.
  • The CLI can read templates from standard input and output to standard output

Several of these features are demonstrated in the following bash one-liner:

$  echo '<#@ parameter name="Date" type="System.DateTime" #>That was a <#=$"{Date:dddd}"#>' | t4 -o - -p:Date="2016/3/8"
That was a Tuesday
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
3.0.0 39,313 9/3/2024
3.0.0-preview-0052-g5d0f76c785 421 2/28/2024
3.0.0-preview-0049-g4f33c8d8b1 156 1/27/2024
3.0.0-preview-0045-g9b6fc72764 141 1/23/2024
3.0.0-preview-0027-g2711105671 3,395 10/3/2023
2.3.1 749,567 10/28/2022
2.3.0 26,605 10/5/2022
2.2.1 525,498 2/23/2021
2.2.0 16,640 12/5/2020
2.0.5 44,687 6/13/2019
2.0.4 1,048 4/4/2019
2.0.3 794 3/7/2019
2.0.2 3,514 12/14/2018
2.0.1 733 12/14/2018
2.0.0 826 12/13/2018