2

Here is my Code and it's working fine but its create .cs file under Enums.tt and the file name is Enums.generated.cs but I want to create an output file at my specific location in the project how can I achieve this?

Let me know is there any way to do this stuff. I searched for this but I didn't find any proper solution.

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


#>
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
Jasmin Solanki
  • 369
  • 7
  • 26
  • Possible duplicate of [Custom Output File Path For T4 Template](https://stackoverflow.com/questions/21352411/custom-output-file-path-for-t4-template) – Sinatr May 10 '19 at 06:58

1 Answers1

5

Add following method to the T4 template:

<#
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();
        }
    }
}
#>

Add following lines at the end of your output (last <# control block #>) to prevent writing to child node and write to custom directory/file:

<#
SaveFile(folder, filename + ".cs", this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
#>

This project holds some examples.

Please note that it will still add a empty child node, but no output is written inside the file.

Tim Maes
  • 473
  • 3
  • 15