Sunday, November 7, 2010

Visual T4 beta 1 for VS 2010 is now available!

During the last month we have been receiving a constant flow of daily emails like “where the heck is the VS2010 version???” so we are pleased to announce the public availability of the beta 1 bits which you can download from the Extension Manager dialog or by hand from the Visual Studio Gallery.

Visual T4 for VS 2010 is an almost complete re-write from the VS 2008 version. We wanted to take advantage of the new WPF-based editor and MEF introduced in VS 2010 to offer the best possible experience you can have when editing T4 files.

As a result of this most 3rd party extensions out there in the VS Gallery should be compatible with Visual T4 thus improving your productivity even when writing T4 code.

You can see below the very handy Quick Add Reference in action, working from inside a C# embedded code block:

visualt4-01

Or the Regex Editor, used to edit a regular expression inside a C# code block:

visualt4-02

The beta 1 bits are pretty stable although there are a few nasty issues that we are working on fixing for beta 2 but we didn’t want to hold on releasing the bits as there were LOTS of you interested in early trying them.

PLEASE share your feedback with us: bugs, suggestions, etc., everything is welcomed and we try to reply to every email we receive. You can follow us @visualt4, reach us at visualt4@clariusconsulting.net, or use the comments on this posts, or the QA section in the VS Gallery entry.

Friday, August 20, 2010

Visual T4 for Visual Studio 2010

It’s been a long time since our last post. I just wanted to let you know about the amazing progress we’ve been making on Visual T4 for VS2010. What’s new in short? A major overhaul to take full advantage of all the benefits of the brand new WPF-based core text editor as well as the new language services to provide the absolutely unique and long-time praised Intellisense integration.

We’ll go into the details on another post, but here’s a bunch of screenshots to get you excited, using the ADO.NET C# POCO Entity Generator templates.

Colors honor your favorite C# editor settings:

image
image

Tooltips as usual in C#:

image

Intellisense on APIs as usual in C#:

image

Parameter info as usual in C# code :)

image

Pretty good indenting!

image

Full support for VERY complex includes! (the EF one has 2200+ lines)

image

Full T4 directives intellisense with tooltips, as you’d expect:

image

Can add arbitrary assemblies an get their intellisense too:

image

Can build any LINQ query you fancy with full intellisense:

image

Use your favorite code snippets as usual in C#:

image

With even full anonymous types support:

image

And Intellisense of course:

image

A BIG kudos to the Visual T4 team for such an amazing integration into VS2010. We’ll be sharing the bits more broadly very soon. We’re beta testing it at this very moment.

Monday, June 29, 2009

Buy Visual T4 Pro, Get Free Pro ASP.NET MVC Book!

Yes, the best T4 Editor meets the best ASP.NET MVC book.

AspNetMvcFreeBook

If you’re into ASP.NET MVC and want to play and customize the MVC T4 templates with wonderful support getting full Preview, IntelliSense, syntax coloring and more for both T4 and C# embedded code this deal is for you!

Buy here the Visual T4 Editor Professional and you will get the book for free.

This promotion is only valid till July, 3rd, 2009  or until we ran out of our 100 books stock, whatever comes first.

As we know no one wants to wait a week for a book we will ship it using a 2nd-day service to get you the book really quick.

Please note we can only ship to an US address, sorry.

Monday, April 20, 2009

Visual T4 in the news!

Our Visual T4 products continue to be mentioned by pretty much everyone out there working with T4. You already know our tools were used in MIX09 (besides other major conferences in the past) and that pretty big teams at Microsoft recommend it.

We did google a bit and these are the most “remarkable” mentions we’re getting (it’s just great to see we have built the “standard” tool for editing T4 files!):

Scott Hanlseman (Principal Program Manager, Microsoft) on T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret:

"if you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring. They have a pay version that gets you more if you want."

 Oleg Sych, uses Visual T4 Editor in his must-read series of T4 articles mentions:

“…Instead, the approach used by Clarius in their implementation of this directive included with T4 Editor relies on CallContext, is a lot more robust and elegant…”

“…Text editing and debugging support for T4 templates is currently rather limited. T4 Editor (a free download) from Clarius Consulting provides IntelliSense and syntax highlighting in Visual Studio text editor for .tt files…”

Miguel de Icaza, on Mono's Text Template Transformation Toolkit (T4):

“To my surprise T4 thing is wildly used by lots of people. Daniel Cazzulino's company has a product just to improve Visual Studio's support for editing .tt files.”

Redmond Developer News , on Code Generation Made Easy :

