Reputation: 39
I want to run the t4 TextTransForm.exe utility on my build server on the command line. I'm aware that the DTE object etc are not available on the command line. But executing a simple transform on a template which expects a parameter also suggests the parameter directive is not working on the command line.
C:\util>"C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\12.0\TextTransform.exe" test.tt -a !!MyParameter!test error : Errors were generated when initializing the transformation object. The transformation will not be run. The following Exception was thrown: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.VisualStudio.TextTemplating9edb37733d3e4e5f96a377656fe05b5c.GeneratedTextTransformation.Initialize() at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0) at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()
This is my test.tt template:
<#@ template language="C#" hostspecific="true" #>
<#@ parameter name="MyParameter" type="System.String" #>
Parameter in expression block: <#= MyParameter #>
Parameter in statement block: <# Write(MyParameter); #>
Looking at discussions like Get argument value from TextTransform.exe into the template gave me the impression it would also work on the command line without a specific host or Visual studio installed.
So am I doing something wrong, or will this just not work on the command line?
Upvotes: 2
Views: 1759
Reputation: 875
hkstr's answer is almost correct, but the "Host.ResolveParameterValue()" call fails when used inside Visual Studio. The answer is to wrap the call in try...catch or to check for DTE. In my case the DTE had the information I wanted:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#
string activeConfiguration = null;
// The IServiceProvider is available in VS but isn't available on the command line.
IServiceProvider serviceProvider = Host as IServiceProvider;
if (serviceProvider != null)
{
DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));
activeConfiguration = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
}
else
{
activeConfiguration = this.Host.ResolveParameterValue("", "", "ActiveConfiguration");
}
Console.WriteLine("ActiveConfiguration is: " + activeConfiguration);
#>
Upvotes: 0
Reputation: 39
The parameter directive was the line which caused the error. Just remove it and your can read the value of the parameter with: this.Host.ResolveParameterValue("","","MyParameter");
So the working .tt looked like this:
<#@ template language="C#" hostspecific="true" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
Parameter value is
<#
var MyParameter= this.Host.ResolveParameterValue("","","MyParameter");
Console.WriteLine("MyParameter is: " + MyParameter);
#>
Upvotes: 1