Configure T4 forests with PowerShell

I want to create a Custom Scaffolder that uses arbitrary PowerShell logic. It can display T4 templates (several if I want), with the result:

  • Like a new file in your project
  • as a new block of code inserted into an existing class
  • my PowerShell logic can use the Visual Studio s API to manage files and code elements in other ways.

How to customize T4 linings using PowerShell?

+4
source share
1 answer

From http://blog.stevensanderson.com/2011/04/07/mvcscaffolding-creating-custom-scaffolders/ :

In the Visual Studio Package Manager console, run the following command:

Scaffold CustomScaffolder ClassName 

This adds a CodeTemplates folder to the project containing the files for the new core.

As you can see, we have two files:

  • PowerShell script (.ps1) where we can put arbitrary logic to decide which templates will be rendered and where the result will be displayed. From default, it displays the T4 template and uses the output to create a new file called ExampleOutput in the root directory of your project.
  • T4 template (.t4) , i.e. what the default .ps1 file does. By default, a simple C # / VB class is created (depending on your project type). If you want this to work, you can run the custom skiffolder right away:

    Scaffold ClassName

This will create a new file of the ExampleOutput.cs class in the root of your project folder. It is really simple to show you how it works. We don’t really want this, so we are not starting a new scaffolder yet, or if you have already done so, delete ExampleOutput.cs

+4
source

Source: https://habr.com/ru/post/1446944/


All Articles