Home > Developing Using CodeFluent Entities, Producers, Tools > Patch text files after code generation with CodeFluent Entities

Patch text files after code generation with CodeFluent Entities


 
CodeFluent Entities provides a large set of concepts and tools to generate code as custom as wanted. But, it may be a reason you need to modify the output produced by the CodeFluent Entities engine when there is not an option or a feature that allows you do that.
 
The Patch Producer is a producer that lets you “patch” text files using regular expressions. You can find the Patch Producer under the Utility Producers category.
 

Patch producer

Patch producer


 
Add a Patch

Add a Patch


 
Path: can be a directory path or a file path. If relative, it is based from the directory containing the project root part.
File Search Pattern: is a file filter expression. Only used if “path” is a directory.
Search Pattern: a regular expression matching groups.
Replace: a key=value pair list separated by the “Replace Separator Character”.
 

You can use the default namespace in the path attribute using the syntax {1:DefaultNameSpace}

 
 

Example 1

 
CodeFluent Entities provides a “tracking” feature.
 

Tracking User Name

Tracking User Name


 
In most of the cases the CodeFluentUser.Name is of the form “Domain\UserName” and let’s say we only want to store in the database the “UserName” without the “Domain” (ie. The CodeFluentUser.UserName).
 
In my case I have a CodeFluent Entities project with a Business Object Model (BOM) producer and a SQL Server persistence producer. The BOM generated files are stored in a class library project having as name the default namespace.
 
So, I need to search all files matching “ *.cs ” in my BOM project:
 
.cs Search Path

.cs Search Path


 
And make the replacement using the regular expression:
,\s*persistence\.Context\.User\.(?<name>Name)\s*\)
 
And replace the matched value in the group name “name” by “UserName”:
 
UserName replace

UserName replace


 
If I build my CodeFluent Entities, now I have:
 
UserName replaced

UserName replaced


 
 

Example 2

 
CodeFluent Entities adds some information on each generated file about the generation itself. For example, for SQL files:
 

Generation information

Generation information


 
Let’s say we want to add the name our company within this information, something like “CodeFluent Generated for SoftFluent […]
We add a new “Patch” producer.
 
Generation information Patch

Generation information Patch


 
As you can see, I store my SQL files in a folder named “SQL” in the same location as my BOM generated files (cf. example 1).
 
We build the CodeFluent Entities project and now we have:
 
Generation information Patched

Generation information Patched


 
Have fun.
 
Regards,
Pablo Fernandez Duran
 
 

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s