Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CodeSmith wrote the first code template


May 25, 2021 CodeSmith


Table of contents


Write the first code template

In CodeSmith Use Tutorial (1): Overview We can learn the basic steps of using CodeSmith to automatically generate code using CodeSmith to automatically generate NHiberate code from a database:

  1. Choosing to use the right template, CodeSmith comes with a number of commonly used templates with the development package, and if you can't find the right template, CodeSmith supports custom templates.
  2. Select the appropriate parameter settings for the template.
  3. Auto-generated code (can be any type of code, C, Java, . . XML text, etc.)

CodeSmith wrote the first code template

Its core is the code template file, with CodeSmith comes with a lot of commonly used templates, can be queried through the template browser, in addition to the Internet there are many third-party development of templates, before using can check whether there are ready-made templates, or you can modify the existing template to complete the need for automatic code generation.

When developing an app, many people like to create templates for these similar code by copying the code in the previous project and then modifying it to satisfy the new project, which often has a lot in common (think of the template class for C++, generic for C#, and so on), and then by setting properties (directly different points in the code), CodeSmith can automatically create the code you need.

This example provides a simple example of how to create a custom code template. CodeSmith provides support for Visual Studio's integrated development environment, in this case, assemblyInfo.cs, which is also required to simplify each C# project by creating templates, typically by manually modifying the middle properties of AssemblyInfo.cs (or Copy and Paste :-) when developing the C# app.

First, let's use Visual Studio to create a version of AssemblyInfo in your project under console or WinForm project .cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HelloWorld")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("HelloWorld")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("72797715-64b9-4bab-a49f-f55e8a0a18d7")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

To use CodeSmith, we add CodeSmith's project file in HelloWorld and create a template file, AssymblyInfo.cst

CodeSmith wrote the first code template

The created project files are as follows:

CodeSmith wrote the first code template

Writing CodeSmith's code templates is very similar Asp.Net writing a page, and CodeSmith supports the use of C, VB. Net and JavaScript write templates as scripting languages, in this case using C# as scripting language (source code/language), and the planned build is also the C# language (target code/language), open AssemblyInfo.cst, and modify the code as

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %>

Each CodeSmith code template starts with CodeTemplate, defining the source language, target language, and simple description that the code template uses.

Then add this template to your CodeSmith project, right-click codesmith.csp, and select Add output

CodeSmith wrote the first code template

CodeSmith's project will then be created, but clicking "Generate code" won't generate any code because our code template AssemblyInfo.cst didn't do anything.

Creating a code template can start with the results generated, and you can copy the code you want to build directly to the code template AssemblyInfo.cst, for example:

using System.Reflection;
using System.Runtime.CompilerServices;
//
// Created: 1/1/2013
// Author: James Shen
//
[assembly: AssemblyTitle("User storage utility")]
[assembly: AssemblyDescription("Helps manage data in Isolated Storage files.")]
[assembly: AssemblyConfiguration("Retail")]
[assembly: AssemblyCompany("Guidebee Pty Ltd, Inc.")]
[assembly: AssemblyProduct("StorageScan")]
[assembly: AssemblyCopyright("Copyright (c) Guidebee Pty Ltd.")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0")]
[assembly: AssemblyDelaySign(true)]

You can divide the contents of the code template you want to generate into three parts:

  • Fixed content
  • Parts that can be dynamically generated by code (such as the date above)
  • The part that requires the user to provide the property configuration

If you use Codesmith's Generate Codes, AssemblyInfo .cs (the default is the template name) is automatically generated, but the AssemblyInfo.cs location is not the Properties/AssemblyInfo.cs we need, which can be done by overloading the GetFileName method of the code template:

<%@ CodeTemplate Language="C#" TargetLanguage="C#"
  Description="Create an AssemblyInfo.cs file." %>
...
<script runat="template">
public override string GetFileName() {
    return "Properties/AssemblyInfo.cs";
}
</script>

This automatically overrides the original Properties/AssemblyInfo file when using The Generate Codes .cs Smith project. The content is the part of the code in the template.

But each time the generated code is fixed, as a template there is no flexibility, we can check the content of the template, those content is variable. For example, AssemblyInfo .cs the date of the assembly and the properties of Assembly are variable for different projects.

Some of these variables can be automatically generated by code, such as dates, and some need to be configured by users, such as AssetItle, AssemblyDescription, and so on.

For the date section, it can be implemented as follows by the C-code:

// Created: <%= DateTime.Now.ToLongDateString() %>

As you can see, CodeSmith's template files, such as AssemblyInfo.cst and Asp.Net's Page file, have very similar functionality and can be embedded directly into the code (or VB.Net, JavaScripts) by embedding them directly in the code.

For properties, you can define them first:

<%@ 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="Guidebee Pty Ltd." %>
<%@ 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." %>

Property definitions are defined by Property, Name defines property names, Type is the data type of a property, Default defines the default value of a property, and Description defines the role and description of the property.

You can then use these properties in the code for C#, and the complete code template is as follows:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create 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="Guidebee Pty Ltd." %>
<%@ 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)]

If you need "Generate output" to first configure the properties of the code template, this is done through "Manage output."

CodeSmith wrote the first code template

Number of times if you open the codesmith.csp file you can see the property content configured for AssemblyInfo.cst:

<?xml version="1.0" encoding="utf-8"?>
<codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
  <propertySets>
    <propertySet name="AssemblyInfo" template="AssemblyInfo.cst">
      <property name="Configuration">Debug</property>
      <property name="Company">Guidebee Pty Ltd.</property>
      <property name="Version">1.0.*</property>
      <property name="FileVersion">1.0</property>
      <property name="Author">James Shen</property>
      <property name="Title">Code smith Demo</property>
      <property name="Description">My First Template code</property>
      <property name="Product">SandCastle</property>
    </propertySet>
  </propertySets>
</codeSmith>

The generated code is as follows:

using System.Reflection;
using System.Runtime.CompilerServices;
//
// Created: Thursday, 3 January 2013
// Author:  James Shen
//
[assembly: AssemblyTitle("Code smith Demo")]
[assembly: AssemblyDescription("My First Template code")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCompany("Guidebee Pty Ltd.")]
[assembly: AssemblyProduct("SandCastle")]
[assembly: AssemblyCopyright("Copyright (c) 2013 Guidebee Pty Ltd.")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0")]
[assembly: AssemblyDelaySign(true)]

This example Downloads