Welcome to weblogs.com.pk Sign in | Join | Help

The Priceless Match Of CodeSmith And Visual Studio 2005

CodeSmith is my all time favorite code generator. Its template based code generator and if you have a background of ASP/Perl or other similar scripting technologies like JSP/PHP etc, you will find yourself home! With its recent release which supports tight integration with Visual Studio 2005; one can use it as Custom Tool. Here is how you can do wonders!!

Add an XML file in your project; and write the following in it.

<?xml version="1.0" encoding="utf-8" ?>
<
codeSmith>
    <!--
template that will be used to generate code -->
    <
template path="hashtable.cst" />
    <!--
namespace for the generated code -->
    <
namespace>CSharpCodeGeneratorSample</namespace>
    <!--
namespaces that should be imported -->
    <
imports>
        <
import namespace="System" />
        <
import namespace="System.Collections" />
    </
imports>
    <!--
any number of variables can be setup for use in the property sets -->
    <
variables>
        <!--
this variable can be used in the property sets like $(SomeVariable) -->
        <
add key="SomeVariable" value="SomeValue" />
    </
variables>
    <!--
any properties can be setup here as defaults and can be overridden in each property set -->
    <
defaultProperties>
        <
property name="Accessibility">Public</property>
    </
defaultProperties>
    <!--
list of property sets which each get loaded into the template and executed -->
    <
propertySets>
        <
propertySet>
            <
property name="KeyType">string</property>
            <
property name="ItemType">string</property>
            <
property name="ClassName">StringHashtable</property>
        </
propertySet>
        <
propertySet>
            <
property name="KeyType">string</property>
            <
property name="ItemType">int</property>
            <
property name="ClassName">StringIntHashtable</property>
        </
propertySet>
        <
propertySet>
            <
property name="KeyType">int</property>
            <
property name="ItemType">int</property>
            <
property name="ClassName">IntHashtable</property>
        </
propertySet>
    </
propertySets>
</
codeSmith>

Then add the following files (using add existing items) in your project from %CodeSmith-3.2-Installtion%\SampleTemplates\Collections folder

  • CommonScript.cs
  • Hashtable.cst
  • IDictionary.cst
  • IList.cst

Then view the properties of your XML file and set the custom tool; as shown below.

Now whenever you will save the XML file; your custom tool will automatically run; additionally you can run the custom tool from its context menu as well. What will happen by running the CodeSmith as its custom tool is that CodeSmith will compile the template as defined in your XML file and generate the code and the code file thus generated will automatically be included in your project as shown below.

Welcome to the next generation development experience…Have fun!

Disclaimer

You might need the professional edition of CodeSmith; which is not free. This post is not about advertising CodeSmith; but just to show you the possibilities. CodeSmith is not that expensive anyways; and the value it brings is simply priceless‚Ķ

Published Thursday, February 16, 2006 6:45 PM by khurram
Filed under: ,

Comments

No Comments

New Comments to this post are disabled