【发布时间】:2019-09-03 15:12:01
【问题描述】:
我正在尝试将保存的文件添加到我的 T4 模板文件的当前项目中。
SaveOutput方法按预期保存文件,但是当我使用IncludeInCurrentProject方法时,调用ProjectItems.AddFromFile(docs here),然后将文件添加到root 项目。
我想将SaveOutput 方法生成的文件包含在项目的确切位置上。
这就是我现在拥有的:
这是我的 .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 模板文件的同一文件夹中?
【问题讨论】:
标签: c# visual-studio t4