2

我想在命令行上的构建服务器上运行t4 TextTransForm.exe实用程序。我知道DTE对象等在命令行上不可用。 但是,对需要参数的模板执行简单的转换也表明参数指令在命令行上不起作用。T4 texttransform在命令行上传递参数可能吗?

C:\ UTIL> “C:\ Program Files文件(x86)的\ Common Files文件\ Microsoft共享\ TextTemplating \ 12.0 \ TextTransform.exe” test.tt -a!MyParameter测试 错误:错误是在初始化变换对象时生成。转换不会运行。引发以下异常: System.NullReferenceException:未将对象引用设置为对象的实例。 在Microsoft.VisualStudio.TextTemplating9edb37733d3e4e5f96a377656fe05b5c.GeneratedTextTransformation.Initialize() 在CallSite.Target(封闭,调用点,对象) 在System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1 [T0](调用点站点,T0为arg0) 在Microsoft.VisualStudio。 TextTemplating.TransformationRunner.PerformTransformation()

这是我test.tt模板:

<#@ template language="C#" hostspecific="true" #> 
<#@ parameter name="MyParameter" type="System.String" #> 
Parameter in expression block: <#= MyParameter #> 
Parameter in statement block: <# Write(MyParameter); #> 

看着像讨论 Get argument value from TextTransform.exe into the template给我留下深刻的印象它也可以在没有安装特定主机或Visual Studio的命令行上运行。

所以我做错了什么,或者这只是不能在命令行上工作?

+0

据当时https://msdn.microsoft.com/en-us/library/bb126245.aspx你已经正确创建的模板的文档。 –

回答

1

参数指令是导致错误的行。只要删除它,你可以读取参数的值: this.Host.ResolveParameterValue(“”,“”,“MyParameter”);

所以工作.TT是这样的:

<#@ template language="C#" hostspecific="true" #> 
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #> 


Parameter value is 
<# 
var MyParameter= this.Host.ResolveParameterValue("","","MyParameter"); 
Console.WriteLine("MyParameter is: " + MyParameter); 
#> 
+0

在命令行转换过程中,visual Studio主机不可用 –

0

hkstr的答案几乎是正确的,但在Visual Studio中使用时, “Host.ResolveParameterValue()” 呼叫失败。答案是在try ... catch中包装呼叫或检查DTE。在我的情况下,DTE有我想要的信息:

<#@ 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); 
#>