/T4 Toolbox

How to install T4 Toolbox for Visual Studio 2019

If you’re wanting to use T4 templates for code generation you’ll likely find that there are some limitations such as only being able to generate one output file per template. You will also find that a lot of the examples on the internet depend on using features of T4 Toolbox.

The T4 Toolbox website lists the following features:

  • Generate multiple output files from a single text template
  • Automatically add output files to one or more projects and folders
  • Automatically add or check-out generated files from source control
  • Edit text templates in Visual Studio with syntax colourization, outlining, QuickInfo tooltips, error reporting and statement completion

Currently, as of 28th July 2019, T4 Toolbox has not officially been updated for Visual Studio 2019. You could fall back to using Visual Studio 2017 and download the Visual Studio extension for it from the Visual Studio marketplace here.

However, a member of the open-source community (peterM) has raised a pull request with changes to get it working with Visual Studio 2019. So I followed the pull request to his forked repo and cloned it and started trying to figure out how to build it and install it into Visual Studio 2019. Unfortunately, I couldn’t build it straight away and had to spend some time figuring out how to get it to build successfully.

I’ll go into more detail as to everything I learnt and had to do to get it building further below in this post but for now, I want to detail the steps required to get T4 Toolbox installed into Visual Studio 2019.

You will need to ensure you have the following pre-requisites on your machine:

  • Visual Studio 2019 installed (I used version 16.2) including the .NET desktop development and Visual Studio extension development workloads installed.
  • .NET Framework 3.5 Windows feature installed

The quickest and easiest option is probably to clone my T4Toolbox git repo which is forked off of peterM’s forked T4Toolbox git repo. This contains a fix so it should work out of the box but I can’t guarantee that it will be kept up-to-date.

The second option is to clone peterM’s forked T4Toolbox git repo. If you encounter build issues try deleting the TemplateParser.g.yacc.cs and TemplateScanner.g.lex.cs files in the Debug and Release folders under the “.\src\T4Toolbox.TemplateAnalysis\obj” folder.

The third option is to use Visual Studio 2017 and wait until the official project has been updated for Visual Studio 2017.

Once you can successfully build the project, build it under the Release configuration (Surprisingly this took 80 seconds to build on my machine). You should now see a T4Toolbox.0.0.0.0.vsix file in your “.\src\T4Toolbox.vsix\bin\Release” folder. Open this file to install it into Visual Studio. You will need to close all instance of Visual Studio to allow it to install. Once completed open Visual Studio 2019 click on Extensions > Manage Extensions and you should see that T4 Toolbox for Visual Studio 2019 has been installed.

image

Now that you should be up and running it’s time to get into the fun nitty-gritty of what was involved in figuring out how to get the project to build so that the next time someone has a similar issue it will help them (including myself!).

For reference my machine is running Windows 10 (1809), Visual Studio 2019 (16.2) and I already had the .NET desktop development and Visual Studio extension development workloads installed.

I cloned peterM’s forked T4Toolbox git repo and tried to build it. I was prompted to install .NET Framework 3.5. image I installed it and restarted my machine. When I tried to build it again I got a bunch of errors. For reference I’ve listed the errors and a screenshot below:

image

  • CS0115 ‘TemplateScanner.yyerror(string, params object[])’: no suitable method found to override
  • CS0246 The type or namespace name ‘ScanBuff’ could not be found (are you missing a using directive or an assembly reference?)
  • CS0115 ‘TemplateScanner.TemplateBuffer.Pos’: no suitable method found to override
  • CS0115 ‘TemplateScanner.TemplateBuffer.Read()’: no suitable method found to override
  • CS0115 ‘TemplateScanner.TemplateBuffer.GetString(int, int)’: no suitable method found to override
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘buffer’ and no accessible extension method ‘buffer’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘code’ and no accessible extension method ‘code’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘GetCode’ and no accessible extension method ‘GetCode’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘tokLin’ and no accessible extension method ‘tokLin’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘tokCol’ and no accessible extension method ‘tokCol’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘BEGIN’ and no accessible extension method ‘BEGIN’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘tokEPos’ and no accessible extension method ‘tokEPos’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘tokELin’ and no accessible extension method ‘tokELin’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘tokECol’ and no accessible extension method ‘tokECol’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘code’ and no accessible extension method ‘code’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘yyless’ and no accessible extension method ‘yyless’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner.TemplateBuffer’ does not contain a definition for ‘FileName’ and no accessible extension method ‘FileName’ accepting a first argument of type ‘TemplateScanner.TemplateBuffer’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘lNum’ and no accessible extension method ‘lNum’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS1061 ‘TemplateScanner’ does not contain a definition for ‘cCol’ and no accessible extension method ‘cCol’ accepting a first argument of type ‘TemplateScanner’ could be found (are you missing a using directive or an assembly reference?)
  • CS0103 The name ‘ScanBuff’ does not exist in the current context

Once I deleted the TemplateParser.g.yacc.cs and TemplateScanner.g.lex.cs files in the “.\src\T4Toolbox.TemplateAnalysis\obj\Debug” folder I was then able to build the project successfully.

I suspect that when the first build triggered while we didn’t have .NET Framework 3.5 installed that it must have produced bad files. Deleting these files forced them to be regenerated and producing correct files.

I tried running the GenerateScanners and GenerateParsers build tasks directly but that didn’t change anything. I also tried running the commands (gplex.exe and gppg.exe) they were using but those tools said that they didn’t detect any changes so they didn’t regenerate the files. I then looked for a way to force the tools to always generate a new file but didn’t find any so deleting these files before these tools run was the only thing I found which worked.

As a more permanent solution, I’ve added 2 extra build targets which will trigger before each of these files gets generated to ensure that these are always in sync. For reference here is the git commit.

image

From here it was pretty straight forward to build the solution and install the produced T4Toolbox.vsix file which I’ve already detailed instructions for above.

Now to prove that these changes don’t just “work on my machine” I did the following:

  • I created a new Windows 10 (1809) VM in Azure using their templates.
  • Installed Chrome
  • Installed Visual Studio 2019 Enterprise (16.2).
  • Installed the .NET desktop development and Visual Studio extension development workloads
  • Repeated the steps above to make sure that I didn’t miss any steps.
  • I also deleted and cloned my repo to make sure that it works straight out of the box.

Subscribe to Ashley Hollis

Get the latest posts delivered right to your inbox

Ashley Hollis

Ashley Hollis

.NET Software Developer.

Read More