Introduction
You can create your own custom pre-generation build logic by utilizing the CodeSmith Task within MSBuild. MSBuild tasks help manage the build process within your Visual Studio projects.
Why would I want to roll my own when you integrate it with Visual Studio?
There might be times when you need to customize some aspect of the generation process during it's consuming build process. During these time you might have to call CodeSmith from Ms-Build using the CodeSmith task that's shipped for you.
CodeSmith MSBuild Targets
An MSBuild target file is used to define your own tasks during the build process. CodeSmith ships their own targets file which defines all the capabilities of using generating CodeSmith Projects during the generation process. The CodeSmith.targets file defines how to use the Generate BuildAction from your Generate Items and also defines how to call CodeSmith with your CodeSmith Project file and run the generation process.
This file can be found:
Program Files
\MSBuild\CodeSmith\CodeSmith.targets
NOTE: When you set the BuildAction to Generate in Visual Studio, you're actually using the CodeSmithGenerate Target and set the project item to use the Generate Build Task.
Configuration
In order to use the CodeSmith Generation task you must import the CodeSmith.targets file for usage in your Ms-Build Project file.
You can import this target in your Visual Studio Projects by using the Import tag in your Visual Studio Project file.
Code Block | ||
---|---|---|
| ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003 "> ... <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets "> <Import Project="$(MSBuildExtensionsPath)\CodeSmith\CodeSmith.targets "> ... <Project> |
Generate
CodeSmith projects can be manually executed from your MS-Build project file by executing:
<CodeSmith ProjectFile="MyProject.csp" />
In this example we will be showing how to call a custom CodeSmith Project that will generate necessary meta-data prior to building in release mode.
Example:
Code Block | ||
---|---|---|
| ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003 "> ... <Import Project="$(MSBuildExtensionsPath)\CodeSmith\CodeSmith.targets "> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets "> <Target Name="BeforeBuild" Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <CodeSmith ProjectFile="GenerateMetaData.csp" > <Target> ... <Project> |
Usage
The CodeSmith Task exposes a few properties to assist you in your CodeSmith tasks.
ProjectFiles - is the property that can accept a single or ";" separated values of names of CodeSmith Projects that will be used for Code Generation.
Example:
Code Block | ||
---|---|---|
| ||
<CodeSmith ProjectFile="GenerateMetaData.csp;GenerateIndex.csp"> |
OutputFiles - is the property that specifies all of the OutputFiles specified in your CodeSmith Project files.
Verbose - a boolean property that indicates whether or not to receive verbose messages.
Debug - a boolean property that indicates if the templates should be compiled in debug mode.
You can also specify your own templates to run during this custom build process by using the CspFiles tag. This will let you specify your items in an ItemGroup and pass them all into ProjectFiles.
Example:
Code Block | ||
---|---|---|
| ||
<ItemGroup> <CspFiles Include="MyProject.csp" > <CspFiles Include="MyOtherProject.csp" > <ItemGroup> <Target Name="Build"> <CodeSmith ProjectFiles ="@(CspFiles)" > <Target> |