Skip to content

Support for .tt templates in .kproj files? #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kevinkuszyk opened this issue Jan 14, 2015 · 61 comments
Closed

Support for .tt templates in .kproj files? #272

kevinkuszyk opened this issue Jan 14, 2015 · 61 comments

Comments

@kevinkuszyk
Copy link

Is there a plan to include support for .tt files in .kproj files in vnext?

I just tried adding T4MVC to an MVC6 project but the .tt files didn't get copied into the project.

When I manually copied the .tt into the project folder, it showed in in the solution explorer, but the option to run it was missing.

@kevinkuszyk kevinkuszyk changed the title Support for .tt in .kproj files? Support for .tt templates in .kproj files? Jan 14, 2015
@Eilon
Copy link
Contributor

Eilon commented Jan 15, 2015

No current plans to support it.

@kevinkuszyk
Copy link
Author

Thanks for the quick reply.

Fyi. - I just posted this on the user voice page too:
https://aspnet.uservoice.com/forums/252111-asp-net-vnext/suggestions/6970908-support-t4-templates-in-kproj-in-visual-studio

@wwwlicious
Copy link

+1

Until MVC changes from using awful magic strings for routes, partials etc T4MVC has been invaluable for refactor friendly, view compile-time checking.

@Html.ActionLink("Home", "Index", "Home") == "urgh"
@Html.ActionLink("Home", MVC.Home.Index()) == "win"

@Eilon
Copy link
Contributor

Eilon commented Jan 19, 2015

Indeed, you'll get no argument from anyone on the team that hard-coding repeated strings all over the place is rather... lame. OK actually it's super lame.

As part of this discussion I think it's important to share my thoughts as to why TT probably isn't going to be the long-term solution. First, it's pretty much a VS-only, design-time-only solution. People have done hacky workarounds to run TT files from the command-line but it's not a supported pattern. And in the world of ASP.NET 5, being a Windows-only VS-only solution just isn't going to cut it. I have to be able to use the feature when part of my team might be running Ubuntu and using vim to work on their part of the project. Secondly, editing support for TT files in VS is rather weak. There have been some reasonable 3rd party solutions, but nothing in-box.

So then what do we choose instead? I call my next witness, the Roslyn C# compiler. Roslyn lets you do neat things. Roslyn lets you do some pretty amazing things. One of those amazing things with the way ASP.NET 5 uses Roslyn is that it lets you at compile time (which is also potentially at run time (well, load time, really)) process the syntax trees, generate some code of your own, and then let the world keep turning.

This is how ASP.NET 5 does assembly neutral interfaces (ANIs). ANIs are implemented in ASP.NET 5 by having a scanner that looks for certain attributed types and deletes their syntax trees, and, instead, creates new tiny assemblies with just those types.

