Discover errors in your .NET app you never knew were happening and use detailed error reports to squash them with ease. Make your app Exceptionless!
Sign up for FREE!
This tip will help you to create, then start extending, a basetemplate class of your own that can hold your favorite library routines, and also have base class access to the cool functions found in the SQLSchema and OutputFileCodeTemplate class.
1. Start by copying the sample project’s CustomProperties and BaseTemplates subdirs from \Program Files\CodeSmith\v3.2\SampleProjects into your SW dev tree (I used ModifiedCodeSmithSamples as my parent dir name). This step protects your base class and the Codesmith source and assemblies it depends on from changes in the Samples when new versions of Codesmith are released. If you like the new samples, you can incorporate them at a time convenient to you. It also keeps the original sample untouched,
2. Start VS 2005, and in a solution in which you plan to use Codesmith to create some of the code, create a solution folder called BaseTemplates. Add the two projects found in the two sample project subdirs you just copied into your SW dev tree directory . (You could also choose to create a a whole new solution to house the new MyBaseTemplate project and these two dependencies).
3. Expand the CustomProperties project, and work with it:
· Delete the "missing" references to GUI and Engine, and add references that point back to the corresponding .dlls in the \Program Files\CodeSmith\v3.2\bin dir (CodeSmith.Gui.dll and CodeSmith.Engine.dll)
· Delete the GlobalAssemblyInfo.cs file from the project.
· Change the project's signing Secure Key File to a .snk file of your own. Reference “tips for creating strong named assemblies” in VS 2005 help for info on creating a .snk file.
· Build the Assembly.
4. Expand the Codesmth.BaseTemplate project, and work with it:
· Delete the "missing" references to Engine, and add a reference that points back to the .dll in the \Program Files\CodeSmith\v3.2\bindir.
· Add a reference to the CustomProperties project you just built (Select "Projects" tab in the AddReference dialog box).
· Change the project's signing Secure Key File to a .snk file of your own.
· Build the Assembly
5. Build your own MyBaseTemplate project in this Solution Folder (C# Class Library project);
· add references to the SchemaExplorer and Engine .dlls in the /programfiles/cs/v3.2/bin dir (later you can add references to other Codesmith assemblies you want to add to MyBaseTemplate);
· add a reference to the Codesmth.BaseTemplate project you built in step 4 (use the “Projects” tab).
· Change the project's signing to indicate it should use a strong key, and point the Secure Key File to a .snk file of your own.
· Edit the Project’s properties: Select the “Build Events” sheet. Enter the following four lines in the “Post-Build event command line”
· copy "$(TargetPath)" "C:\Program Files\Codesmith\v3.2\AddIns"
· copy "$(TargetDir)$(TargetName).pdb" "C:\Program Files\Codesmith\v3.2\AddIns"
· copy "$(TargetPath)" "$(DevEnvDir)PublicAssemblies"
· copy "$(TargetDir)$(TargetName).pdb" "$(DevEnvDir)PublicAssemblies"
· Rename the class1.cs file to MyBaseTemplate.cs, and fill it with this:
public class MyBaseTemplate : SqlCodeTemplate
Build this project.
6. Start your own template.cst files with these lines:
%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.MyBaseTemplate" TargetLanguage="C#" Description="these templates inherit all the classes and methods in the basetemplates sample, plus my own." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="MyBaseTemplate" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
Have fun extending your base template!
One minor annoyance if you are working on your base template class. Codesmith seems to have a tendency to get an out-of date version stuck in its template cache here: \Documents and Settings\All Users\Application Data\CodeSmith\v3.2\Template Cache
I often clear this directory out, if I’m working on base template changes and they don’t seem to be working right, or even appearing at all. The problem also sometimes manifests as the inability to compile the templates built on the MyBaseClass. I’m pretty prone to making mistakes, so sometime I can’t tell if it is a caching problem or my fat fingers… but clearing the cache never hurts…
Hope you find this tip useful! Please post any corrections or expansions on this tip you think of.