You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Commons.Collections;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称LAPTOP-E0N2L34V
* 命名空间SlnMesnac.Generate.Templates
* 唯一标识4acc596a-6223-4156-b16c-952c225eff25
*
* 创建者WenJY
* 电子邮箱wenjy@mesnac.com
* 创建时间2024-04-11 13:27:33
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Generate.Templates.Service.Impl
{
public class ServiceCreate
{
private static readonly string templateDir = @"E:\桌面\SlnMesnac\SlnMesnac.Generate\Templates\Service\Impl\";
public bool Create(string tableName, string NameSpace, string outdir)
{
try
{
VelocityEngine velocityEngine = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, @"file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir);
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
//模板的缓存设置
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存
props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒)
velocityEngine.Init(props);
//为模板变量赋值
VelocityContext context = new VelocityContext();
context.Put("tableName", tableName);
context.Put("NameSpace", NameSpace);
context.Put("outdir", outdir);
//从文件中读取模板
Template template = velocityEngine.GetTemplate(@"\Services.vm");
if (!Directory.Exists(outdir + "\\Services"))
{
Directory.CreateDirectory(outdir + "\\Services");
}
//合并模板
using (StreamWriter writer = new StreamWriter(outdir + $"\\Services\\{tableName.Substring(0, 1).ToUpper()}{tableName.Substring(1)}ServiceImpl.cs", false))
{
template.Merge(context, writer);
}
return true;
}catch(Exception ex)
{
throw new InvalidOperationException($"Service实现类模板创建异常:{ex.Message}");
}
}
}
}