Search code examples
code-generationcodesmith

codesmith generator merge in an active snippet


In developing an active gen scenario, how can we eliminate/automate the .csp portion of a merge and execute it in an active snippet/code?

The sample merge details always have a manual element that I want to eliminate. I have read the following and some older stuff that is relevant but not answering the question.

https://codesmith.atlassian.net/wiki/spaces/Generator/pages/528957/Merge+Strategies

https://codesmith.atlassian.net/wiki/spaces/Generator/pages/528968/Using+Merge+Strategies+to+Enable+Active+Generation


Solution

  • I work on CodeSmith Generator. You can accomplish this by passing the strategy to RenderToFile like this

    <%@ CodeTemplate Language="C#" %>
    <%@ Register Name="PreserveRegionsSample" Template="PreserveRegionsSample.cst"  %>
    <% 
    var strategy = new CodeSmith.Engine.PreserveRegionsMergeStrategy("^[ \\t]*[ \\s]*\"?(?i:Custom)\"?");
    
    var template = new PreserveRegionsSample();
    template.RenderToFile("PreserveRegionsSample.cs", strategy); 
    %>