T4 Toolbox for Visual Studio 2012


Beta version of T4 Toolbox for Visual Studio 2012 is now available from the Visual Studio Gallery. You can install it from Visual Studio itself, using the Extension Manager. Simply select TOOLS, Extensions and Updates from the main menu; search the Visual Studio Gallery for T4 Toolbox and click Download.

image

Once the T4 Toolbox package installed, you can find T4 Toolbox project item templates in the Add New Item dialog of Visual Studio for C# and Visual Basic projects.

image

Changes in this version

This release of T4 Toolbox is significantly different than the previous version for Visual Studio 2010 and 2008. It was rebuilt from the ground up to take advantage of new capabilities of Visual Studio 2012 and Text Templating Transformation Toolkit.

VSIX package

The most obvious benefit is ability to deliver it as a VSIX package via the Extension Manager instead of an MSI installer. Instead of having to check for and download updates manually and wait for several long minutes for installation to complete, you will get automatic update notifications from Visual Studio and can update the package in just a few seconds. Admittedly, packaging T4 Toolbox as a VSIX alleviates even more pain on the development side, as it finally allows me to reduce the steps required to configure development environment to simply getting latest from source control and hitting F5. For both of these reasons, VSIX packaging will allow much more rapid delivery of the T4 Toolbox updates going forward.

No ready-to-use code generators

T4 Toolbox for Visual Studio 2012 does not include ready-to-use code generators. LINQ to SQL, SQL Enum, AzMan,  and other generators that were shipped with the previous version are no longer included. This decision was based on several factors. Some of these code generators became obsolete with the release of new frameworks and would be a waste of time to move forward. Dropping them, at least for the time being, also allows me to concentrate on improving the tools, and quite frankly, makes this release and the ongoing development feasible. I also think that NuGet packages are a better way to deliver T4 templates than VSIX. If you need to use any of the legacy code generators with Visual Studio 2012 I will do my best to support your effort to upgrade them on CodePlex.

No ready-to-use directive processors

T4 Toolbox for Visual Studio 2012 does not include any directive processors you can directly use in your templates. Directive processors are difficult to develop and maintain. They generate code generation code and have to be implemented using CodeDOM. They are also harder to use than templates because code they generate is invisible to you by default. In addition, the VolatileAssembly is no longer necessary because in Visual Studio 2012 the assembly directive shadow-copies the assembly files before loading them. The Xsd directive was a simple convenience wrapper for functionality provided by the xsd.exe command line utility. Most XML schemas are relatively static and you can use the utility to generate the XML serialization classes and include them in your templates directly.

Going forward, I think that developing reusable templates is a simpler and better way to package code generation logic than directive processors. When support for multiple output languages is required, you can use CodeDOM in preprocessed templates. The only time you have to use the directive processors is when you need to change the transformation process itself. For example T4 Toolbox still relies on a directive processor in the T4Toolbox.tt include file to initialize the TransformationContext and save additional output files, however you shouldn’t use this directive processors in your code.

No runtime templates

This required creating a separate Extension SDK VSIX package and didn’t make the first release. Let me know if you are actively using the runtime (a.k.a. preprocessed) templates with T4 Toolbox today.

API Changes

The original version of the T4 Toolbox was developed with the assumption that code generation is always performed at design time, when you save a template file or Run Custom Tool from the Solution Explorer. Since then, T4 gained better MSBuild support and many developers want to perform code generation at build time or both build and design time. In order to allow supporting this functionality in the future, the T4 Toolbox API was changed to remove direct dependency on Visual Studio API and be consistent with the MSBuild API.

TransformationContext

This class now fully implements the Singleton pattern consistently with HttpContext and other similar classes in the .NET framework. Instead of multiple static properties, like Transformation and Host it defines a single property called Current which can be used to access instance properties of the singleton object. So instead of accessing transformation host via TransformationContext.Host, you now have to access it via TransformationContext.Current.Host.

DTE, Project, ProjectItem, DefaultNamespace and RootNamespace properties are no longer available. They had internal dependencies on the Visual Studio APIs and were not forward compatible with MSBuild. However, TransformationContext now implements the IServiceProvider interface and you can use the new GetService method to access the same information like so.

