CodeSmith BASICS (7)

Source: Internet
Author: User

The translation in this article is the CodeSmith console guide.

Many people only know that CodeSmith is like a graphics application or a Visual Studio attachment, but there are many other usage methods through CodeSmith's console application. The console application is very valuable because it can be used to generate scripts or some other automatic tools. This document aims to show you how to use its console application and how to define variables and parameters.

Basic Usage

In most cases, you can use the console application to create a template, an attribute file, and then save the output file. This is a good example of putting the process of merging templates into one process, just like using the NAnt tool.

First, we need to determine what kind of template to complete and what kind of XML Attribute file to create for this template. The XML property file provides the attributes required for execution templates. The simplest way to generate an attribute file is to open a template in CodeSmith Explorer, fill in the attributes, click generate, and then click Save Property Set XML. This button will be found after you click the generate button, next to the Save Output and Copy Output buttons. Then, the system prompts you to enter the file name for saving the XML property file. The following shows an XML property File Created by the ArrayList. cst template.

1 <? Xml version = "1.0" encoding = "us-ascii"?>
2 <codeSmith>
3 <propertySet>
4 <property name = "Accessibility"> Public </property>
5 <property name = "ClassName"> PersonArray </property>
6 <property name = "ItemType"> Person </property>
7 <property name = "ItemValueType"> False </property>
8 <property name = "ItemCustomSearch"> False </property>
9 <property name = "KeyName"> PersonID </property>
10 <property name = "KeyType"> int </property>
11 <property name = "IncludeInterfaces"> True </property>
12 <property name = "IncludeNamespaces"> False </property>
13 </propertySet>
14 </codeSmith>

As you can see, you can also manually create this file, but use

CodeSmith Explorer is easier.

Now that we have this XML file, let's continue to see how to execute this template and use the console tool to save the results. First, we need to use the/template parameter to declare the template we want to use, like this:

C: \ Program Files \ CodeSmith \ v3.0> cs/template: Samples \ Collections \ ArrayList. cst

In this example, we use the ArrayList. cst template, which is stored in the local Samples \ Collections folder. Next, we need to declare the XML file we need to create in the last step. We use the/propertyset parameter for implementation.

C: \ Program Files \ CodeSmith \ v3.0> cs/template: Samples \ Collections \ ArrayList. cst/propertyset: PersonArray. xml

This/property parameter is used to specify our XML property file. The last parameter we need is the/output parameter, which is used to specify how the output is saved.

C: \ Program Files \ CodeSmith \ v3.0> cs/template: Samples \ Collections \ ArrayList. cst/propertyset: PersonArray. xml/out: test. cs

Use the/out parameter to output the result to a file named test. cs and save it. After you run this command, the template starts to run. You can use the property file to output the result to the test. cs file and save it.

This is the most effective way to use the console.

 

Merging Output

The biggest challenge in code generation is to separate the generated code from the code written or modified by the developer. The console provides an effective and unique solution to this problem. You can use a specified parameter to specify a region where the code generated by the template needs to be added to an existing code file.

The following is a simple code file that contains the area where we want to add and generate code.

1 using System;
2
3 namespace Entities
4 {
5 GeneratedOrderEntity # region GeneratedOrderEntity
6
7
8 # endregion
9}

Our goal is to add the code generated by the DatabaseSchema \ BusinessObject. cst template to the GeneratedOrderEntity area of the class file. As in the previous example, the template is executed using the CodeSmith console application, but another parameter merge is used this time.

C: \ Program Files \ CodeSmith \ v3.0> cs/template: Samples \ DatabaseSchema \ BusinessObject. cst/propertyset: OrderEntity. xml/out: OrderEntity. cs/merge: InsertRegion = "RegionName = Sample Generated Region; Language = C #;"

Using the merge parameter, we can specify the region name. In this example, GeneratedOrderEntity is used. Then, the console application will execute the template and add the result to this region. Let's take a look at the code generated after this command is executed.

1 using System;
2
3 namespace Infozerk. AuthServices. UnitTestSuite
4 {
5 GeneratedOrderEntity # region GeneratedOrderEntity
6
7
8 Order # region Order
9/** // <summary>
10 // This object represents the properties and methods of a Order.
11 /// </summary>
12 public class Order
13 {
14 protected int _ id;
15 protected string _ customerID = String. Empty;
16 protected int _ employeeID;
17 protected DateTime _ orderDate;
18 protected DateTime _ requiredDate;
19 protected DateTime _ shippedDate;
20 protected int _ shipVia;
21
22 -- other parts of the class are omitted for the sake of short

As you can see, the Order class is added to the specified region. The content generated by using the merge parameter in the code file can be easily re-generated after other parts are modified or handwritten without any impact.

 

Parameter introduction Parameter Reference

 

Specifying Output

/Out: <file>

Specifies the name of the output file created from the template.

/Out: default

Specifies the name of the file to be saved as a template by default.

/Merge: <mergetype >=< init>

Specifies the output area of the template. It can be abbreviated to/m.

 

Specifying Input

/Template: <file>

Select the template to be executed, abbreviated as/t

/Propertyset: <file>

The XML Attribute file used to generate code. Abbreviated as/p

 

Compiler Options

/Debug [+ |-]

Specifies the debugging information to be included in the template. (Debugging can be performed when running the template)

/Tempfiles [+ |-]

Specifies to retain temporary files. (If debugging is performed on a temporary file)

 

Miscellaneous

/Help

Displays help information.

/Nologo

Copyright information of the generator is forbidden.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.