Now imagine a world where you could do the same functionality as you had with T4MVC, but potentially far more robust (you have Roslyn, after all), far faster (you already have all the syntax trees parsed), cross-platform (Windows, *nix, OS X, etc.), and can generate code hopefully more easily (well, with Roslyn that isn't always the case, but still).

So, I'd love to hear thoughts on a solution that heads in that direction. Anyone?

@kevinkuszyk
Copy link
Author

I was thinking about this some more last week and came to the conclusion cross platform support might be the blocker and Roslyn the solution.

I opened this issue because I don't want to loose access to the strongly typed action name, view names etc. we get from T4MVC.

If there is better cross platform solution then I'd go for that.

Do you see a Roslyn based T4MVC replacement shipping as part of MVC 6 or in a future version, or is there still space for a community lead extension?

@Eilon
Copy link
Contributor

Eilon commented Jan 19, 2015

The team doesn't have any immediate plans to build this out, so I think a community-organized solution is the best place to start. But, of course, the team would love to hear about any roadblocks or issues found while building such a solution because we would very much like to unblock any such issues.

@kevinkuszyk
Copy link
Author

Thanks @Eilon. I'll try to build something, but I'm not sure where to start. I have two big questions to begin with, but I'm not sure where to post them:

  1. Is there a project.json hook we can use to invoke our code generation? Or should we use the BeforeBuild target in the .krpoj file.
  2. How to use Roslyn "reflection" to find all the controller classes in a project? I've looked through the samples in the Roslyn repro, but I can't see an example of loading a project and finding types in it.

@davidfowl
Copy link
Member

You can follow the same pattern as the RazorView preprocessor https://github.com/aspnet/Mvc/blob/dev/samples/MvcSample.Web/Compiler/Preprocess/RazorPreCompilation.cs. In the target project, the file needs to go into compiler/preprocess/*.cs.

How to use Roslyn "reflection" to find all the controller classes in a project? I've looked through the samples in the Roslyn repro, but I can't see an example of loading a project and finding types in it.

You need to play with the API and learn how to use it. Not sure where you can find better examples but they do exist somewhere

@mcquiggd
Copy link

This really is not good enough.

There is huge investment in T4 throughout the .Net community, focusing directly on MVC applications from T4 scaffolding of custom project items, through utilities, to entire frameworks. There is an implementation of T4 for Mono; T4 is open source.

We have T4 templates for generating TypeScript, KnockOut Models, just about everything you could imagine that is related to web development.

While Visual Studio 2015 increases support for T4, the ASP.Net vNext team decided to quietly drop it completely. And to add insult to injury, the reasoning mentioned is because 'T4 would not work for my team members that use Ubuntu'. Comments such as 'Just rewrite it in Roslyn', 'maybe the community will come up with something' are far from helpful.

I am seriously concerned at the direction the ASP.Net team are taking. I could just about suffer the pain of being forced to write tortuous Gulp scripts to compile SASS and put it in a location, bundling and minification, that used to be simple and straightforward, but to find out that the ASP.Net team prefer to focus on the needs of the tiny Ubuntu / Sublime text on a Mac user base, rather than their core audience of Visual Studio users, is maybe the last straw.

Already .Net developers are looking at all these changes, and thinking 'why should I bother - if Microsoft doesn't even like its own ecosystem, I might as well become a Node developer, with WebStorm and AWS'.

P.S Regarding 'TT is design time only solution'; hmm... that would be why I can create runtime T4 templates - in anything but ASP.Net vNext.

@mcquiggd
Copy link

As a follow up, where is the best place to register opposition to this design decision? Can you recommend the relevant User Voice / blog / issue tracker for this, if you are not interested in listening to people?

@davidfowl
Copy link
Member

We've been developing ASP.NET 5 so far by making so any solution we come up with is viable cross platform. It's a good principle and it helps us design things that can work well from the command line but that will also light up in visual studio.

We're looking at bringing back single file generators so things like T4 and resx will just work (within visual studio only of course). We'll likely have to find other solutions for problems like these when developing cross platform.

One of our devs even ported T4 to CoreCLR https://github.com/bricelam/TextTemplating.

Stay tuned.

@mcquiggd
Copy link

@davidfowl - thanks for the quick response.

So, can you please clarify whether or not T4 will in fact be officially supported in ASP.Net MVC 5 ?(clarification - ASP.Net MVC5 is how this appears in Visual Studio 2015 preview templates - this is officially MVC6)

Seriously, I do get the cross-platform concept, and I see its importance for Microsoft. But you are losing existing developers due to these kind of decisions. Check out the threads in even the .Net groups on LinkedIN, for evidence that your core audience is having doubts and looking around.

a) Its a good principal - for runtime if people want to host on Linux. But - wont someone 'think of the developers..?' ;-) Don't make the design decisions for the lowest common denominator (a text editor). Your ace card is precisely Visual Studio. If T4 is not available for sublime text etc, then let their 'community' add it if they want. Don't just remove it for the majority of us, who need Visual Studio for the other one thousand and one things it does better in the Microsoft ecosystem than a text editor.

b) At least provide a transition period - and pre-warn people that if they expect to be able to move to MVC vNext, they will be losing T4 support. This has not been made public, amid all the celebrations of adding bower, NPM, Grunt, Gulp, etc etc.