"There are a lot of tools out there that do code generation," Conery says. "Great tools like CodeSmith and Clarius Consulting's T4 Editor”

Gareth Jones (Visual Studio team member, Microsoft) , on Clarius take T4 editing to the next level :

“…I see that Clarius have now got to an alpha stage for the next stage of their T4 editing toolset.

As well as their Community Edition and Pro Edition, they're now going to offer a full-featured code generation environment they're calling Visual T4 Code Generator, including tight integration with server explorer, database tables and XML as well as a multi-file generation implementation out of the box and lots more beside…”

Pedro Silva (Visual Studio team member, Microsoft) , on T4 Editor Beta Released:

“The folks at Clarius have an update to their T4 template editor. They're now in Beta. It's a very useful editor for anyone doing large amounts of T4 template editing with good factoring of template code vs script and color coding of keywords.”

Kathleen Dollard (Microsoft MVP) , on What’s Wrong with T4?:

“There’s good news here. Clarius provides a colorizing editor as a community edition to get you started. If you want fancier features, their main product is just $100”

Hanselminutes on Code Generation

In case you missed this (as we did) Scott interviews Kathleen Dollard (Microsoft MVP) on code generation in .Net and Visual Studio.

It’s an interesting chat on the past and current state of code gen, with emphasis on T4. You can listen to the podcast here.

Make sure you don’t miss the audio @ 19m:43s where Clarius Visual T4 Editor is recommended if you are into serious editing of T4 templates :)

Thursday, March 26, 2009

The process of building a Code Generation template

More often than not, you’ll have several related classes and you’ll want to turn them into generated code. There is a process I like to use to make a generation template that’s based on this idea. You can also use the process even if you don’t have the classes, by building them manually.

First, you’ll need a class. For example, we’re going to use generation to create a strongly type representation of a directory structure and files on disk. In the example, these files will be configuration files spread throughout a few directories, but we’ll open them as simple strings to make the sample easier. Let’s look at the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MyCompany.MyProduct
{
class Configuration
{
private string baseDir = "Configuration";

public Configuration(string baseDir)
{
this.baseDir = string.Concat(baseDir, "\\Configuration");
}

public Network Network
{
get
{
return new Network(baseDir);
}
}
}

class Network
{
private string baseDir;

public Network(string baseDir)
{
this.baseDir = string.Concat(baseDir, "\\Network");
}

public string ProxiesConfig
{
get
{
return File.ReadAllText(string.Concat(baseDir, "\\Proxies.Config");
}
set
{
File.WriteAllText(string.Concat(baseDir, "\\Proxies.Config"), value);
}
}

public string HostsConfig
{
get
{
return File.ReadAllText(string.Concat(baseDir, "\\Hosts.Config");
}
set
{
File.WriteAllText(string.Concat(baseDir, "\\Hosts.Config"), value);
}
}
}

class Security
{
private string baseDir;

public Security(string baseDir)
{
this.baseDir = string.Concat(baseDir, "\\Security");
}

public string RolesConfig
{
get
{
return File.ReadAllText(string.Concat(baseDir, "\\Roles.Config");
}
set
{
File.WriteAllText(string.Concat(baseDir, "\\Roles.Config"), value);
}
}
}
}


Notice that this code already has a “generated” look and feel. The idea is to create a Configuration object with the base path for configuration, and to go down from there to all the configuration items. Network and Security will be directories within Configuration, and the other properties will refer to files in the directories. Notice also that at this point it’s not very important to have complete classes. A rough layout is enough.



The next step is to find varying parts. This is really simple: variables will generally be present as constants or as variable properties. Constants is far more common. In this step, we’ll replace the constants with representative code blocks:



    class Configuration
{
private string baseDir = <#= baseDirectoryName #>;

public Configuration(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryPath #>”);
}

public Network Network
{
get
{
return new Network(baseDir);
}
}
}

class Network
{
private string baseDir;

public Network(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryPath #>”);
}

public string ProxiesConfig
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}

public string HostsConfig
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}
}


Then we’ll also replace the varying property names (and class and method names if necessary) with code blocks too:



    class <#= baseDirectoryName #>
{
private string baseDir = <#= baseDirectoryName #>;

public Configuration(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryPath #>”);
}

public <#= childDirectoryName #> <#= childDirectoryName #>
{
get
{
return new <#= childDirectoryName #>(baseDir);
}
}
}

class <#= baseDirectoryName #>
{
private string baseDir;

public <#= baseDirectoryName #>(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryName #>”);
}

public string <#= configFileName #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}

public string <#= configFileName #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}
}


At this point, you should have some duplication. The next step is to remove that duplication by looping through duplicated constructs:



