I’m working on a set of templates that create a Visual Studio solution and a few projects in that solution. The purpose is to encapsulate the creation of a new VS solution that encompasses a number of features that all my programs should share, which include a resource .dll and satellite assemblies; custom exceptions integrated with MS Enterprise Application Blocks (EAB) exceptions; trace messages using the EAB trace facilities, etc. When the “newSolution” template is executed, it creates the .sln file, a “solutionlevel” assembly info file; projects for the Resources .dll; the initial English, French, and German .resx files; a project for a CommonLib .dll; files for ErrorMessages.cs and TraceMessages.cs, and a MainProgram project and .cs files.
I’ve successfully created the initial templates that create all of these files, and it makes a nice initial solution on which I can add the specific functionality for a new program. But I’d like to be able to incorporate new knowledge into these templates, and regenerate these files for existing programs to add the new functionality to all my programs . I’m looking to better understand the mergestrategies to accomplish this (although I may just go with partial classes if this turn out to be too tough).
The set of templates are organized as “driver” templates, and individual file templates called by the drivers. Drivers can call other driver templates too (the initial “newsolution.cst” creates a .sln file and a <%= MainProgramName %>CommonAssemblyInfo.cs file, and also calls the ResourcesProjectDriver.cst, the CommonLibProjectDriver.cst, and the <%= MainProgramName %>ProjectDriver.cst. Each Driver.cst builds the corresponding .csproj file, and the necessary .cs files for each project). One of the properties set at the top layer driver and propagated to each Driver.cst is a Boolean property called “Update”. When false, an initial set of files is created. When true, the same files should be regenerated while preserving any #region Keep xxx regions.
I’ve tried the following: (Major portions omitted for brevity)
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="True" Description="Creates a solution" %>
<%@ Register Name="SolutionGlobalAssemblyInfoFile" Template="SolutionGlobalAssemblyInfoFile.cst" MergeProperties="False" ExcludeProperties="IncludeMeta" %>
<script runat="template">
public void OutputSolutionGlobalAssemblyInfoFile()
{
SolutionGlobalAssemblyInfoFile solutionGlobalAssemblyInfoFile = this.Create<SolutionGlobalAssemblyInfoFile>();
// copy all properties with matching name and type to the sub-template instance
this.CopyPropertiesTo(solutionGlobalAssemblyInfoFile);
if (Update) {
solutionGlobalAssemblyInfoFile.RenderToFile(this.SolutionRootPath + "/" + this.SolutionName + "GlobalAssemblyInfo.cs", new PreserveRegionsMergeStrategy());
} else {
solutionGlobalAssemblyInfoFile.RenderToFile(this.SolutionRootPath + "/" + this.SolutionName + "GlobalAssemblyInfo.cs",true);
}
}
</script>
<% OutputSolutionGlobalAssemblyInfoFile();%>
When Update is false, the new .cs file gets generated, but when Update is true, the .cs file gets replaced with a 0-byte file. Can anybody shed some light on how a sub-template should be called from a driver template, in such a way that the sub-template will effect the PreserveRegionsMergeStrategy?