T4 alternative in .NET Core?
Asked Answered
H

1

44

a T4 text template is a mixture of text blocks and control logic that can generate a text file.

T4 templating is not natively supported in .Net Core.

Can anyone suggest to me T4 alternative in .NET Core?
How can we do code generation?

Horology answered 4/9, 2016 at 4:35 Comment(11)
I was looking into this a few days ago. You could try this: bricelam.net/2015/03/12/t4-on-aspnet5.htmlBarthol
what do you need from T4? I've moved most of my T4 over to simple .net scripts run via LinqPad or a .fsx file from within visual studio directly.Manipular
How is this off topic? This is a real question that I currently have - I found the answer page on SO but you guys closed it as off topic. Unreal.Therapist
Adding here because the topic is closed. For runtime text templates, you can add a separate .NET Framework project to your solution for all your codegen, and then add links to the generated .CS files in your .NET Core project.Gothic
Please explain how this can be made on topic As I'd like to know the answer to the question...Bates
@John Hilliday, do you have an example?Kalin
@Kalin - No I don't have a code example. However, the process is the same as if you had used a T4 template directly in your project, except you add it to a separate .NET Framework project, and then copy the generated code into the .NET Core project. To simplify the build process, add a link to the generated source files instead of copying them so they are updated automatically whenever you modify the T4 template. Then whenever you build the .NET Core project, all the code needed to produce the desired output is included, so there is no need for the T4 templating engine at runtime.Gothic
@JohnHolliday Thanks, I was thinking of example where project is spits the valid generated cs file which is then included into the consumer project (whether it t4 or other gem mechanism). Specifically what will be the MSBuild machinery and how is this solution may be packaged to NuGet. Seems like there is no such a thing. I'll try to experiment with mono t4 first, because it has modern cli tool reference support and the preview packages support ANC 3.0+Kalin
Source Generators are the way to go since .NET 5 and on.Liverpool
Per the request by multiple users. this question has just been re-opened.Myrtie
Additional question with some good options: #47691799Myrtie
B
16

You could use Scripty. It looks quite nice and fits with the new Analyzers from Roslyn.

http://daveaglick.com/posts/announcing-scripty

https://github.com/daveaglick/Scripty

Since they are dropping the project.json format (https://blogs.msdn.microsoft.com/dotnet/2016/05/23/changes-to-project-json/) you should be able to use Scripty from the .xproj or .csproj file.

Barthol answered 4/9, 2016 at 5:17 Comment(8)
Scripty works with Visual studio code (.NET Core) as cross platform solution or not ?Horology
Havent tried. The docs say it is a juger package based on Roslyn, so Id say yesBarthol
@OzieGamma, do you know if it supports runtime generation? I don't see anything about it in project home.Stenograph
I never got into runtime generation, what's your use case for it?Manipular
Scripty does not work with .net core. Additionally VS 2017 has introduced major problems to it as well.Softcover
The MSBuild idea of scripty seems to be an unreliable solution, so I wouldn't recommend heavy investment into it.Softcover
I've only ever used scripty from within VS 2017 and its been fine as far as the CustomTool goes. I imagine the CLI would do the same since its quite literally the same engine. It will generate whatever output you can make from any c# program, but the examples are written with the IVSSingleFileGenerator in mind, but you can do more complex things as well. The CustomTool function that can be invoked like T4 templates will not work in vscode, because vscode does not support that kind of tooling to begin with.Emblaze
I tried to make Scripty work on dotnet (linux), but no success. That's why I spent some time and wrote python script (autogen) which integrates with csproj and generates artifacts (*.cs) from templates on build. I'm using it to generate proxy\stubs\DTOs for messaging.Palish

© 2022 - 2024 — McMap. All rights reserved.