将T4生成的文件包括在模板文件的同一文件夹中

时间:2019-09-03 15:12:01

标签: c# visual-studio t4

我正在尝试将保存的文件添加到T4模板文件的当前项目中。 SaveOutput方法按预期方式保存了文件,但是当我使用IncludeInCurrentProject方法时,该方法调用了ProjectItems.AddFromFiledocs here),然后将文件添加到项目的

我想将SaveOutput方法生成的文件包含在项目的确切位置上。

这就是我现在拥有的:

enter image description here

这是我的.tt文件:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".sql" #>
<#@ assembly name="Microsoft.VisualBasic.dll" #>
<#@ assembly name="EnvDTE" #>
<#@ import Namespace="System.IO" #>
<#@ import namespace="EnvDTE" #>

<#
    var MigrationName = Microsoft.VisualBasic.Interaction.InputBox("InputBox Label", "Description");
    if (string.IsNullOrWhiteSpace(MigrationName))
        return "";

    var MigrationVersion = System.DateTime.Now.ToString("yyyyMMddHHmm");
    var MigrationFileName = MigrationVersion + "-" + MigrationName + ".sql";
    var MigrationFilePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "", MigrationFileName));
#>

My T4 template goes here

<#
SaveOutput(MigrationFilePath);
IncludeInCurrentProject(MigrationFilePath)
    .Open(Constants.vsViewKindPrimary)
    .Activate();
#>
<#+
    private void SaveOutput(string filename)
    {
        File.WriteAllText(filename, GenerationEnvironment.ToString());
        GenerationEnvironment.Clear();
    }

    private ProjectItem IncludeInCurrentProject(string filename)
    {
        return CurrentProject.ProjectItems.AddFromFile(filename);
    }

    private Project CurrentProject
    {
        get 
        {
            if (_currentProject == null) {
                var serviceProvider = (IServiceProvider)this.Host;
                var dte = serviceProvider.GetService(typeof(DTE)) as DTE;

                foreach(Project project in dte.Solution.Projects) 
                {
                    // workaround. dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject didn't work
                     string projectPath = project.FullName.Replace("\\" + project.FileName, string.Empty);
                     if(Host.TemplateFile.Contains(projectPath))
                     {
                        _currentProject = project;
                        break;
                     }
                }
            }
            return _currentProject;
        }
    }


    private Project _currentProject;
#>

如何将生成的文件包含在T4模板文件的同一文件夹中?

2 个答案:

答案 0 :(得分:0)

当您调用IncludeInCurrentProject时,输入应为路径+文件名。

答案 1 :(得分:0)

CurrentProject.ProjectItems始终在根目录中引用ProjectItems

我使用文件路径浏览了ProjectItems,并在正确的路径上调用了AddFromFile。您可以检查GetCurrentProjectItem方法,此行为已在此处实现。

现在模板可以正常工作了:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".sql" #>
<#@ assembly name="Microsoft.VisualBasic.dll" #>
<#@ assembly name="EnvDTE" #>
<#@ import Namespace="System.IO" #>
<#@ import Namespace="System.Text.RegularExpressions" #>
<#@ import namespace="EnvDTE" #>

<#
    var migrationName = Microsoft.VisualBasic.Interaction.InputBox("InputBox Label", "Description");

    Regex alphaNumericRegex = new Regex("[^a-zA-Z0-9-_]");
    migrationName = alphaNumericRegex.Replace(migrationName, string.Empty);

    if (string.IsNullOrWhiteSpace(migrationName))
        return "";

    var migrationVersion = System.DateTime.Now.ToString("yyyyMMddHHmm");
    var migrationFileName = migrationVersion + "-InsertCourt" + migrationName + ".sql";
    var migrationFilePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), migrationFileName));
#>

My T4 template goes here

<#
SaveOutput(migrationFilePath);
IncludeInCurrentProject(migrationFilePath)
    .Open(Constants.vsViewKindPrimary)
    .Activate();
#>
<#+
    private void SaveOutput(string filename)
    {
        File.WriteAllText(filename, GenerationEnvironment.ToString());
        GenerationEnvironment.Clear();
    }

    private ProjectItem IncludeInCurrentProject(string filename)
    {
        ProjectItem item = GetCurrentProjectItem().ProjectItems.AddFromFile(filename);
        item.Properties.Item("BuildAction").Value = "None";
        return item;
    }

    private ProjectItem GetCurrentProjectItem()
    {

        var serviceProvider = (IServiceProvider)this.Host;
        var dte = serviceProvider.GetService(typeof(DTE)) as DTE;

        string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
        string foldersString = templateDirectory.Replace(Path.GetDirectoryName(GetCurrentProject().FullName), string.Empty);
        string[] foldersArray = foldersString.Split(new [] {"\\"}, StringSplitOptions.RemoveEmptyEntries);

        foreach(Project project in dte.Solution.Projects) 
        {
            ProjectItems currentProjectItems = null;

            foreach(string folder in foldersArray)
            {
                if(currentProjectItems == null) 
                {
                    currentProjectItems = project.ProjectItems;
                }

                foreach(ProjectItem projectItem in currentProjectItems) 
                {
                    if(projectItem.Name == folder) 
                    {
                        if(Array.IndexOf(foldersArray, folder) == foldersArray.Length - 1)
                        {
                            return projectItem;
                        }
                        currentProjectItems = projectItem.ProjectItems;
                        break;
                    }
                }
            }
        }
        return null;
    }

    private Project GetCurrentProject()
    {
        var serviceProvider = (IServiceProvider)this.Host;
        var dte = serviceProvider.GetService(typeof(DTE)) as DTE;

        foreach(Project project in dte.Solution.Projects) 
        {
            // workaround. dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject didn't work
            string directory = Path.GetDirectoryName(project.FullName);
            if(Host.TemplateFile.Contains(directory))
            {
                return project;
            }
        }
        return null;
    }


    private ProjectItem _currentProjectItem;
#>