c) I would seriously think it would be easier and simpler to build a T4 host for the tiny proportion of Linux and Mac users who would need it.

@davidfowl
Copy link
Member

So, can you please clarify whether or not T4 will in fact be officially supported in ASP.Net MVC 5?
I think you mean MVC 6 not 5.

T4 hasn't been removed per se, our project system just doesn't support single file generators at the moment (the little Custom tool icon on file properties). We're actively investigating the work that it will take to bring this back.

As for T4 on .NET Core? That's not a thing but it won't matter unless you're trying to port T4 itself to run on the new CLR. If you're only concerned with your existing Visual Studio workflow then it won't make a difference.

Things are generally removed by default then brought back when we figure out how to make things work well everywhere (more people use sublime than you think 😄). That's not a hard and fast rule that can't be broken though.

I'll update this thread when we have more information about our single file generator story.

/cc @sayedihashimi @BillHiebert

@mcquiggd
Copy link

@davidfowl

Thanks again...

Well, T4 does not work in the ASP.Net MVC 5 preview projects (which are officially MVC6). It only works in the existing MVC projects that do not use gulp etc.

To reiterate, my personal interest (and probably also for the hundreds of thousands who don't know about the lack of T4 support as it stands today), is simply that we need to keep T4 available and functional within ASP.Net MVCx, as it is within other Visual Studio 2015 project types, for existing users so they can continue to leverage the time and investment they have made, in a technology that Microsoft is still promoting outside the ASP.Net vNext team.

I know a lot of people use Sublime - I do too - but just like 99% of the others, I do not use it for .Net ;-)

Off topic - seriously, if Microsoft want to be relevant client side, invest some love in http://Aurelia.io.

It is an opportunity to leapfrog Google (Angular), Facebook (React), and Twitter (Bootstrap) and become a name in web dev rather than be viewed as legacy. If you do not have a presence in the client side landscape, people will continue to go straight from Angular etc to Node (ironically using Sublime or WebStorm which is becoming the alternative to Visual Studio) and bypass Microsoft completely. It is this huge gap in Microsoft's toolset that is causing you to have to play catch up rather than leading.

Don´t forget your existing developers because you are too focused on chasing open source users on other platforms who actively view Microsoft as an enemy, and are pretty darn unlikely to suddenly change.

@davidfowl
Copy link
Member

Well, T4 does not work in ASP.Net MVC 5 preview projects ;)

ASP.NET 5 -> MVC 6

To reiterate, my personal interest (and probably also for the hundreds of thousands who don't know about the lack of T4 support as it stands today), is simply that we need to keep T4 available and functional within ASP.Net MVCx Preview, as it is within other Visual Studio 2015 project types, for existing users so they can continue to leverage the time and investment they have made, in a technology that Microsoft is still promoting outside the ASP.Net vNext team.

That's well understand. Although you'll still have to tweak the generated content as the frameworks have changed.

@mcquiggd
Copy link

@davidfowl Tweaking is not a problem, compared to not having the option at all...

Thanks for your feedback - I will shut up, for now :-)

@mcquiggd
Copy link

P.S can we please re-open or create a new issue for this, as it is obviously not closed.

@davidfowl davidfowl reopened this Apr 26, 2015
@Perambulator
Copy link

+1 for full T4 support or at least design time

This came as a surprise to me because I assumed that T4 would be added and this was just in beta. I cant imagine why T4 would be seen as being redundant as it is so heavily used by existing tools and commercial products. Say it aint so!

@rogerhendriks
Copy link

+1

@aukgit
Copy link

aukgit commented Apr 27, 2015

+1 for T4
Well in MVC 2 demos Microsoft repeatedly mention to use T4 and now they putting it in hold. For few months I have seen that every new thing that Microsoft created , destroyed the previous one. When EntityFramework 6 released, it was a whole new thing , they move namespaces from .NET to EntityFramework 6 which cause lot of problems with existing projects. Idea was to have a standalone product. Okay good, now in EF7 they are going to break everything and it almost a new thing and many support(edmx , database first approach[not sure]) will not be there too. Which begs a serious question "does Microsoft build anything with their technology?" .

