t4 templates per template TransformOnBuild setting - visual-studio-2010

I would like to automatically execute one of the .tt files when my project is built, but not the others. Is it possible to set the TransformOnBuild property on specific .tt files instead of project wide? Or another method to accomplish this?

I have decided to add a custom pre build step to generate the .tt files needed:
<Exec Command=""%PROGRAMFILES(x86)%\Common Files\microsoft shared\TextTemplating\10.0\TextTransform.exe" template.tt -out template.cs" />
This method also allowed me to easily pass parameters (defined in the project file) to the template with -a; another problem I was facing.

Answering as another method to accomplish this.
You could change your .tt as TextTemplatingFilePreprocessor
(descibed better here:
http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/).
Create small command line tool that executes the preprocessor on pre-build event.
The first link includes this example, but it describes the use of preprocessed class:
http://blogs.clariusconsulting.net/pga/vs10-beta-1-t4-preprocessing-part-2/

Related

There's a way to know which project is executing installer on wix?

I have two projects that shares a same .wxi file, but now, I need different actions foreach project.
There's a way to know which project is executing installer and change the installer actions on wxi file?
For example,
One project needs shortcut and the second no needs.
You can use WiX preprocessor for doing this: just set a variable, either on the command line or in the main project file before include, and use conditional statements to add or to drop some elements.

msbuild script utilizing solution information

I have a csproj file, being part of two different Visual Studio solutions. The project file should be able to behave slightly different, depending on the solution it will be used from. What I would need, is something usable as a 'Condition' - a property named for example $(SolutionName) - filled in automagically.
At least, this is my idea. I didn't found anything like that.
I also considered to have two small project files importing the common parts. This would prevent editing all these properties from inside Visual Studio, I guess. It would write changes only in the active 'master file', correct?
So, is there any other way to discriminate at project level using solution information?
Turns out there is a property named exactly $(SolutionName). Try this; first set an environment variable as:
> set MSBuildEmitSolution=1
Then build your solution file using MSBuild from the same command line
> MSBuild My.sln
You will find the MSBuild project transformation of your solution file, it will be named My.sln.metaproj.
Just open that in a text editor and you can see the other properties. Examine the "Build" target in this projectd file, you can see that all these properties are passed in to the MSBuild task when it builds your projects, so you should be able to discriminate conditions based on any of them.

Output Path for Shared T4 Template

How can I specify the output path of a T4 template?
When I add my T4 template using "Add as Link" to my project from $TemplatePath$, it generates the output file in $TemplatePath$, not $ProjectPath$. How can I specify that $ProjectPath is where the generated file is placed?
I got around this by having a master template in one location and then templates that imported that one (but did little else) in each project. Of course, that's not the nicest solution.
We typically use the same system that Jeff Yates mentioned with minimal (often single line) stub templates including the shared template.
You could also use Oleg Sych's T4Toolbox to project output to any directory you please.
However, the standard IVsSingleFileGenerator that T4's custom tool is built upon doesn't have any dials to turn to control it's output location and it's behavior in the case of linked files is as you describe.

Run a Visual Studio custom tool at build time

I have a script that will convert a text file into a resource file, so that I can have multiple language support by having text files for different languages converted into different resources. Once I run the script to get the resx file, I then have to run a custom build tool (as described here:Link to Code Project) in order to create the Designer.cs files to use the new files.
In keeping with the philosophy that I should be able to build the entire project with a single button click, how can I remove the step where I have to explicitly call the custom build tool in order to make the codebehind files?
I've tried automatically deleting the Designer.cs files as a pre-build step, since I would think that the custom build tool would automatically run if there were no Designer.cs files, but no dice.
So, I want my build script in Visual Studio/msbuild to do:
1) convert text to resx (done)
2) move resx files to appropriate directory (done)
3) create designer.cs files using a custom build tool (not done)
It's that last step...
Unfortunately I don't think there's an easy way of doing this. Custom Build Tools only run from within VS.NET - they don't run when you build your project using MSBuild from the command line.
In general, if you have the choice of writing a build tool as a Customer Build Tool or an MSBuild Task then choose the MSBuild Task every time as these Tasks will run in VS.NET and from the command line.
The designer.cs files for resources are there to support your coding. They give you the strongly typed access into the resource file. So, as long as you create your culture invariant resources in VS.NET (and let it create the .designer.cs files) then adding additional language support later (additional .resx files) should work fine.
If, however, your text files are your primary resource files, and you're adding new resource strings into these text files first, then you'll need to find another way of generating .cs files that allow you to code against those resources. If you HAD to, generating the .designer.cs file yourself (or somethign similar) wouldn't be that difficult. Using CodeDom or T4 you could create the helper class, using an existing .designer.cs file as a template.
There is a way to generate the *.Designer.cs for *.resx as part of the build process. There is a built-in MSBuild task GenerateResource, which basically a wrapper around the SDK tool resgen.exe. Here you can find an example how to use it.
Another thing you would find useful, if the generated *.Designer.cs version is not correct, basically, GenerateResource task is not calling the desired version of resgen.exe, setting property SdkToolsPath might help you.
Have you tried adding a Exec step in Before/AfterBuild step in your csproj? You have to manually edit the file for this, but that should solve your problem.
I'm not fully clear on if you want this done before or after the build. If you need it done sometime after Pass1/Pass2, you can hook into the targets directly. Try looking into Microsoft.Build.Common.Targets to get a feel for how to do this.

How to use T4 and Visual Studio to Extend a Partial Class

I wanted to use T4 to generate properties for a partial class. I'm running into a configuration problem where my .TT file is in the same project as the class file I want to extend. So if I have to include the assembly in the .TT file it get's locked. I tried to do a command line in the pre-build but the problem there is that VS always wants to recompile the .TT file with the project.
The only solution I can think of is to rename the .tt files to say .t4 and then use a pre-build command with TextTransform -out to create the .cs file in the project directory.
Can anyone think of a cleaner way to do this?
Assuming that locking is caused by your template using Reflection to read metadata of the partial class you need to extend, you could solve the locking problem if you use CodeModel. This API is provided by Visual Studio and allows you to get the metadata directly from the source file, without the need to compile the partial class or load the compiled DLL. Here is an example of a T4 code generator that uses this approach: http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration

Resources