Reputation: 15227
We are using t4 templates for managing configurations in our project. And web.config file is generated via web.tt file. After generation in csproj file I have following:
<Content Include="Web.config">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Web.tt</DependentUpon>
</Content>
Is it possible to configure t4 template somehow to generate independent web.config file, not under web.tt?
Upvotes: 4
Views: 1916
Reputation: 19230
Yes you can. First define a save routine in a T4 include file.
SaveOutput.tt:
<#@ template language=“C#” hostspecific=“true” #>
<#@ import namespace=“System.IO” #>
<#+
void SaveOutput(string fileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, fileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
Now every time you call SaveOutput with a file name it will write the current buffer to that external file. For example.
Web.tt:
<#@ include file=“SaveOutput.tt” #>
<#
GenerateConfig();
SaveOutput(”..\..\Web.Config”);
#>
If you want to generate multiple files:
<#@ include file=“SaveOutput.tt” #>
<#
GenerateFile1();
SaveOutput(”..\..\File1.txt”);
GenerateFile2();
SaveOutput(”..\..\File2.txt”);
#>
Upvotes: 3