I like T4 , however I don't like T4MVC . I do like CodeTemplates for T4 generation.

@ghost
Copy link

ghost commented Apr 28, 2015

+1 for T4 please

aukgit I agree I do not think Microsoft use these tools for real applications like we do. They give guidance to use T4 then remove it. Resx is used very heavily for translations but that is missing too? So now we have to throw away our code because Microsoft want to please Linux fans? Very very bad move Microsoft.

@guardinfo-spam
Copy link

+1 for T4 please. there's a lot of time invested in T4 and frankly if Ubuntu users don't like it then they don't have to use it. Those of us who made extensive use of T4 templates need them to work as they always did. Can the team please consider this seriously. If they only work on windows then so be it. Your vast majority of users is on windows and we're simply offering this world to other OSes.

Dropping important parts of the system to please a specific platform is not a good idea and it will end up hurting you in the long term. You can't drop support just like that and you cannot seriously expect the community to do your job for you.

So please stop this thing with Ubuntu users or whatever other platform you want and think about your current users. Don't kick us all out in your quest to gain new users. You need to keep your existing developer base happy. If you do drop it, you will basically make us all think that you can stop support for anything very quickly which will more than likely alienate a very large base of your current users.

Do you really want this to happen ?

@josher565
Copy link

+1 for T4
T4 has been flying under the radar for a long time, it typically gets the last consideration in the past and it probably is here too. That's ok, but having a generation system is really handy for a lot of uses. I suspect T4 competes with other directions in the current project (yeoman support, roslyn). That's ok too, just so long as we know how/when to use competing solutions and how it supports the old world of T4. I suspect supporting T4 will be simpler. Its probably a good candidate for OSS to let the voices here and others shape and see if it's as important as it's suspected. It probably hasn't found it's ultimate form quite yet and getting Linux feedback (in code or otherwise) would be interesting. Is there such a facility in the LAMP/POSIX stack other than npm tools?

@mcquiggd
Copy link

mcquiggd commented Sep 7, 2015

@davidfowl

Yes, that's my point really - I can make my own T4 stuff work at the moment, but not other peoples published packages. Hopefully they get a roadmap for creating MVC6 compatible versions, where applicable.

I am really very happy with progress of ASP.Net 5 now that a lot of the earlier holes have been filled. We just need the story on code generation (the prelude of which has already been outline by @sayedihashimi)

So, fingers crossed... ;-)

@rpokrovskij
Copy link

I agree to remove Razor because we will get better instruments instead (we will work in another paradigm where there are no "server-side only" templates - and I appreciate it). But removing T4 what you are giving to us? We use T4 to generate code (typescript, html, various templates) from EF6 model. How we could use Roslyn for it (to generate ts,html)? How we could use yeoman to analize EF6 model? And again we can't migrate to yeoman today exactly because we will need tomorrow migrate to Roslyn. And we have a lot of code. So please keep T4 and T4 scaffolding working (at least from command line). It is not about the web client-server architecture it is just a quick file from EF6. Or propose anything that can replace current T4 scaffolding for us. There is no problem for me to rewrite CodeTemplates to generate whatever MVC6 will appreciate, so just because the current view/controller scaffolding generates Razor there is no need to remove T4 scaffolding from VS.

@BillHiebert
Copy link

Note that T4 is supported in the RC release of ASP.NET 5 if you have Visual Studio 2015 Update1 installed. It uses the same custom tool mechanism that csproj projects use.

@dls314
Copy link

dls314 commented Dec 3, 2015

@BillHiebert I'm not sure if you mean that you can import and use the Microsoft.TextTemplating.targets from the Modeling SDK for Microsoft Visual Studio 2015, but I found out that you can do that in RC 1.

@tibitoth
Copy link
Contributor

I created a tool for asp.net 5 to generate t4 templates in design time (in linux or mac too). https://github.com/totht91/TextTemplating

@MaxHorstmann
Copy link
Contributor

