6

I am sure most of you are aware that you can use the Nuget package Microsoft.VisualStudio.Web.CodeGeneration.Design in your ASP.NET Core Web Api project to create Api controller classes with methods for GET, POST, PUT etc. and even Entity Framework code in there.

Now here's my question: What if I want to write my own? I found out about the new C# Source Generators (https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/), but they create code at compile time. I want to do the same thing this NuGet package does: Create code at design time by typing a command into the Powershell Console in Visual Studio or even add my own options to the context menu, so the Code Generator (Scaffolder) adds files to my project that I can see, edit and unit test.

Unfortunately, I wasn`t able to find much on the subject. Can you help?

//Edit: To make this clear: I am not looking to modify the existing ASP.NET Core Scaffolder. I want to create my own extension and I am looking for tutorials, examples or anything that will help me get started :)

  • Did you make any discoveries since posting this? I am fairly interested in something similar. – Marcom Jul 24 '21 at 02:06

2 Answers2

2

First, find the Templates folder in your: C:\Users{username}\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\{your project version} enter image description here

Copy and paste into your project(Here I only keep the ControllerGenerator folder, you can also operate on other folders.).

enter image description here

Then add nuget package Microsoft.VisualStudio.Web.CodeGeneration.Design in your project.

Then you can customize the scaffolding template under this folder.

Test Result:

enter image description here

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • Thanks for your input Yinqiu. I will use this at some point. But my question was more on the line of creating my own extension (that can maybe add multiple files, create folders etc) rather than modifying the existing templates! – Robert Bantele Feb 05 '21 at 15:59
2

.NET Tool

There is an article here that describes a simple dotnet tool.

It is basically a console application with options set in .csproj as follows:

<PackAsTool>true</PackAsTool>
<ToolCommandName>MyTool</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>

Then make a Nuget out of it using dotnet pack and install the tool in your project or globally. Then you can:

dotnet MyTool Param1 Param2 ParamN

Or run like a regular application:

D:\MyDependentApp> dotnet run --project  path/to/MyTool Param1 Param2 ParamN

Scaffolding

To create a scaffolding, you can use Text Template Transformation Toolkit or most commonly known as T4. An example of a template (with .tt file extension):

<#@ output extension=".cs" #>
<#@ template language="C#" hostspecific="true" #>
<#@parameter type="System.String" name="ClassName" #>
public class <#=ClassName#>
{  }

To process the template, you can use Mono.TextTemplating package, here's the nuget and the source.

var generator = new Mono.TextTemplating.TemplateGenerator();

var session = generator.GetOrCreateSession();
session.Add("ClassName", "MyClass");

var result = generator.ProcessTemplate(templatePath, outputPath); 

The output file:

public class MyClass
{  }

PS

The example above is just a starting point.

kazinix
  • 28,987
  • 33
  • 107
  • 157