Introduction to CodeSmith Project Files
A new feature in CodeSmith 4.0 is the ability to manage groups of CodeSmith templates and their outputs with a single file called a CodeSmith Project. CodeSmith Projects enable you to define and run an entire code generation process quickly and easily using the environment of your choice. They were designed with flexibility in mind to let you define the way you work.
This introduction will show how to use a csp (CodeSmith project) to organize your code generation in a development environment. The example will use the sample Wilson ORMapper templates that are included in CodeSmith. To use the generated code, a copy of the Wilson ORMapper is needed.
Creating the CodeSmith Project File
The ORMapper templates are split into three separate groups of templates. The first set of templates is the MappingFile.cst. Use this template to start out your csp.
The first step is to load up CodeSmith and select the Mapping.cst template. Set the properties as needed. This example will use the sample Petshop database that comes with CodeSmith.
Once you have the properties values set, click the Save icon. This will create the start of a csp file.
Managing the CodeSmith Project File
Now use the Explorer context menu on the csp file and choose Manage Outputs. In the manage outputs dialog, add the second template that is need for the ORMapper. For this example, add the CSharp.ClassGenerator.cst template. Set the properties values for the template and click ok to close the dialogs.
Generating with a CodeSmith Project
Now the csp file has been created, it can be used to generate code. There are several ways to generate with the project file, see Using a CodeSmith Project to Generate Anywhere in the User’s Guide.
Visual Studio and CodeSmith Project Files
CodeSmith 4.0 has a new Visual Studio addin that allows you to use a csp right in Visual Studio, see Using a CodeSmith Project inside Visual Studio in the User’s Guide. Including the csp file in Visual Studio will help to keep the project clean and make it much easier to re-generate code when needed. The csp file can be added to the solution as a Solution Item or to a project directly. When the csp is added to a C# or VB project, more options are available, including adding the outputs to the project.
In the example above, the created Petshop.csp file can be added a Petshop Visual Studio solution. Now, any time there is database change, the Petshop.csp file can regenerate the ORMapped class files savings a ton of time.
The CodeSmith Project feature makes automating your code generation process really easy and consistent whether you are working from inside of Visual Studio 2005, MSBuild, Windows Explorer, a command line / batch file, or CodeSmith itself.