@BillHiebert great news, can't get T4MVC to work with ASP.NET 5 yet though, see http://stackoverflow.com/questions/34399939/make-t4mvc-work-with-asp-net-5

@fredriksknese
Copy link

I have been thinking about using Roslyn analysers to get the same functonality.

@fredriksknese
Copy link

It may not have the exact functionality. But it helps with the code generation for static strings in MVC.

@brightertools
Copy link

+1 for T4 support in MVC6 and Class Library Packages.
T4's do seem to work in Class Library Packages (where we generate CRUD/boilerplate code from Entity Classes) but this only works when clicking "Run Custom Tool" on the .tt file, "Transform All T4 Templates" does not work.

UPDATE:

Although T4 templates work in class library packages for single file output, Multiple file output, using a TemplateFileManager hangs Visual Studio. If I move the T4 to a regular class library project and output files to a Class Library package, Visual Studio also hangs. It seems like T4 is not completely supported.

Has anyone else found this issue?

@mcquiggd
Copy link

mcquiggd commented Feb 4, 2016

Hi all,

I dislike "Magic Strings" intensely, and MVC is way too overloaded with them. I don't use T4MVC as I personally find that far too heavy for my needs, and I have some approaches that I use for my own company projects.

So, I have uploaded an initial sample of automatically generating Controller Names, Action Names and View Names with both the MVC5 and MVC6 RC1 Update 1 versions of the excellent MVC Boilerplate.

https://github.com/Pro-Coded/MVCMagic

I will be adding more features over the next couple of days; on the roadmap is also use of Roslyn for (at least) metadata extraction, with a view to being flexible enough to move to any future approach that might supercede T4. I really want people to let me know what they think... I want some feedback to see if there is an audience for this before releasing a NuGet probably next week.

David

@sayedihashimi
Copy link
Member

FYI in RC1 we have added support for .tt files so I'll go ahead and close this. If there is more info here that I missed please let me know.

@mcquiggd thanks for the link, I'll check it out.

@brightertools
Copy link

I don't believe its fully supported given my experience of the last 2 days?

  1. "Transform All T4 Template" in the Build menu doesn't work
  2. Generating multiple files hangs VS when files are added to a class library package project.

@kevinkuszyk
Copy link
Author

@sayedihashimi can you re-open? Like @mredman I found the following problems:

  1. When I add the T4MVC nuget package the .tt doesn't get copied to the project (I guess this is due to the changes in how NuGet handles content files? I will open a separate issue on T4MVC to track this).
  2. When I manually copy the .tt file over, right click and choose "Run Custom Tool" VS hangs.

I pushed a sample repro here: https://github.com/kevinkuszyk/aspnet-core-t4mvc

/cc @davidebbo

@ghost
Copy link

ghost commented Feb 5, 2016

I have tried running T4 templates to generate multiple files again on a class library package use a new install of windows 10 and VS2015: it still hangs, but the menu items seem to be working (ie running the template as it hangs when I clicked click one of the)

@sayedihashimi
Copy link
Member

@kevinkuszyk I have opened a new issue for a more generic implementation for single file generators at aspnet/Tooling#394

When I add the T4MVC nuget package the .tt doesn't get copied to the project (I guess this is due to the changes in how NuGet handles content files? I will open a separate issue on T4MVC to track this).

This is by design from the runtime. For dnx/dotnet project NuGet packages with content are not supported. To discuss this please open an issue in either http://github.com/aspnet/dnx/ or https://github.com/dotnet/corefx.

When I manually copy the .tt file over, right click and choose "Run Custom Tool" VS hangs.

Can you open a seperate issue at https://github.com/aspnet/Tooling/issues with specific repro steps?

@brightertools see my comment right above this.

@giancarloa
Copy link

+1 for T4 support...

yikes... ever since asp.net core hit rc status I began looking at it... almost fainted when i found out about t4... pretty much agree with everything @mcquiggd has said... thanks @mcquiggd for your voice...

@kevinkuszyk
Copy link
Author

@sayedihashimi thanks. New issues open as requested.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests