Are there any alternatives to T4 templates and EnvDTE for cross platform asp.net 5 development?
Asked Answered
I

7

13

We currently use T4 templates to generate C# code files based on C# Code (Entity POCO's) and the EDMX (in older applications)

Moving to ASP.NET 5 with a view to support cross platform development, are there any code generation tools available that can read a projects class & meta data structures, and to generate C# files at design time, similar to T4 templates?

Immensity answered 3/1, 2016 at 15:27 Comment(0)
G
3

Even tt templates weren't supported earlier in asp.net-5 projects. It has been added recently (in Visual Studio 2015 Update 1).

The discussion is here: https://github.com/aspnet/Home/issues/272

And in the thread, Eilon said the Roslyn is the way to analyse the code and generate new ones... And he again said the ASP team has no immediate plan. So currently we have no solution from Microsoft.

But I did some search and didn't find any cross platform community tool to generate codes based on other codes. :(

Grim answered 5/1, 2016 at 23:55 Comment(0)
E
6

You can try Scripty - it is Roslyn based scripting.

  • Syntax highlighting for *.csx files works out of the box;
  • VS extension exists to process scripts on save;
  • MSBuild task exists to process scripts during buld process;
  • Script allows you to traverse a hierarchical model of the project;
  • Multiple files generation from single script is supported;
Ecru answered 28/6, 2017 at 16:11 Comment(1)
The Scripty VS extension is no longer available for the mac and had problems geting the MSBuild library to work... The assemblhy name was invalid as was missing Scripty.MsBuild.dll maybe it was never available.Barreto
H
4

yeoman seems to be a good option. There exist some generators already for scaffolding asp.net 5 apps and you can create your own generators

It is cross platform and can be used on linux/mac/windows.

Heinrike answered 3/1, 2016 at 15:33 Comment(2)
does yeoman allow for introspecting cs files in the current project? I think that is what Mark (and I) are looking for.Kabul
I'm not an expert on yeoman, I just know that is what people are using for project templates on other operating systems for asp.net core where they don't have the full visual studio to generate the project templates, probably should google that or ask that specific question if you can't find it googlingHeinrike
G
3

Even tt templates weren't supported earlier in asp.net-5 projects. It has been added recently (in Visual Studio 2015 Update 1).

The discussion is here: https://github.com/aspnet/Home/issues/272

And in the thread, Eilon said the Roslyn is the way to analyse the code and generate new ones... And he again said the ASP team has no immediate plan. So currently we have no solution from Microsoft.

But I did some search and didn't find any cross platform community tool to generate codes based on other codes. :(

Grim answered 5/1, 2016 at 23:55 Comment(0)
S
1

Scriban worked for me as an alternative to replace T4 templates for generating unit test code. But it's a pure templating engine.

Surfboard answered 22/2, 2019 at 6:11 Comment(1)
When you say "But it's a pure templating engine", is that different from what T4 is?Tercel
K
0

I have been looking into this same sort of thing, although not specifically with ASP.NET MVC but across project types as I move to .Net Core. I kept my T4 relying on EDMX early on. Which works out since EF7 moves beyond the file format in favor of code. Where I was planning on going with my code generation was a combination of ScriptCS and C# REPL. Roslyn complicates things for me a tad since there are no design time assemblies from what I understand. So the trick may be to understand and introspect cs files from within the project.

Kabul answered 11/2, 2016 at 22:38 Comment(0)
S
0

The new LeMP preprocessing engine for C# can be used as an alternative to T4 templates. May I ask what specifically you mean by "projects class & meta data structures"?

Succinct answered 13/3, 2016 at 13:58 Comment(4)
We use T4 templates to read properties from classes within a specific namespace and also look at property attributes.. These are generally Entity Framework pocos and generate boilerplate CRUD code after looking at these . I will have a look at LeMP. Thanks.Immensity
Hmm, do you mean that with T4 you can do reflection on.... what, DLL references in the current project? You can't inspect other classes (which haven't been compiled yet) in the same project, can you? I wasn't aware T4 had this capability. LeMP can't do anything like that at the moment. Can you point me to any articles on this capability?Succinct
Yes, T4 references EnvDTE which is a Document Model for a Visual Studio Project so the T4 can iterate through, not using reflection. This is why T4 is only available VS and not cross platform. Ideally we would like something to parse classes in the same way and have methods to get public/virtual Properties, check for specific property attributes etc .. some more info: t4-editor.tangible-engineering.com/blog/…Immensity
That is interesting. Thanks.Succinct
E
0

For this purpose I created a simple python script called "autogen".

How it works: the script takes jinja template file (instead of T4) and data file (json) and generates one or multiple output files (e.g. C#, or any format) based on the template.

How it can be used (example): describe interface (methods, parameters) definition in json file, create *.j2 template files for C#, Java, Python, etc.. Then run autogen.py script and files for proxy/stub for all languages will be created.

This can help to create lot of similar code without copy/pasting and make code changes by simply changing single json file.

Moreover it can be integrated with build pipiline, msbuild (.net, .net core) and is cross-platform.

Ecru answered 11/1, 2019 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.