You are here
Home > Reviewed > Visual Studio Integration Of CodeSmith Generator

Visual Studio Integration Of CodeSmith Generator

Visual Studio Integration – In the previous article I showed you how to create a quick template in CodeSmith Generator Studio. If you remember how easy it was to do, you will most certainly love this article on integrating CodeSmith Generator inside Visual Studio. It is just as easy. Ultimately, you want to stay in one IDE. I personally dislike jumping between applications when I am knee deep in code. CodeSmith Generator has thus made integration into Visual Studio a breeze. I am using Visual Studio 2013 Ultimate.

Visual Studio Integration – Increase Developer Productivity

Start by opening Visual Studio and creating a new project. You will see that there is a new menu item. Click ‘Template Explorer’ or type Ctrl+E, Ctrl+T.

Visual Studio Integration

This will enable the Template Explorer in the Visual Studio IDE. CodeSmith Generator ships with some awesome samples, so it’s good to keep this in mind before you go ahead and write your own template.

Visual Studio Integration

We want to create a new CodeSmith Generator Template. So right click the ‘My Templates’ folder and select New -> CodeSmith Generator Template. I chose to create a C# template.

Visual Studio Integration

CodeSmith Generator creates a new file called NewTemplate1.cst. Go ahead and rename this to a more descriptive name.

Visual Studio Integration

I’ll be creating the Business Objects template I showed you in the previous article so I renamed my file accordingly.

Visual Studio Integration

As you can see, I created roughly the same template as in the previous article. I just added additional code to ensure that it would build. I also included some default values for some of the property directives.

Visual Studio 2013 Integration

Once you have completed the template, you need to save it and right click on the template file. From the context menu, select Execute.

Visual Studio 2013 Integration

CodeSmith Generator will now display the properties screen to allow you to define specific settings for this template. When you have added these, click on the Generate button.

Visual Studio 2013 Integration

CodeSmith Generator now pops up a screen with your generated code output. There might be a more elegant way of doing this, but I just went ahead and saved the generated code to a code file inside my Visual Studio project.

Visual Studio 2013 Integration

After saving the generated code to a file in my solution, I right clicked on my project and selected Add -> Existing Item.

Visual Studio Integration

I navigated to the file I saved previously and added it to my project.

Visual Studio Integration

The added file is now displayed inside your solution explorer. Go ahead and build your solution to ensure a clean build. If there are any errors, simply modify your CodeSmith Generator template and save the template before generating the file again. When you save this file (replacing the existing file), Visual Studio will inform you that the file has been modified outside the IDE and ask you if you want to reload the file.

Visual Studio Integration

As you can see, the power of CodeSmith Generator is easily extended to Visual Studio making you a more productive developer. Another great feature is that all your templates are going to be available to you throughout all your projects in Visual Studio. This is fantastic for re-usability. So the challenge is creating truly generic code templates. It might take a little time initially to create your template, especially if it is a complex template. The upside of this is obviously that generated code is simply a click of a button away.

Stay tuned for more CodeSmith Generator articles.

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