You are here
Home > Reviewed > CodeSmith Generator Projects And Visual Studio

CodeSmith Generator Projects And Visual Studio

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

Right click the project file and click on ‘Manage Outputs’ from the context menu.

CodeSmith Generator Projects

From the Manage Outputs screen, click on the ‘Add Output’ menu button.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

Next, I’ll copy the existing Purchase Order output by clicking on the Copy Output menu button.

CodeSmith Generator Projects

This creates a copy of the Purchase Order output and CodeSmith Generator appropriately renames the output.

CodeSmith Generator Projects

Next, click on Edit Output to change the copied Purchase Order output.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

Once you have completed editing and configuring the project, go ahead and click on ‘Generate Outputs’.

CodeSmith Generator Projects

CodeSmith starts to render the outputs based upon your project file. When it has completed you can view the details if you wish.

CodeSmith Generator Projects

Next I clicked on my Visual Studio project and added the created files as existing items to my visual studio project.

CodeSmith Generator Projects

You can see the SORTOI and PORTOI classes added to the project. Go ahead and build the solution and see if it compiles.

CodeSmith Generator Projects

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.

CodeSmith Generator Projects

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.

1 add new item

You will see the CodeSmith Project added to your Visual Studio Project.

2 csp project added

The code view for the CodeSmith Project is now opened in Visual Studio.

2a csp xml

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.

3 Manage 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.

4 csp xml

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.

5 csp Output Options

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.

6 after build

Once you reload the project you will see the created files added to the Visual Studio solution.

7 project rebuilt

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.

Dirk Strauss
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has almost mastered.
https://dirkstrauss.com

Similar Articles

Top