43

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?

DCastenholz
  • 196
  • 2
  • 12
NBM
  • 1,231
  • 4
  • 14
  • 18
  • I was looking into this a few days ago. You could try this: http://www.bricelam.net/2015/03/12/t4-on-aspnet5.html – OzieGamma Sep 04 '16 at 05:14
  • 1
    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. – Maslow Nov 16 '16 at 16:03
  • 30
    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. – ericdc Dec 10 '18 at 15:43
  • 2
    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. – John Holliday May 29 '19 at 15:44
  • 1
    Please explain how this can be made on topic As I'd like to know the answer to the question... – Thundter Jan 08 '20 at 10:59
  • @John Hilliday, do you have an example? – dadhi Feb 15 '20 at 14:46
  • @dadhi - 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. – John Holliday Feb 15 '20 at 18:31
  • @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+ – dadhi Feb 16 '20 at 11:21
  • Source Generators are the way to go since .NET 5 and on. – Cosmin Sontu Sep 18 '21 at 22:10
  • Per the request by multiple users. this question has just been re-opened. – DCastenholz Sep 22 '21 at 19:52
  • Additional question with some good options: https://stackoverflow.com/questions/47691299/texttemplating-target-in-a-net-core-project – DCastenholz Sep 22 '21 at 19:57

1 Answers1

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.

OzieGamma
  • 426
  • 3
  • 10
  • 1
    Scripty works with Visual studio code (.NET Core) as cross platform solution or not ? – NBM Sep 04 '16 at 09:57
  • Havent tried. The docs say it is a juger package based on Roslyn, so Id say yes – OzieGamma Sep 04 '16 at 14:23
  • @OzieGamma, do you know if it supports runtime generation? I don't see anything about it in project home. – mattinsalto Nov 09 '16 at 22:19
  • I never got into runtime generation, what's your use case for it? – Maslow Nov 16 '16 at 16:04
  • 2
    Scripty does not work with .net core. Additionally VS 2017 has introduced major problems to it as well. – Andrew Hoffman Sep 28 '17 at 18:43
  • 1
    The MSBuild idea of scripty seems to be an unreliable solution, so I wouldn't recommend heavy investment into it. – Andrew Hoffman Sep 28 '17 at 18:44
  • 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](https://github.com/StingyJack/StingyBot/tree/master/src/StingyBot.Common/SlackApi). 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. – StingyJack Sep 06 '18 at 16:19
  • 1
    I tried to make Scripty work on dotnet (linux), but no success. That's why I spent some time and wrote python script ([autogen](https://github.com/rustamkulenov/autogen)) which integrates with csproj and generates artifacts (*.cs) from templates on build. I'm using it to generate proxy\stubs\DTOs for messaging. – rustam Oct 27 '18 at 12:30