如何更改 T4 模板中的输出文件?
How to Change Output File in T4 Template?
这是我的代码,它工作正常,但它在 Enums.tt 下创建了 .cs 文件,文件名为 Enums.generated.cs,但我想在项目中的特定位置创建一个输出文件我怎样才能做到这一点?
让我知道有什么办法可以做到这一点。我搜索了这个,但没有找到任何合适的解决方案。
<#@ template debug="true" hostSpecific="false" #>
<#@ output extension=".generated.cs" #>
<#@ Assembly Name="System.Data" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string tableName = "ContentTypeMaster";
string columnId = "Id";
string columnName = "Name";
string connectionString = "Data Source=192.168.120.71;Initial Catalog=ErisWebsite;Persist Security Info=True;User ID=IM;Password=Intellimedia$#12;MultipleActiveResultSets=True;Connection Timeout=5000";
#>
using System;
using System.CodeDom.Compiler;
namespace ER.ErisCampaign.Enums
{
/// <summary>
/// <#= tableName #> auto generated enumeration
/// </summary>
public enum <#= tableName #>
{
<#
SqlConnection conn = new SqlConnection(connectionString);
string command = string.Format("select {0}, {1} from {2} order by {0}", columnId, columnName, tableName);
SqlCommand comm = new SqlCommand(command, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
bool loop = reader.Read();
while(loop)
{
#> /// <summary>
/// <#= reader[columnName] #> configuration setting.
/// </summary>
<#= Pascalize(reader[columnName]) #> = <#= reader[columnId] #><# loop = reader.Read(); #><#= loop ? ",\r\n" : string.Empty #>
<#
}
#> }
}
<#+
private string Pascalize(object value)
{
Regex rx = new Regex(@"(?:[^a-zA-Z0-9]*)(?<first>[a-zA-Z0-9])(?<reminder>[a-zA-Z0-9]*)(?:[^a-zA-Z0-9]*)");
return rx.Replace(value.ToString(), m => m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower());
}
#>
在 T4 模板中添加以下方法:
<#
public void SaveFile(string folder, string fileName, string content)
{
using (FileStream fs = new FileStream(Path.Combine(folder, fileName), FileMode.Create))
{
using (StreamWriter str = new StreamWriter(fs))
{
str.WriteLine(content);
str.Flush();
}
}
}
#>
在输出的末尾添加以下行(最后 <# control block #>)以防止写入子节点和写入自定义 directory/file:
<#
SaveFile(folder, filename + ".cs", this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
#>
This 项目包含一些示例。
请注意,它仍然会添加一个空的子节点,但不会在文件中写入任何输出。
这是我的代码,它工作正常,但它在 Enums.tt 下创建了 .cs 文件,文件名为 Enums.generated.cs,但我想在项目中的特定位置创建一个输出文件我怎样才能做到这一点?
让我知道有什么办法可以做到这一点。我搜索了这个,但没有找到任何合适的解决方案。
<#@ template debug="true" hostSpecific="false" #>
<#@ output extension=".generated.cs" #>
<#@ Assembly Name="System.Data" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string tableName = "ContentTypeMaster";
string columnId = "Id";
string columnName = "Name";
string connectionString = "Data Source=192.168.120.71;Initial Catalog=ErisWebsite;Persist Security Info=True;User ID=IM;Password=Intellimedia$#12;MultipleActiveResultSets=True;Connection Timeout=5000";
#>
using System;
using System.CodeDom.Compiler;
namespace ER.ErisCampaign.Enums
{
/// <summary>
/// <#= tableName #> auto generated enumeration
/// </summary>
public enum <#= tableName #>
{
<#
SqlConnection conn = new SqlConnection(connectionString);
string command = string.Format("select {0}, {1} from {2} order by {0}", columnId, columnName, tableName);
SqlCommand comm = new SqlCommand(command, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
bool loop = reader.Read();
while(loop)
{
#> /// <summary>
/// <#= reader[columnName] #> configuration setting.
/// </summary>
<#= Pascalize(reader[columnName]) #> = <#= reader[columnId] #><# loop = reader.Read(); #><#= loop ? ",\r\n" : string.Empty #>
<#
}
#> }
}
<#+
private string Pascalize(object value)
{
Regex rx = new Regex(@"(?:[^a-zA-Z0-9]*)(?<first>[a-zA-Z0-9])(?<reminder>[a-zA-Z0-9]*)(?:[^a-zA-Z0-9]*)");
return rx.Replace(value.ToString(), m => m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower());
}
#>
在 T4 模板中添加以下方法:
<#
public void SaveFile(string folder, string fileName, string content)
{
using (FileStream fs = new FileStream(Path.Combine(folder, fileName), FileMode.Create))
{
using (StreamWriter str = new StreamWriter(fs))
{
str.WriteLine(content);
str.Flush();
}
}
}
#>
在输出的末尾添加以下行(最后 <# control block #>)以防止写入子节点和写入自定义 directory/file:
<#
SaveFile(folder, filename + ".cs", this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
#>
This 项目包含一些示例。
请注意,它仍然会添加一个空的子节点,但不会在文件中写入任何输出。