<#
for (int classes = 0; classes < n; classes++)
{
#>
class <#= baseDirectoryName #>
{
private string baseDir;

public <#= baseDirectoryName #>(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryName #>”);
}

<#
for (int props = 0; props < n; props++)
{
#>
public string <#= configFileName #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}
<#
}
#>
}
<#
}
#>


You should also add whatever’s left behind (in this case, the “directory” properties:



<#
for (int classes = 0; classes < n; classes++)
{
#>
class <#= baseDirectoryName #>
{
private string baseDir;

public <#= baseDirectoryName #>(string baseDir)
{
this.baseDir = string.Concat(baseDir, “<#= baseDirectoryName #>”);
}

<#
for (int childClasses = 0; childClasses < n; childClasses++)
{
#>
public <#= childDirectoryName #> <#= childDirectoryName #>
{
get
{
return new <#= childDirectoryName #>(baseDir);
}
}
<#
}
#>
<#
for (int props = 0; props < n; props++)
{
#>
public string <#= configFileName #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, “<#= configFileName #>”);
}
set
{
File.WriteAllText(string.Concat(baseDir, “<#= configFileName #>”), value);
}
}
<#
}
#>
}
<#
}
#>


At this point, you should have a pretty good skeleton. The point now is to make it work:



<#
foreach(string directory in Directory.GetDirectories("myconfigurationdir"))
{
#>
class <#= directory #>
{
private string baseDir;

public <#= directory #>(string baseDir)
{
this.baseDir = string.Concat(baseDir, "<#= directory #>");
}

<#
foreach (string childDirectory in Directory.GetDirectories(directory))
{
#>
public Network <#= childDirectory #>
{
get
{
return new <#= childDirectory #>(baseDir);
}
}
<#
}
#>
<#
foreach (string file in Directory.GetFiles(directory))
{
#>
public string <#= file #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, "<#= file #>");
}
set
{
File.WriteAllText(string.Concat(baseDir, "<#= file #>"), value);
}
}
<#
}
#>
}
<#
}
#>


At this point you should be added any imports that are needed for the generation code to work (System.IO in this case). You may also need to separate processes in methods, as to make them recursive. For instance, our classes need to be generated for each directory in the hierarchy. Finally, you must solve generation-specific problems. For instance, this generation will probably have many invalid class and property names (“Proxies.Config” is not a valid class name). You can change that by using the CleanName procedure that we already reviewed in this blog. The result will look something like this:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

<#
GenerateClass("myconfigdir");
#>
<#+
void GenerateClass(string directoryName)
{
foreach(string directory in Directory.GetDirectories(directoryName))
{
#>
class <#= CleanName(directory) #>
{
private string baseDir;

public <#= CleanName(directory) #>(string baseDir)
{
this.baseDir = string.Concat(baseDir, "<#= directory #>");
}

<#+
foreach (string childDirectory in Directory.GetDirectories(directory))
{
#>
public <#= CleanName(childDirectory) #> <#= CleanName(childDirectory) #>
{
get
{
return new <#= CleanName(childDirectory) #>(baseDir);
}
}
<#+
}
#>
<#+
foreach (string file in Directory.GetFiles(directory))
{
#>
public string <#= CleanName(file) #>
{
get
{
return File.ReadAllText(string.Concat(baseDir, "<#= file #>");
}
set
{
File.WriteAllText(string.Concat(baseDir, "<#= file #>"), value);
}
}
<#+
}
#>
}
<#+
GenerateClass(directory);

}
}


string CleanName(string)

{

//some code cleaning here

}
#>


The final step is to separate the code into modules. For instance, you can put the CleanName procedure in an include T4 file to be used as a helper, put the header in a “main” T4 and the methods in generation T4 files, separate the properties into its own include, or whatever feels good to you.



--

Written by Joaquin, joj AT clariusconsulting DOT net. Disclaimer: opinions expressed in this post are my own and not necessarily reflect those of Clarius Consulting.

Monday, March 23, 2009

Visual T4 @ MIX09

We’ve been catching up on web techs watching MIX09 videos and guess what…?

Scott Hanselman in his File|New -> Company: Creating NerdDinner.com with Microsoft ASP.NET Model View Controller (MVC) and Phil Haack in his Microsoft ASP.NET Model View Controller (MVC): Ninja on Fire Black Belt Tips talks both mention the Clarius Visual T4 Editor for editing T4 templates!

Here is a screenshot of Phil, on stage, using the Visual T4 Editor Professional edition:

VisualT4Mix09

Good to see Visual T4 being used everywhere!!