Use TextTransform to automatically generate code with T4 without launching Visual Studio

Page update date :
Page creation date :

Operating environment

Visual Studio
  • Visual Studio 2022
Windows
  • Windows 11

Prerequisites

Visual Studio
  • It works even with a somewhat older version

precondition

This tip assumes the following:

  • Visual Studio installed on Windows

In this case, we will use a tool called "TextTransform (or TextTransformCore)", but since it is a tool included in Visual Studio, it is necessary to install it even if you do not start Visual Studio.

What is the difference between "TextTransform.exe" and "TextTransformCore.exe"?

"TextTransform.exe" is an early tool that has been around since the advent of the Entity Framework.

TextTransformCore.exe is a new tool built on top of .NET 6.

There is no major functional difference, so it doesn't matter which one you use.

Create a T4 file (.tt)

It's no different from what you create in Visual Studio, so use a text editor to create it. This time, we will use the code that we previously created as a Mr./Ms..

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

<#
  List<string> types = new(){"Int", "Short", "Long", "Float", "Double", "Decimal"}; 
#>

public static class ParseExtensions
{
<# foreach (var type in types) { #>
<#   var typeLower = type.ToLower(); #>
  public static <#= typeLower #> Parse<#= type #>(this string self, <#= typeLower #> defaultValue)
  {
    return <#= typeLower #>.TryParse(self, out var val) ? val : defaultValue;
  }
<# } #>
}

In this case, we will place this file in the following location. You can run it anywhere, so leave it wherever you want.

  • C:\Temporary\Sample.tt

Generate code using the command line tool "TextTransform"

In Visual Studio, it was easy because Visual Studio automatically generated code using "TextTransform", but if you don't use Visual Studio, you have to manually generate code using "TextTransform".

TextTransform is a command-line tool that is installed when you install Visual Studio in the following folder:

  • < drivePath>\Program Files\Microsoft Visual Studio\<Visual Studio Version>\<Editions>\Common7\IDE\TextTransform.exe
  • < drivepath>\Program Files\Microsoft Visual Studio\<Visual Studio Version>\<Editions>\Common7\IDE\TextTransformCore.exe

[Example]

  • C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\TextTransform.exe

Launch a terminal or command prompt and use TextTransform to generate code.

For Windows 11, launch Terminal from the Start menu. For other operating systems, use the command prompt.

For terminal, switch to the command prompt. There's nothing you can't do with PowerShell, but the description changes slightly.

cd In the command, navigate to the folder where you placed the .tt file and enter the command as follows: (Please change the version of the folder path as appropriate.)

[Input example]

  • "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\TextTransform" Sample.tt

If successful, the process proceeds as if it were done without incident. If you actually look at the folder, you can see that the code is auto-generated.

The contents are also properly created.

I want to shorten the path of TextTransform when executing a command.

If you don't want to write a long path to your TextTransform every time you run a command, you can register that path in an "environment variable". If it is a development command prompt that comes with Visual Studio, it will reference that path by default, making it easier to write.

If you look at the Visual Studio folder from the Start menu, you'll find a development command prompt and launch it.

In the same way, when writing a command, you can omit the path of the TextTransform and execute it.

By the way, in the case of Windows 11, it is easy to open it because it is built into the terminal.

Put commands in a .bat or .ps1 file to make them easy to run

The content to be described is the same as when entering a normal command, so the content is omitted. Depending on the OS settings, it may be restricted so that it cannot be executed even if you double-click the .bat or .ps1 file, so in that case, please set it so that it can be executed.