T4 TemplateFileManager 输出到项目文件夹

T4 TemplateFileManager output to projectfolder

我对 C# 和 T4 完全陌生。但作为一名数据专家,我想生成一些东西。我创建了一个生成多个文件的工作解决方案。到目前为止,我很自豪 :)

但现在我想启用生成文件到某些项目文件夹。我已经尝试了一些基于 Google 的东西,但似乎他们都使用了 special/other T4 解决方案。所以我的问题是有没有一种方法可以(相对容易地)将多个文件生成到单独的文件夹中(可以这么说;'Stored Procedures' 中有 2 个文件,'Tables' 中有 2 个不同的文件)?

我现在的是基于https://github.com/renegadexx/T4.Helper/blob/master/Source/TemplateFilemanager.CS.ttinclude

我制作的是这样的:

<#@ include file="TemplateFileManagerV2.1.ttinclude" #>
    
<#

    string connectionString = "......."; 
    SqlConnection conn = new SqlConnection(connectionString); 
    string selectQueryTables = "MyQueryForTables"; 
    SqlDataAdapter commandTables = new SqlDataAdapter(selectQueryTables,conn); 

    DataSet TablestoGenerate = new DataSet();
    commandTables.Fill(TablestoGenerate, "Tables");
    var manager = TemplateFileManager.Create(this);

    foreach (DataRow tRow in TablestoGenerate.Tables["Tables"].Rows)  
    {  
            string FileName = tRow["TableType"].ToString().Trim(' ')+'.'+tRow["TableName"].ToString().Trim(' ')+".sql";
            manager.StartNewFile(FileName);
#>
            CREATE TABLE [<#= tRow["TableType"].ToString().Trim(' ') #>].[<#= tRow["TableName"].ToString().Trim(' ')#>] (
            <#
                string selectQueryColumns = "MyQueryForColumns";
                SqlDataAdapter commandColumns = new SqlDataAdapter(selectQueryColumns,conn); 
                DataSet ColumnstoGenerate = new DataSet();    
                commandColumns.Fill(ColumnstoGenerate, "Columns");

                foreach (DataRow cRow in ColumnstoGenerate.Tables["Columns"].Rows)
                {
            #>
                    , [<#= cRow["ColumnName"].ToString().Trim(' ') #>] [<#= cRow["DataType"].ToString().Trim(' ') #>]<# if(cRow["precision"] != DBNull.Value) { #>(<#= cRow["precision"].ToString().Trim(' ') #>)<# } #>

            <#
                }
            #>
            )
        GO
    <#
    }   

    manager.Process();

#>

您可以使用 EnvDTE.ProjectItems.AddFromFile() 方法将现有文件添加到 VS 项目 - 接口文档 here.

我扩展了 Damian Guard here 创建的很棒的工具,他在其中从单个 T4 模板创建了多个文件,还允许选择性地指定将这些文件放入文件夹。代码相当简单,不依赖外部 tools/packages.

这会在每个单独的文件夹中为您提供一个单独的文件

tt 文件看起来像这样

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ output extension="/" #>
<#@ template language="C#v3.5" hostspecific="True"#>
<#@ include file="Manager.ttinclude"#>

<#
    var manager = new Manager(Host, GenerationEnvironment, true, "T4CustomFileGeneration")
    {
        OutputPath = Path.GetDirectoryName(Host.TemplateFile),
    };
#>

<# manager.StartBlock("FooStoredProc.sql", "Stored Procedures"); #>
CREATE PROC FOO ...
<# manager.EndBlock(); #>

<# manager.StartBlock("BarTable.sql", "Tables"); #>
SELECT * FROM Bar ...
<# manager.EndBlock(); #>

<# manager.Process(); #>

主要逻辑在 ttinclude 中,为简洁起见,我已将其链接到下面。但是你不需要触及 ttinclude 中的任何内容,只需在 tt 文件中指定你想要的文件、文件夹和 SQL 逻辑。

Working Demo Here