<#@ template language="C#" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.11.0" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.Shell" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#
// Get DTE
var dte = (DTE)TransformationContext.Current.GetService(typeof(DTE));

// Get ProjectItem representing the template file
ProjectItem projectItem = dte.Solution.FindProjectItem(TransformationContext.Current.Host.TemplateFile);

// Get the Project of the template file
Project project = projectItem.ContainingProject;

// Get the root namespace of the project
var rootNamespace = (string)project.Properties.Item("RootNamespace").Value;
this.WriteLine(rootNamespace);

// Get the default namespace of the project item
var vsSolution = (IVsSolution)TransformationContext.Current.GetService(typeof(SVsSolution));
IVsHierarchy vsHierarchy;
ErrorHandler.ThrowOnFailure(vsSolution.GetProjectOfUniqueName(project.FullName, out vsHierarchy));
uint projectItemId;
ErrorHandler.ThrowOnFailure(vsHierarchy.ParseCanonicalName(projectItem.FileNames[1], out projectItemId));
object defaultNamespace;
ErrorHandler.ThrowOnFailure(vsHierarchy.GetProperty(projectItemId, (int)VsHierarchyPropID.DefaultNamespace, out defaultNamespace));
this.WriteLine((string)defaultNamespace);
#>
OutputItem

The old OutputInfo class has been updated for consistency with MSBuild’s ProjectItem class. It has been renamed to OutputItem. Its BuildAction property was renamed to ItemType and its BuildProperties property was renamed to Metadata.

Roadmap

Going forward I want to take the T4 Toolbox the point where it provides first-class developer experience for working with text templates. This includes adding some of the missing features that didn’t make the first cut, like unit testing as well as adding new features, like build-time code generation, support for additional project types, namely SQLDB, and Visual Studio language service with colorized syntax and IntelliSense.

Information and Links


Other Posts
ASP.NET Dynamic Data Unleashed

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

[…] T4 Toolbox for Visual Studio 2012 (Oleg Sych) […]

Congrats! I’ve been waiting on this! I look forward to all the changes you have in store.

Sincerely,

Rick

Maybe a static method in the transformationcontext that alleviates the host at least, so it don’t break too many templates. Like so:

public static ITextTemplatingEngineHost Host
{
get
{
return TransformationContext.Current.Host;
}
}

Thanks for the feedback, Rick. Let me think about it.

Is T4ScriptFileGenerator no longer supported with this version?

It didn’t make the first cut. How did you use it with the previous version?

In previous versions, I was using T4ScriptFileGenerator to automatically run a T4 template whenever a particular input file was saved.
Are there any alternative solutions for 2012?

There is no direct replacement. I will add the ScriptFileGenerator back in the next update of T4 Toolbox.

Great news, thanks!

Thanks mate.
How do I make this generic in my VS2012 project file, such as it works on my mates machines? Want to transform during building.

C:\Users\Olivier\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\zi351sj2.0n2\Include

Olivier,

Your team mates will need to install the T4 Toolbox VSIX on their machines as well. At this time it does not support build-time code generation, only design-time (run custom tool). How important is build-time code generation would be on your projects?

Oleg

Is there project integration with .sqlproj types? If not I would be happy to contribute…

I haven’t had time to fully test T4 Toolbox with SQLDB projects. Any issuses you report would be helpful.

I haven’t had anny issues at all with the your first build for VS2012, except having to change TransformationContext.Host to TransformationContext.Current.Host.

Well done!

Rick

Can you provide a sample for T4 template for Code only EF model

Is it possible to make it so, that “Run custom tool” on VS2012 would use 10.0 version of T4 Toolbox?
We have a library that uses T4 Toolbox and this library is used on VS2010 projects. Now we started using VS2012 and getting:
Error 1 Compiling transformation: The type ‘Microsoft.VisualStudio.TextTemplating.TextTransformation’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.VisualStudio.TextTemplating.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

but Microsoft.VisualStudio.TextTemplating.10.0 is included in references.

and we can’t use Microsoft.VisualStudio.TextTemplating.11.0, because they treat DTE, Project, ProjectItem properties new way.

