T4 create file without DependentUpon attribute in csproj file

We use t4 templates for configuration management in our project. The web.config file is created through the web.tt file. After generating in the csproj file, I have the following:

<Content Include="Web.config"> <AutoGen>True</AutoGen> <DesignTime>True</DesignTime> <DependentUpon>Web.tt</DependentUpon> </Content> 

Is there any way to configure the t4 template to create an independent web.config file, and not under web.tt?

Web.config file depends upon web.tt

+4
source share
1 answer

Yes, you can. First, define the save procedure in a file with T4 enabled.

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 writes the current buffer to this external file. For instance.

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"); #> 
+3
source

All Articles