5

I need to pass the parameter from my prebuild event to my T4 template. Currently i am using following Pre-build command to build my T4 template file but i am not able to pass parameter to it.

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\texttransform.exe" "$(ProjectDir)VersionGenerator.tt"

I need to pass the parameter to my VersionGenerator.tt template.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Vinod Kumar
  • 408
  • 4
  • 18

1 Answers1

4

Using TextTransform.exe there's a command-line switch for passing parameters to t4 templates: -a which accept parameters in the following format:

-a [processorName]![directiveName]!<parameterName>!<parameterValue>

When using, consider:

  • You can resolve the parameter by Host.ResolveParameterValue.
  • processorName and directiveName are optional and you can omit them.
  • You always need type the '!' marks, even if you omit the optional processor and directive names.
  • For each parameter that you want to pass, you need one -a switch.
  • You can use "" around switch value.
  • For more information about Take a look at Generate files with the TextTransform utility.

Example

Assuming you have the following template Template1.tt:

<#@ template hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<# string param1 = this.Host.ResolveParameterValue("", "", "param1"); #>   
param1 : <#= param1 #>

The following command:

texttransform.exe "Template1.tt" -a "!!param1!value1" 

Results in:

param1 : value1

What should be Pre-build event command line?

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\texttransform.exe" "$(ProjectDir)Template1.tt" -a "!!param1!value1"

Want more parameters?

Just define them in template and pass them usinganother -a switch:

texttransform.exe "Template1.tt" -a "!!param1!value1" -a "!!param2!value2"
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This is off-topic and is a different solution, but you can consider this solution for running t4 templates at build time: [Invoke text transformation in the build process](https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2019) – Reza Aghaei Feb 12 '20 at 14:34
  • Whether the argument name passed in the `this.Host.ResolveParameterValue("", "", "Foo")` is case sensitive or not. Could you please explain. – Venkat Feb 13 '20 at 05:34
  • 1
    @Venkat At leas in my test it showed it's not case-sensitive. – Reza Aghaei Feb 13 '20 at 05:45
  • @Vinod Kumar since you are the original poster, it would be great if you can verify the answer :) – Reza Aghaei Feb 15 '20 at 12:52
  • I am using `try { changeset = this.Host.ResolveParameterValue("-", "-", "changeset"); ` and this parameter is the second one, not the only one, I pass the 2 parameters like this and only the second one is empty...whereas the first parameter is totally fine. Passing both: `-a "!!buildConfiguration!$(ConfigurationName)" -a "!!changeset!asdasd"` – iBobb Mar 05 '21 at 15:23