I was able to play with MSBuild and adding

to project file was able to run transformation, (so it should be possible) but then have another problem
MSBuild and EnvDTE.DTE dont play together.

TargetFramework is 4.0

Microsoft.VisualStudio.TextTemplating.10.0 is the T4 implementation assembly that ships with Visual Studio 2010. Microsoft.VisualStudio.TextTemplating.11.0 ships with Visual Studio 2012. I don’t believe what you are trying to do is supported by Visual Studio itself.

Hello,

where I can get a version for Visual Studio 2010?
“The extension manifest is invalid”

regards Chris

You can download the previous version of T4Toolbox for Visual Studio 2010 at http://t4toolbox.codeplex.com

As per your T4ScriptFileGeneratorm, when will this be available?

It seems to be in the source on codeplex, but the source seems outdated? Or am I looking in the wrong place?

In T4Toolbox for Visual Studio 2012, it is called T4Toolbox.ScriptFileGenerator. There is also a new custom tool called T4Toolbox.TemplatedFileGenerator, which allows you to associate a single, reusable T4 file with multiple input files using the “Custom Tool Template” property in the Visual Studio Properties window.

Oleg,
I hit a wall trying to upgrade our application to VS2012. Our application uses a link to sql DBML. I think the missing piece is the XsdProcessor. Do you have any plans to add it back into the
T4Toolbox?

Thank you!
David

Hello.

It seems that we will migrate from T4 to something other, if build-time generation will not be implemented, because we rely on it in our auto-build scripts.
You can’t rely on devs, they can forgot to run custom-tool and build will be broken.

Will you implement build-time support?

David, You can use xsd.exe to generate code produced by the XsdProcessor and include it in your templates manually. As long as the XML schema does not change, such as the LINQ to SQL schema, there is no benefit to using the XsdProcessor.

Anatoly, T4 Toolbox extends design-time code generation functionality of Visual Studio. At some point, I would like to implement build-time support, however, my immediate attention is on other features.

Very good tools. I’like it.

A question about your change for VS2012
OutputItem -> BuildAction is rename to ItemType!

On my code
t.Output.ItemType = ItemType.None;

But my file is generate with BuildCation = Build?

Have you sample?

Thanks.

Mib

In an originally VS2010 solution I adapted an EF 4 template with a lot of T4 Toolbox functionality (for example creating multiple files per entity).
After migration to VS2012 I first did not understand that I had to install a new version of T4 Toolbox and tried to runt the generator directly. This deleted al my entity classes causing the rest of my code to now contain hundreds of compilation errors.
When I try to run the custom tool on the model.tt file I get the following T4-related error: “Error 1 Compiling transformation: The type or namespace name ‘BusinessObjects’ could not be found (are you missing a using directive or an assembly reference?)”
The mentioned namespace exists and is the base namespace for the whole project.
Can anybody help me with this??

Are PreProcessed Templates supported yet? We are switching over to VS2012 soon and we use PreProcessed templates. Also, can I use the old T4Toolbox with VS 2010 and the new T4Toolbox with VS 2012? Will they work side-by-side nicely?

Thanks,
Mike

Do you have T4 Toolbox for VS 2013?

I’m still maintaining a VS2010 project for T4Toolbox as well as a 2012 version… the common code that’s meant to be used in both projects works fine except for the OutputItem change. Is there a plan to back-port this change to the VS2010 version of T4Toolbox?

Hello oleg, I am very confused about Syntax highlighting and Visual Studio Express editions.

Are there any T4 Template Highlighting Tools which work on the Express edition? So far I have tried the Clarius and the Devart ones.

When I follow your instructions to locate the package using the “Extensions and Updates” in Visual Studio 2012 Express the “T4 Toolbox” is not found.

Thanks,
Matthew.

Hi Oleg,

Do you have any news on when we might expect a 2013 studio update for T4Toolbox?

Thanks

Hi, I love the T4 toolbox.

I have a question. When I use a custom tool template for a file in my projects (an .XLSX file in this case) how can I refer to this file in the T4 template that I have assigned as Custom Tool Template?

Thanks for any feedback.