Using a Generator Project from MSBuild

You can create your own custom pre-generation build logic by utilizing the CodeSmith Generator 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 Generator from MSBuild using the CodeSmith Generator task that's shipped for you.

CodeSmith Generator MSBuild Targets

An MSBuild target file is used to define your own tasks during the build process. You can install the CodeSmith.Generator.Task NuGet package which defines all the capabilities of using generating CodeSmith Generator Projects during the generation process. The NuGet package contains a targets file that defines how to use the Generate BuildAction from your Generate Items and also defines how to call CodeSmith with your CodeSmith Generator Project file and run the generation process.


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 MSBuild Project file.

You can import this target in your Visual Studio Projects by using the CodeSmith Generator Import tag in your Visual Studio Project file.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003 ">
   ...
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   ...
<Project>

Generating a Generator Project

CodeSmith Generator projects can be run from your MSBuild projects by adding the following line to your project file:

 

<CodeSmith ProjectFile="MyProject.csp" />

 

In this example we will be showing how to call a custom CodeSmith Generator Project that will generate necessary meta-data prior to building in release mode.

Example

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003 ">
   ...
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 
  <Target Name="BeforeBuild" Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <CodeSmith ProjectFile="GenerateMetaData.csp" />
  <Target>
   ...
<Project>

Usage

The CodeSmith Generator Task exposes a few properties to assist you in your CodeSmith Generator tasks.

ProjectFiles: is the property that can accept a single or ";" separated values of names of CodeSmith Generator Projects that will be used for Code Generation.

Example
<CodeSmith ProjectFile="GenerateMetaData.csp;GenerateIndex.csp" />

OutputFiles: is the property that specifies all of the OutputFiles specified in your CodeSmith Generator 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.

Property: a string property that takes a list of key value pairs that should override any property values defined in the CodeSmith Generator Projects.

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
<ItemGroup>
      <CspFiles Include="MyProject.csp" />
      <CspFiles Include="MyOtherProject.csp" />
    <ItemGroup>

    <Target Name="Build">
      <CodeSmith ProjectFiles ="@(CspFiles)" Property="StringProperty=test;" />
    <Target>

Find More information on MSBuild Tasks.