CodeSmith代码生成模板

一、主模板代码,用于调用子模版。

 <%@ CodeTemplate Language="C#" TargetLanguage="C#"  Debug="True" Description="模板输出" %>
  
  <%@ Property Name="NameSpace" Type="System.String" Default="" Category="NameSpace" Optional="True" Description="项目的命名空间"  %>
  
  <%@ Property Name="SourceDatabase" Type="DatabaseSchema" Default="" Optional="True" Category="数据源" Description="选择数据库" %>
  <%@ Property Name="SourceTables" Type="SchemaExplorer.TableSchemaCollection" Default="" Optional="True" Category="数据源" Description="选择数据表,默认空则为全部表" %>
  
  
  <%@ Property Name="TemplateDirectory" Type="String" Editor="System.Windows.Forms.Design.FolderNameEditor" EditorBase="System.Drawing.Design.UITypeEditor" Optional="False" Category="模板" Description="--模板目录--"%>
 <%@ Property Name="ExceptPrefix" Type="String" Default="" Optional="True" Category="模板" Description="排除前缀为__的" %>
 
 <%@ Property Name="OutputDirectory" Type="String" Editor="System.Windows.Forms.Design.FolderNameEditor" EditorBase="System.Drawing.Design.UITypeEditor" Optional="False" Category="输出目录" Description="--输出目录--"%>
 

 <%@ Assembly Name="System.Design" %>
 <%@ Assembly Name="SchemaExplorer" %>
 <%@ Import Namespace="SchemaExplorer" %>
 
 <%@ Import Namespace="System.IO" %>
 <%@ Import Namespace="System.Xml" %>
 <%@ Import Namespace="System.Text" %>
 <%@ Import Namespace="System.Windows.Forms.Design" %>
 <%@ Import Namespace="System.Text.RegularExpressions" %>
 <%@ Import Namespace="System.Collections.Specialized" %>
 <%@ Import Namespace="System.Collections.Generic" %>
 
 
 
 <script runat="template">
     //模板列表
     Dictionary<CodeTemplate, string> templates = new Dictionary<CodeTemplate, string>();
    
     //入口
     public void Generate()
     {
         GetTemplates(TemplateDirectory); //读取模板
   
         int tableIndex = 0;
         int templateIndex = 0;
         TableSchemaCollection tables = SourceTables != null && SourceTables.Count > 0 ? SourceTables : SourceDatabase.Tables; //若未手动选择数据表,则默认为全数据库
         foreach(TableSchema SourceTable in tables)
         {
             tableIndex++;
 
             Response.Write(string.Format("{0}.Table {1}",tableIndex, SourceTable.Name));
             Response.WriteLine();
             templateIndex = 0;
             foreach(var template in templates)
             {
                 templateIndex++;
                 Response.Write(string.Format("     {0}.template {1}",templateIndex, template.Key.CodeTemplateInfo.FileName));
                 Response.WriteLine();
                 template.Key.SetProperty("NameSpace",NameSpace);       //设置统一的命名空间
                 template.Key.SetProperty("SourceTable",SourceTable);   //传入数据表的名称
                 
                 //读取模板的文件命名格式,生成文件名
                 string FileName = template.Key.GetProperty("FileNameFormat") == null ? SourceTable.Name : string.Format(template.Key.GetProperty("FileNameFormat").ToString(),SourceTable.Name);
                 template.Key.RenderToFile(Path.Combine(template.Value, FileName), true); //按照模板的目录层级生成文件
             }
             Response.WriteLine();
         }
     }
     
     private void GetTemplates(string directoryStr)
     {
         DirectoryInfo directoryInfo = Directory.CreateDirectory(directoryStr);
         GetTemplates(directoryInfo);
     }
     
     //递归方式读取所有满足要求的模板,记录模板的目录结构,生成代码文件时参照此时的目录结构
     private void GetTemplates(DirectoryInfo directoryInfo)
     {
         foreach (var file in directoryInfo.GetFiles())
         {
             if (!file.Extension.ToLower().Equals(".cst") || (!string.IsNullOrEmpty(ExceptPrefix) && file.Name.StartsWith(ExceptPrefix)))
             {
                 continue;
             }
             
             CodeTemplateCompiler compiler = new CodeTemplateCompiler(file.FullName);
             compiler.Compile();   //编译子模板
             if (compiler.Errors.Count == 0)
             {
                 templates.Add(compiler.CreateInstance(),directoryInfo.FullName.Replace(TemplateDirectory, OutputDirectory));
             }
             else
             {
                 Response.WriteLine("编译模板" + file.FullName + "错误!");
             }
         }
 
         foreach (var directory in directoryInfo.GetDirectories())
         {
             GetTemplates(directory);
         }
     }
 </script>
 
 <% this.Generate(); %>

二、定义子模版

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="tableName" %>
<%@ Property Name="FileNameFormat" Type="String" Category="Context" Default="{0}.cs" Description="FileName FormatStr"  Optional="True" %>
<%@ Property Name="NameSpace" Type="System.String" Default="" Category="NameSpace" Description="NameSpace"  Optional="True"  %>

<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

using System;
using ECRF.Infrastructure.Attributes;
using ECRF.Infrastructure.Entities;

namespace ECRF.Models.<%= string.IsNullOrEmpty(NameSpace)?"":NameSpace + "." %>Entities
{
    ///<summary>
    ///<%= StringUtil.ToPascalCase(SourceTable.Name) %>的实体类,描述:<%=SourceTable.Description%>
    ///</summary>
    [PhysicData(Table = "<%=SourceTable.Name%>", Key = "<%= SourceTable.PrimaryKey.MemberColumns.FirstOrDefault().Name %>")]
    public class <%= StringUtil.ToPascalCase(SourceTable.Name) %>: Entity<int>
    {
    <% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
        /// <summary>
        /// <%= column.Description %>
        /// </summary>
        public <%= CSharpAlias[column.SystemType.FullName].Replace("System.","") %> <%= column.Name %> { get; set; }
        
    <% } %>
    }
}

其他子模版可参照第二点进行定义。

posted @ 2023-08-02 18:18  潇潇与偕  阅读(118)  评论(0)    收藏  举报