Sunday, May 13, 2007

My First CodeSmith Template

Download source - 1 KB

Consider this template that will generate the output of an AssemblyInfo.cs file

<%@ Property Name="Author" Type="System.String" Description="Lead author of the project." %>
<%@ Property Name="Title" Type="System.String" Description="Title of the project." %>
<%@ Property Name="Description" Type="System.String" Description="Description of the project." %>
<%@ Property Name="Configuration" Type="System.String" Default="Debug" Description="Project configuration." %>
<%@ Property Name="Company" Type="System.String" Default="MegaUtilities, Inc." %>
<%@ Property Name="Product" Type="System.String" Description="Product Name." %>
<%@ Property Name="Version" Type="System.String" Default="1.0.*" Description=".NET assembly version." %>
<%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version." %>
using System.Reflection;
using System.Runtime.CompilerServices;
//
// Created: <%= DateTime.Now.ToLongDateString() %>
// Author : <%= Author %>
//
[assembly: AssemblyTitle("<%= Title %>")]
[assembly: AssemblyDescription("<%= Description %>")]
[assembly: AssemblyConfiguration("<%= Configuration %>")]
[assembly: AssemblyCompany("<%= Company %>")]
[assembly: AssemblyProduct("<%= Product %>")]
[assembly: AssemblyCopyright("Copyright (c) <%= DateTime.Now.Year.ToString() %> <%= Company %>")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("<%= Version %>")]
[assembly: AssemblyFileVersion("<%= FileVersion %>")]
[assembly: AssemblyDelaySign(true)]

You should notice three distinct types of code in the previous template

1.Content that will never change

CodeSmith treat any code written outside the tag

<%= %>

as a static content that will appear as it was written the output file

2.Content that can be automatically generated

Like the segment

<%= DateTime.Now.ToLongDateString() %>

This will print the date of creation the output file

3. Content that you will prompt the user for

Like the property

<%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version."%>

This property will prompt the user to enter the value of the FileVersion

No comments: