Fix the t4 template reporting an error problem after creating its own project

修复 t4 模板在创建自己项目后报错问题,中文字符格式错误
This commit is contained in:
anjoy 2019-12-10 17:02:57 +08:00
parent ec76f8d5a6
commit fcaeefeeb3
3 changed files with 7 additions and 63 deletions

View File

@ -167,60 +167,33 @@
}
#region DbTable
/// <summary>
/// 表结构
/// </summary>
public sealed class DbTable
{
/// <summary>
/// 表名称
/// </summary>
public string TableName { get; set; }
/// <summary>
/// 表的架构
/// </summary>
public string SchemaName { get; set; }
/// <summary>
/// 表的记录数
/// </summary>
public int Rows { get; set; }
/// <summary>
/// 是否含有主键
/// </summary>
public bool HasPrimaryKey { get; set; }
}
#endregion
#region DbColumn
/// <summary>
/// 表字段结构
/// </summary>
public sealed class DbColumn
{
/// <summary>
/// 字段ID
/// </summary>
public int ColumnID { get; set; }
/// <summary>
/// 是否主键
/// </summary>
public bool IsPrimaryKey { get; set; }
/// <summary>
/// 字段名称
/// </summary>
public string ColumnName { get; set; }
/// <summary>
/// 字段类型
/// </summary>
public string ColumnType { get; set; }
/// <summary>
/// 数据库类型对应的C#类型
/// </summary>
public string CSharpType
{
get
@ -240,36 +213,17 @@
}
}
/// <summary>
/// 字节长度
/// </summary>
public int ByteLength { get; set; }
/// <summary>
/// 字符长度
/// </summary>
public int CharLength { get; set; }
///精度长度
public int Precision{get;set;}
/// <summary>
/// 小数位
/// </summary>
public int Scale { get; set; }
/// <summary>
/// 是否自增列
/// </summary>
public bool IsIdentity { get; set; }
/// <summary>
/// 是否允许空
/// </summary>
public bool IsNullable { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Remark { get; set; }
}
#endregion

View File

@ -5,10 +5,8 @@
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#+
//定义管理者 manager 实体类
class Manager
{
//定义一个 block 块,主要是应用在批量生产中
public struct Block {
public int Start, Length;
public String Name,OutputPath;
@ -21,13 +19,11 @@ class Manager
public ITextTemplatingEngineHost host;
public ManagementStrategy strategy;
public StringBuilder template;
//构造函数,包含 host主机模板输出路径创建管理策略
public Manager(ITextTemplatingEngineHost host, StringBuilder template, bool commonHeader) {
this.host = host;
this.template = template;
strategy = ManagementStrategy.Create(host);
}
//开辟一个 block 块
public void StartBlock(String name,String outputPath) {
currentBlock = new Block { Name = name, Start = template.Length ,OutputPath=outputPath};
}
@ -52,13 +48,11 @@ class Manager
currentBlock.Length = template.Length - currentBlock.Start;
blocks.Add(currentBlock);
}
//定义进程,用来将所有的 blocks 块执行出来
public void Process(bool split) {
String header = template.ToString(headerBlock.Start, headerBlock.Length);
String footer = template.ToString(footerBlock.Start, footerBlock.Length);
blocks.Reverse();
foreach(Block block in blocks) {//遍历
//输出文件
foreach(Block block in blocks) {
String fileName = Path.Combine(block.OutputPath, block.Name);
if (split) {
String content = header + template.ToString(block.Start, block.Length) + footer;
@ -70,7 +64,6 @@ class Manager
}
}
}
//定义管理策略类
class ManagementStrategy
{
internal static ManagementStrategy Create(ITextTemplatingEngineHost host) {
@ -104,16 +97,13 @@ class VSManagementStrategy : ManagementStrategy
templateProjectItem = dte.Solution.FindProjectItem(host.TemplateFile);
}
//创建文件
internal override void CreateFile(String fileName, String content) {
base.CreateFile(fileName, content);
//((EventHandler)delegate { templateProjectItem.ProjectItems.AddFromFile(fileName); }).BeginInvoke(null, null, null, null);
}
//删除文件
internal override void DeleteFile(String fileName) {
((EventHandler)delegate { FindAndDeleteFile(fileName); }).BeginInvoke(null, null, null, null);
}
//根据文件名删除文件
private void FindAndDeleteFile(String fileName) {
foreach(EnvDTE.ProjectItem projectItem in templateProjectItem.ProjectItems) {
if (projectItem.get_FileNames(0) == fileName) {