To add a new CodeSmith Generator Project file to your Visual Studio Project, right click on a Project or folder inside of a project inside of Solution Explorer. Then choose File > New Item from the context menu. This will open the Visual Studio New File Wizard. Next, you will want to select CodeSmith Generator under the General Installed Templates node. Doing this will only show you the available CodeSmith Generator Item Templates.
Finally, select Generator Project and then select the Open button or double click on Generator Project.
To manage a CodeSmith Generator Project, you can use the Right-Click context menu of a CodeSmith Generator Project file from the Solution Explorer tool window.
The Output Options sub menu include:
You can remove the CodeSmith Generator Visual Studio Project dependencies by turning off Generate On Build. |
You can use ActiveGeneration quite easily in your Visual Studio projects now, simply by specifying the BuildAction of your CodeSmith Generator Project.
Setting the BuildAction = "Generate" in the properties of you CodeSmith Generator Project file in the Solution Explorer will cause your entire generation process to occur prior to your project building. This means that any Outputs that you have in your CodeSmith Generator Project will be generated and if you want, included in the Visual Studio project.
We have a BusinessObject.csp CodeSmith Generator Project in our class library Visual Studio project. This CSP has 4 outputs, that generate from the same BusinessObject.cst CodeSmith Generator template.
Looking into the properties of the CodeSmith Generator Project, you can view the Build Action of the file, and there is an option to set it to Generate on Build. Meaning every time you need to tweak your Database meta-data, XML Property meta-data, or CodeSmith Generator template, the changes are picked up in your Visual Studio project the very next time you build.
This enables you to alleviate much of the frequent developer problems with making changes across all of your classes, during the development process.
As you can see depicted in the image below, the business object was generated, and then the build process began. This is a very powerful feature since it allows you to have strong Code Generation integration inside all of your projects in Visual Studio without having to switch to Template Explorer.
A new overload to the templates RenderToFile that will take a parent (DependentUpon) file. This will add metadata to the output file that Visual Studio will use when adding the file to create the hierarchy.
To get your template to support this, you'll need to update the template to use the RenderToFile overload that takes a parent file. Next, add the CodeSmith Generator Project to the Visual Studio project and Generate Outputs. CodeSmith Generator will automatically add the outputs to your Visual Studio project creating the hierarchy.
Sample Template Code
//Create Sub template using the Create method to automaticly wire everything up EntityGeneratedClass entityClass = this.Create<EntityGeneratedClass>(); EntityEditableClass partialClass = this.Create<EntityEditableClass>(); string className = type.Name; string parentFileName = className + ".cs"; parentFileName = Path.Combine(OutputDirectory, parentFileName); //Output parent file partialClass.RenderToFile(parentFileName, false); string fileName = className + ".Generated.cs"; fileName = Path.Combine(OutputDirectory, fileName); //Output child (dependent) file linking to parent entityClass.RenderToFile(fileName, parentFileName, true); |
When generating files to a Visual Studio Project, you may want to set the Build Action of a generated file to a specific value. For example, when generating an EDMX file, you always want to set the Build Action to EntityDeploy.
One can accomplish this by updating your template. First you must create an instance of OutputFile and pass it the file path of the item being generated. Next, you need to add the BuildAction to the MetaData dictionary.
OutputFile output = new OutputFile("PetShop.edmx"); output.Metadata.Add("BuildAction", "EntityDeploy"); this.RegisterOutput(output); |
Now you have two options to ensure that the BuildAction is set correctly.