A friend of me had a little rant while developing an Angular app on a .NET Core backend. Whenever you had to run the npm run E2E command or any other npm or ng command, you had to right click on the folder node in solution explorer, open with file explorere, open cmd and maybe navigate to the right directory (cd ..).
This made me think of another fun little project to work on. Figuring out a way to open the terminal directly in the directory you want, straight from Visual Studio’s solution explorer. Just right click a folder, click ‘Open Terminal’ and there it is.
Several developers complain about the lack of a proper T4 editor experience, as everything is just shown as plain text. See my other T4 tutorials to see the regular T4 text files in the header image.
I created a Visual Studio extension called T4Editor that adds colors to different blocks in you code. You can easily spot the difference between output code, control blocks and class feature blocks. This increases coding productivity and readability of your T4 templates.
In the future, C# code inside these blocks will also be highlighted with the propper C# syntax colors, I’m still working on that.
Update: Now you can set custom colors! Navigate to Extensions – T4Editor – Adjust Colors and pick whatever colors you want.
Update 2: There is now code completion/minor custom intellisense for codeblocks!
Update 4: I have implemented brace matching and improved RegEx. Typing ‘var string = “#>” for example, will not act as a closing tag.
Click here to see the code. Feel free to give feedback or even contribute!
When working with T4 templates in Visual Studio, you have to manually execute the custom for it to trigger the code generation. I used AutoT4 in the past (a VS2012 extension) to execute template on build time but this extension is not supported in the latest version(s) of Visual Studio.
T4Executer is open for contributions and improvements, the source code can be found here. To use, just install the extension. You can download it at the marketplace.
It is a pretty simple package, that ties execution of specified T4 templates to BuildEvents (BuildEvents interface). It’s possible to configure which templates to run before build, after build or just ignore a template completely.
You can configure T4Executer viaExtensions - T4Executer - Configure. A window will open that lists all templates found in your solutions’ projects.
Click on the template you want and specify to run before or after build by clicking the button under the list. You can also select multiple templates at a once. Templates you move to ignore will never be run, this can be useful for .tt files which don’t generate code or just hold some Class Feature Blocks or a collection of Import Directives. Note that the extension filters out .ttinclude files as this extension is mainly used for files that do not output generated code.
By default all templates are executed before your projects build event, when building or rebuilding your solution. Selecting Extensions - T4Executer - Disable will disable this default behaviour.
The timespamp of the generated the generated file is by default not preserved when the content of the file is not changed. There is a option to toggle this via Extensions - T4Executer - Preserve Timestamps.
I hope this helps you while working with T4 Templates in your solution. If you have any questions or feedback, please leave a comment below.