CodeSmith Community
Your Code. Your Way. Faster!

PreserveRegionsMergeStrategy Problem

Latest post 01-02-2007 5:07 PM by dfmartin72. 3 replies.
  • 02-02-2006 6:23 AM

    • Truelies
    • Not Ranked
    • Joined on 02-02-2006
    • Posts 1
    • Points 45

    PreserveRegionsMergeStrategy Problem

    I whant to preserve the content of the "Custom Code" region in a C# generated file

    #region Custom Code NameSpaces
        //Code I want to preserv
        string comment = "";
    #endregion

    It works in batch with the console application like this:

    cs /p:EntityName=CodeType /out:CodeType.cs /t:Entity.cst /p:EntityMap=Entities.Map.xml /merge:PreserveRegions="RegionNameRegex=^[ \t]*(?i:Custom Code);Language=C#;"

    I have tries these 4 solutions from a template file and none works, there are other post in the forum for the same problem but none have a working solution !

    CodeSmith.Engine.PreserveRegionsMergeStrategy strategy = new CodeSmith.Engine.PreserveRegionsMergeStrategy("^[ \t]*(?i:Custom Code)","C#");
    entityTemplate.RenderToFile(outputFolder, strategy);

    CodeSmith.Engine.PreserveRegionsMergeStrategy strategy = new CodeSmith.Engine.PreserveRegionsMergeStrategy();
    strategy.RegionNameRegex = "^[ \t]*(?i:Custom Code)";
    strategy.Language = "C#";
    entityTemplate.RenderToFile(outputFolder, strategy);

    CodeSmith.Engine.PreserveRegionsMergeStrategy strategy = new CodeSmith.Engine.PreserveRegionsMergeStrategy();
    strategy.RegionNameRegex = "Custom Code"; //might use the config file ??
    strategy.Language = "C#";
    entityTemplate.RenderToFile(outputFolder, strategy);

    CodeSmith.Engine.PreserveRegionsMergeStrategy strategy = new CodeSmith.Engine.PreserveRegionsMergeStrategy();
    strategy.Init("RegionNameRegex=^[ \t]*(?i:Custom Code);Language=C#;");
    entityTemplate.RenderToFile(outputFolder, strategy);

    Could you give a simple example of how this should be done ?

    Thanks


     

    • Post Points: 45
  • 02-02-2006 6:40 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,206
    • Points 1,053,930

    Re: PreserveRegionsMergeStrategy Problem

    Your code looks right except for the "outputFolder" thing.  You should be specifying the name of the file that you are going to merge with the folder where the code will be output.  Also, you need to make sure that your template is outputting the place holder regions where you will want to be able to add custom code.  I modified the EntityMaster.cst sample template to allow preserving custom regions in the entity classes.

    EntityTemplate entityTemplate = new EntityTemplate();
    entityTemplate.EntityElement = entity;
    PreserveRegionsMergeStrategy strategy = new PreserveRegionsMergeStrategy("^[ \t]*(?i:Custom)", "C#");
    entityTemplate.RenderToFile(this.OutputDirectory + entity.Name + ".cs", strategy);

    Does that help?


    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect

    • Post Points: 35
  • 01-02-2007 2:13 PM In reply to

    Re: PreserveRegionsMergeStrategy Problem

    I'm fairly new to CodeSmith and very new to the Merge strategies.  I'm using CodeSmith v4.0.0 rev 1724.  I have looked through the forums and haven't been able to find a solution, but this post came the closest.

     I'm generating C# files using a CodeSmith project from within my solution.  I have a template that is generating one file and a sub-template that is supposed to generate a second (different) file.  In the code of the master template I am doing exactly what you have listed above. (Except for how the sub-template is created and populated).  It appears that the sub-template doesn't get generated if the file already exists.  The relevant code is below:

       string filePath = this.CustomClassFilePath + this.ObjectName + ".cs";
       CodeSmith.Engine.PreserveRegionsMergeStrategy strat = new CodeSmith.Engine.PreserveRegionsMergeStrategy("^[ \t]*(?i:Custom)");
       sTemplate.RenderToFile(filePath, strat);


    Any help would be greatly appreciated.
    David Martin

     

     

     

    Filed under:
    • Post Points: 5
  • 01-02-2007 5:07 PM In reply to

    Re: PreserveRegionsMergeStrategy Problem

    I figured it out.  I don't remember reading this, but it certainly makes sense.  Each of the regions that are custom and not to be replaced must follow a naming scheme (i.e. #region Custom . . .), but also (and this is where I messed up) each of the regions must be uniquely named.  I had created a couple of test regions naming each one "Custom Code".

     One thing this has gotten me to wondering about is how others might be using the Merge Strategies.  Are people out there using them to replace the need for partial or base classes?

    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems