How do you use CodeSmith w/o the custom tool?

Christopher asks this question:

How do you use CodeSmith w/o the custom tool? Point me to some docs I've missed, I'd love to see what the hoo-ha's all about. ;-)

CodeSmith is primarily a stand alone template-based code generator application.  The VS.NET custom tool is just an additional way to use the tool.  To use CodeSmith w/o the custom tool, you simple double-click the CodeSmith shortcut if you used the installer or CodeSmith.exe if you used the .zip file version.  This will bring up the CodeSmith Explorer window which basically lets you see all the templates in a given directory and it's sub-directories.  You can then click on one of the templates to execute it, it will bring up a window to allow you to provide context information for your template and then when you click generate it will display the generated code on the right hand side.  On the other hand, the custom tool allows you to include a property set XML file in your VS.NET project that will output code directly into your project.  Here is a sample property set XML file:

<?xml version="1.0" encoding="utf-8" ?>
<codeSmith>
 <namespace>CSharpCodeGeneratorSample</namespace>
 <imports>
  <import namespace="System" />
  <import namespace="System.Data" />
  <import namespace="System.Data.SqlClient" />
 </imports>
 <propertySets>
  <propertySet>
   <property name="SourceTable">
    <connectionString>Data Source=(local)\NetSDK;Database=Northwind;Integrated Security=true;</connectionString>
    <providerType>SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider</providerType>
    <tableName>Customers</tableName>
   </property>
   <property name="IncludeComments">True</property>
  </propertySet>
  <propertySet>
   <property type="SchemaExplorer.TableSchema" name="SourceTable">
    <connectionString>Data Source=(local)\NetSDK;Database=Northwind;Integrated Security=true;</connectionString>
    <providerType>SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider</providerType>
    <tableName>Orders</tableName>
   </property>
  </propertySet>
 </propertySets>
 <template path="businessobject.cst" />
</codeSmith>

When this file is included in your VS.NET project and the Custom Tool property of the file is set to CodeSmithGenerator, then everytime you save this file it will execute the businessobject.cst template for each property set (in this case there are two sets of properties) and the output will be generated into a code-behind file.  This allows you the ability to simulate generics in that you have a single template and there are multiple instances of that template being generated based on different properties.  When you make a change to the template, all generated code reflects that change.

I hope this helps straighten things out.

No Comments