CodeSmith Generator Projects allow developers to manage a batch of CodeSmith Generator templates and outputs. All this is done in a single CodeSmith Generator Project file (.csp). This is great for when you need to generate several code templates that are logically grouped together by their implementation. Think of data layers that can be generated with a single click to create multiple classes driven from a specific database. The possibilities are endless. In this article I will show you a simple implementation of a CodeSmith Generator Project.
CodeSmith Generator Projects
Let’s jump straight in and fire up Visual Studio. I am using Visual Studio Ultimate 2013. Go ahead and right click ‘My Templates’. Create a new CodeSmith Generator Project.
Rename the created file to a more suitable name. In the previous article I created a business object class from a single template. In this article I will create multiple classes from this single template file using the created CodeSmith Project.
Right click the project file and click on ‘Manage Outputs’ from the context menu.
From the Manage Outputs screen, click on the ‘Add Output’ menu button.
The Add Output screen will open. This screen allows you to define the specific output which is based on one of your previously created templates. I will be using the previously created SYSPRO Business Object.cst template.
I gave the output a specific name. In this output I want to create a Purchase Order class based on my template. I therefore called this output ‘Purchase Order’. Under the template section, I selected the template I previously created. For the file, I selected the Visual Studio project path and gave the file an appropriate name. If you use the default name or don’t change the file name, the output will be created in the default templates path as defined in the CodeSmith options. Lastly, the properties are then available for you to define accordingly. You can see I called this class PORTOI. After you save this output, you can go back and edit it if needs be later on.
When you click save, the output is added to the Manage Outputs screen. It will also show you the template that each specific output is based on.
Next, I’ll copy the existing Purchase Order output by clicking on the Copy Output menu button.
This creates a copy of the Purchase Order output and CodeSmith Generator appropriately renames the output.
Next, click on Edit Output to change the copied Purchase Order output.
First off, rename the output name. This time I am going to create a Sales Order class based on my template file. Therefore I called this output Sales Order. As before I used the same template but you could logically use a different template file if you need to generate a different class. For the file path, I again selected the output to be the project folder. As with purchase orders, I edited the properties appropriate to sales orders. The class name is also now called SORTOI.
After saving the Sales Order output, you will see it added to the Manage Outputs screen. You can continue doing this for multiple different classes using the same or different templates.
Just a quick note on the Manage Output menu. If you select a single or several output items in the list, you can generate just those output(s). If you click on the Generate Outputs button, CodeSmith Generator will execute all the outputs defined in the project file. You can also move the outputs around (up or down the list). Lastly you can change the project options.
For my project, I want to create a separate file for each template. I have therefore selected multiple files as an option. You could combine the various outputs into a single file if you wanted to. You can then define header detail (for example namespace declaration and using statements) and footer detail (closing C# braces for example). You can also define default properties and variables, but I will not be going into this in this review. You can refer to the detailed CodeSmith Generator documentation here.
Once you have completed editing and configuring the project, go ahead and click on ‘Generate Outputs’.
CodeSmith starts to render the outputs based upon your project file. When it has completed you can view the details if you wish.
Next I clicked on my Visual Studio project and added the created files as existing items to my visual studio project.
You can see the SORTOI and PORTOI classes added to the project. Go ahead and build the solution and see if it compiles.
Having a look at the purchase order and sales order code files side by side, you can see that they are similar in structure but totally different in implementation.
Adding CodeSmith Generator Projects Directly To Visual Studio
Lastly, there is another way to use CodeSmith Generator Projects. Right click on the Visual Studio project and add a new item. From this screen you will be able to add a CodeSmith Generator Project directly to your Visual Studio project.
You will see the CodeSmith Project added to your Visual Studio Project.
The code view for the CodeSmith Project is now opened in Visual Studio.
As described above, right click the csp file and manage the outputs for the CodeSmith Project. I just used the same template file and added two new classes to the outputs.
After saving the project outputs you will see the code view of the CodeSmith project has been modified and contains the output settings. You can actually just go ahead and close this file in Visual Studio because you aren’t going to be modifying this file directly. So whenever you change your outputs, Visual Studio might pester you to reload the file.
Next you right click the CodeSmith Generator Project and select the ‘Generate On Build’ from the Output Options context menu. This will create the output files and add them automatically to your Visual Studio solution when you build your project.
Go ahead and build your Visual Studio project. Visual Studio will notify you that there has been a file modification and allow you to reload the project.
Once you reload the project you will see the created files added to the Visual Studio solution.
The CodeSmith Generator Project made it possible for me to generate multiple class files in seconds. You can imagine how this will benefit you when the classes you need to generate become more complicated. Download a trial version and have fun with CodeSmith Generator. See for yourself how easy it is to generate code.