Vinod Kumar
Vinod Kumar

Reputation: 408

How to pass the parameters to T4 templates using pre build event command line

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.

Upvotes: 5

Views: 1708

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125187

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"

Upvotes: 5

Related Questions