CodeSmith Generator – I love automating repetitive tasks when programming. Sometimes this isn’t as easy as it sounds. I have specific Visual Studio Templates for specific projects. As I build on the previous template (Improve or expand), I create a new template and export that. There is however one thing that I do a lot, and that is create classes that post specific XML to SYSPRO via WCF or Web Services. Obviously I have been creating a reusable and generic set of classes that allow me to post to SYSPRO using the various business object XML’s. This is a work in progress as there are many business objects. One thing I do is create a class with a set structure. CodeSmith Generator allows me to automate this process. The review below will outline a very basic CodeSmith Generator template. Obviously this is just scratching the surface and I hope to expand on this post in later articles.
CodeSmith Generator – Creating Your First Template
So I guess that right off the bat, any developer will want to see what the software is capable of. What better way to do this than to create a template. Fire up CodeSmith Generator Studio and select File -> New. For my purposes I’ll create a C# Template.
CodeSmith Generator creates a default template for you with some basic content. This might look daunting to a first time user, but believe me once you have rolled your own you will realize how easy this process is. Go ahead and delete everything in this template file.
We will now add our CodeTemplate Directive. This just gives CodeSmith Generator Studio some basic info regarding this template. CodeSmith Generator templates are really just plain text files containing one of three different content types. These are:
- Directives to the CodeSmith Generator engine
- Static content that is copied directly to the template’s output
- Dynamic content (programming code) that is executed by the CodeSmith Generator engine
CodeSmith Tools have a very comprehensive documentation wiki. One thing that they suggest when creating a new template (which make total sense) is to start with the end result. In my case, I want to create the code file below. So I paste this into the template editor below the CodeTemplate Directive.
With my end result in the template editor, I must now start adding dynamic content. CodeSmith Generator looks for content wrapped between <%= and %> tokens. The content between these tokens is treated as expressions which CodeSmith Generator evaluates at runtime. As you can see, the code between the tokens is simply just C#. In the example below, the result of DateTime.Now.ToLongDateString() replaces the expression.
Sometimes there is information that you need to define in your code file that can’t be done via C# alone and you need user input. Let’s assume that we want to define the Author and class description of the code file. For this purpose CodeSmith Generator allows for the creation of property directives. For each bit of information you want to collect from the user, you need to add a property directive. You can see that I have added four property directives below the CodeTemplate Directive. These are Author, Description, Namespace and Class name.
Now, in my code file I can see the property directive appear in my intellisense when typing the name of the property directive. You can also see the description of the specific property directive in the little pop-up next to the property. You can even specify default values for a property directive by doing the following:
<%@ Property Name=”Company” Type=”System.String” Default=”Acme Company, Inc.” %>
After adding all the dynamic content, my completed template looks as follows.
If I now save my template file and double click it in Windows Explorer it will open with the template properties pane. I just did this in CodeSmith Generator Studio itself. Fill in the required information for the property directives we defined earlier.
When you have done this, click on the Generate button.
Behold your generated code file in all it’s glory. I can now generate many different classes based on this single template by simply changing my property values as needed. I now don’t have to copy, past and edit anything any more. What a pleasure.
Obviously this is just a small bit of what CodeSmith Generator can actually do, but this simple example will give you some idea of the power that lies beneath. Stay tuned for a few follow-up posts on CodeSmith Generator. There is much more to learn and see.