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.
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.
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.
CodeSmith Generator creates a new file called NewTemplate1.cst. Go ahead and rename this to a more descriptive name.
I’ll be creating the Business Objects template I showed you in the previous article so I renamed my file accordingly.
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.
Once you have completed the template, you need to save it and right click on the template file. From the context menu, select Execute.
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.
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.
After saving the generated code to a file in my solution, I right clicked on my project and selected Add -> Existing Item.
I navigated to the file I saved previously and added it to my project.
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.
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.