优化代码生成
This commit is contained in:
parent
78aa48a975
commit
e16997a204
@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -39,5 +41,139 @@ namespace Infrastructure
|
|||||||
MD5CryptoServiceProvider md5 = new();
|
MD5CryptoServiceProvider md5 = new();
|
||||||
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除指定目录下的所有文件及文件夹(保留目录)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file">文件目录</param>
|
||||||
|
public static void DeleteDirectory(string file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//判断文件夹是否还存在
|
||||||
|
if (Directory.Exists(file))
|
||||||
|
{
|
||||||
|
DirectoryInfo fileInfo = new DirectoryInfo(file);
|
||||||
|
//去除文件夹的只读属性
|
||||||
|
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
|
||||||
|
foreach (string f in Directory.GetFileSystemEntries(file))
|
||||||
|
{
|
||||||
|
if (File.Exists(f))
|
||||||
|
{
|
||||||
|
//去除文件的只读属性
|
||||||
|
File.SetAttributes(file, FileAttributes.Normal);
|
||||||
|
//如果有子文件删除文件
|
||||||
|
File.Delete(f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//循环递归删除子文件夹
|
||||||
|
DeleteDirectory(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//删除空文件夹
|
||||||
|
Directory.Delete(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex) // 异常处理
|
||||||
|
{
|
||||||
|
Console.WriteLine("代码生成异常" + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 压缩代码
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="zipPath"></param>
|
||||||
|
/// <param name="genCodePath"></param>
|
||||||
|
/// <param name="zipFileName">压缩后的文件名</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool ZipGenCode(string zipPath, string genCodePath, string zipFileName)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(zipPath)) return false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CreateDirectory(genCodePath);
|
||||||
|
string zipFileFullName = Path.Combine(zipPath, zipFileName);
|
||||||
|
if (File.Exists(zipFileFullName))
|
||||||
|
{
|
||||||
|
File.Delete(zipFileFullName);
|
||||||
|
}
|
||||||
|
|
||||||
|
ZipFile.CreateFromDirectory(genCodePath, zipFileFullName);
|
||||||
|
DeleteDirectory(genCodePath);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("压缩文件出错。" + ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建文件夹
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool CreateDirectory(string path)
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||||
|
{
|
||||||
|
path = path.Replace("\\", "/").Replace("//", "/");
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
{
|
||||||
|
DirectoryInfo info = Directory.CreateDirectory(path);
|
||||||
|
Console.WriteLine("不存在创建文件夹" + info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"创建文件夹出错了,{ex.Message}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 写文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">完整路径带扩展名的</param>
|
||||||
|
/// <param name="content"></param>
|
||||||
|
public static void WriteAndSave(string path, string content)
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||||
|
{
|
||||||
|
path = path.Replace("\\", "/").Replace("//", "/");
|
||||||
|
}
|
||||||
|
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||||
|
}
|
||||||
|
Console.WriteLine("开始写入文件,Path=" + path);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//实例化一个文件流--->与写入文件相关联
|
||||||
|
using var fs = new FileStream(path, FileMode.Create, FileAccess.Write);
|
||||||
|
//实例化一个StreamWriter-->与fs相关联
|
||||||
|
using var sw = new StreamWriter(fs);
|
||||||
|
//开始写入
|
||||||
|
sw.Write(content);
|
||||||
|
//清空缓冲区
|
||||||
|
sw.Flush();
|
||||||
|
//关闭流
|
||||||
|
sw.Close();
|
||||||
|
fs.Close();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("写入文件出错了:" + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -284,7 +284,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
//生成代码到指定文件夹
|
//生成代码到指定文件夹
|
||||||
CodeGeneratorTool.Generate(dto);
|
CodeGeneratorTool.Generate(dto);
|
||||||
//下载文件
|
//下载文件
|
||||||
FileHelper.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName);
|
FileUtil.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName);
|
||||||
|
|
||||||
return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName });
|
return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ZR.CodeGenerator.Model;
|
using ZR.CodeGenerator.Model;
|
||||||
|
using ZR.Common;
|
||||||
using ZR.Model.System.Generate;
|
using ZR.Model.System.Generate;
|
||||||
|
|
||||||
namespace ZR.CodeGenerator
|
namespace ZR.CodeGenerator
|
||||||
@ -17,6 +18,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class CodeGeneratorTool
|
public class CodeGeneratorTool
|
||||||
{
|
{
|
||||||
|
private static readonly string CodeTemplateDir = "CodeGenTemplate";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 代码生成器配置
|
/// 代码生成器配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -104,7 +106,7 @@ namespace ZR.CodeGenerator
|
|||||||
foreach (var item in dto.GenCodes)
|
foreach (var item in dto.GenCodes)
|
||||||
{
|
{
|
||||||
item.Path = Path.Combine(dto.GenCodePath, item.Path);
|
item.Path = Path.Combine(dto.GenCodePath, item.Path);
|
||||||
FileHelper.WriteAndSave(item.Path, item.Content);
|
FileUtil.WriteAndSave(item.Path, item.Content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +119,8 @@ namespace ZR.CodeGenerator
|
|||||||
/// <param name="replaceDto">替换实体</param>
|
/// <param name="replaceDto">替换实体</param>
|
||||||
private static void GenerateModels(ReplaceDto replaceDto, GenerateDto generateDto)
|
private static void GenerateModels(ReplaceDto replaceDto, GenerateDto generateDto)
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TplModel.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplModel.txt");
|
||||||
var tplDto = FileHelper.ReadJtTemplate("TplDto.txt");
|
var tplDto = JnHelper.ReadTemplate(CodeTemplateDir, "TplDto.txt");
|
||||||
|
|
||||||
string fullPath = Path.Combine(_option.ModelsNamespace, "Models", _option.SubNamespace, replaceDto.ModelTypeName + ".cs");
|
string fullPath = Path.Combine(_option.ModelsNamespace, "Models", _option.SubNamespace, replaceDto.ModelTypeName + ".cs");
|
||||||
string fullPathDto = Path.Combine(_option.ModelsNamespace, "Dto", _option.SubNamespace, $"{replaceDto.ModelTypeName}Dto.cs");
|
string fullPathDto = Path.Combine(_option.ModelsNamespace, "Dto", _option.SubNamespace, $"{replaceDto.ModelTypeName}Dto.cs");
|
||||||
@ -134,7 +136,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// <param name="replaceDto">替换实体</param>
|
/// <param name="replaceDto">替换实体</param>
|
||||||
private static void GenerateRepository(ReplaceDto replaceDto, GenerateDto generateDto)
|
private static void GenerateRepository(ReplaceDto replaceDto, GenerateDto generateDto)
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TplRepository.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplRepository.txt");
|
||||||
var result = tpl.Render();
|
var result = tpl.Render();
|
||||||
var fullPath = Path.Combine(_option.RepositoriesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Repository.cs");
|
var fullPath = Path.Combine(_option.RepositoriesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Repository.cs");
|
||||||
|
|
||||||
@ -146,16 +148,14 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static void GenerateService(ReplaceDto replaceDto, GenerateDto generateDto)
|
private static void GenerateService(ReplaceDto replaceDto, GenerateDto generateDto)
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TplService.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplService.txt");
|
||||||
var tpl2 = FileHelper.ReadJtTemplate("TplIService.txt");
|
var tpl2 = JnHelper.ReadTemplate(CodeTemplateDir, "TplIService.txt");
|
||||||
var result = tpl.Render();
|
|
||||||
var result2 = tpl2.Render();
|
|
||||||
|
|
||||||
var fullPath = Path.Combine(_option.ServicesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Service.cs");
|
var fullPath = Path.Combine(_option.ServicesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Service.cs");
|
||||||
var fullPath2 = Path.Combine(_option.IServicsNamespace, _option.SubNamespace, $"I{_option.SubNamespace}Service", $"I{replaceDto.ModelTypeName}Service.cs");
|
var fullPath2 = Path.Combine(_option.IServicsNamespace, _option.SubNamespace, $"I{_option.SubNamespace}Service", $"I{replaceDto.ModelTypeName}Service.cs");
|
||||||
|
|
||||||
generateDto.GenCodes.Add(new GenCode(4, "Service.cs", fullPath, result));
|
generateDto.GenCodes.Add(new GenCode(4, "Service.cs", fullPath, tpl.Render()));
|
||||||
generateDto.GenCodes.Add(new GenCode(4, "IService.cs", fullPath2, result2));
|
generateDto.GenCodes.Add(new GenCode(4, "IService.cs", fullPath2, tpl2.Render()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -163,7 +163,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static void GenerateControllers(ReplaceDto replaceDto, GenerateDto generateDto)
|
private static void GenerateControllers(ReplaceDto replaceDto, GenerateDto generateDto)
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TplControllers.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplControllers.txt");
|
||||||
tpl.Set("QueryCondition", replaceDto.QueryCondition);
|
tpl.Set("QueryCondition", replaceDto.QueryCondition);
|
||||||
var result = tpl.Render();
|
var result = tpl.Render();
|
||||||
|
|
||||||
@ -196,7 +196,7 @@ namespace ZR.CodeGenerator
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var tpl = FileHelper.ReadJtTemplate(fileName);
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, fileName);
|
||||||
tpl.Set("vueQueryFormHtml", replaceDto.VueQueryFormHtml);
|
tpl.Set("vueQueryFormHtml", replaceDto.VueQueryFormHtml);
|
||||||
tpl.Set("VueViewFormContent", replaceDto.VueViewFormHtml);//添加、修改表单
|
tpl.Set("VueViewFormContent", replaceDto.VueViewFormHtml);//添加、修改表单
|
||||||
tpl.Set("VueViewListContent", replaceDto.VueViewListHtml);//查询 table列
|
tpl.Set("VueViewListContent", replaceDto.VueViewListHtml);//查询 table列
|
||||||
@ -224,7 +224,7 @@ namespace ZR.CodeGenerator
|
|||||||
_ => "Vue.txt",
|
_ => "Vue.txt",
|
||||||
};
|
};
|
||||||
fileName = Path.Combine("v3", fileName);
|
fileName = Path.Combine("v3", fileName);
|
||||||
var tpl = FileHelper.ReadJtTemplate(fileName);
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, fileName);
|
||||||
tpl.Set("treeCode", generateDto.GenTable?.Options?.TreeCode?.FirstLowerCase());
|
tpl.Set("treeCode", generateDto.GenTable?.Options?.TreeCode?.FirstLowerCase());
|
||||||
tpl.Set("treeName", generateDto.GenTable?.Options?.TreeName?.FirstLowerCase());
|
tpl.Set("treeName", generateDto.GenTable?.Options?.TreeName?.FirstLowerCase());
|
||||||
tpl.Set("treeParentCode", generateDto.GenTable?.Options?.TreeParentCode?.FirstLowerCase());
|
tpl.Set("treeParentCode", generateDto.GenTable?.Options?.TreeParentCode?.FirstLowerCase());
|
||||||
@ -242,7 +242,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static void GenerateVueJs(ReplaceDto replaceDto, GenerateDto generateDto)
|
public static void GenerateVueJs(ReplaceDto replaceDto, GenerateDto generateDto)
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TplVueApi.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplVueApi.txt");
|
||||||
var result = tpl.Render();
|
var result = tpl.Render();
|
||||||
|
|
||||||
string fileName;
|
string fileName;
|
||||||
@ -279,7 +279,7 @@ namespace ZR.CodeGenerator
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var tpl = FileHelper.ReadJtTemplate($"{tempName}.txt");
|
var tpl = JnHelper.ReadTemplate(CodeTemplateDir, $"{tempName}.txt");
|
||||||
tpl.Set("parentId", generateDto.GenTable?.Options?.ParentMenuId ?? 0);
|
tpl.Set("parentId", generateDto.GenTable?.Options?.ParentMenuId ?? 0);
|
||||||
var result = tpl.Render();
|
var result = tpl.Render();
|
||||||
string fullPath = Path.Combine(generateDto.GenCodePath, "sql", generateDto.GenTable.BusinessName + ".sql");
|
string fullPath = Path.Combine(generateDto.GenCodePath, "sql", generateDto.GenTable.BusinessName + ".sql");
|
||||||
@ -293,30 +293,25 @@ namespace ZR.CodeGenerator
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GenerateVueQueryForm()
|
public static string GenerateVueQueryForm()
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("QueryForm.txt");
|
return JnHelper.ReadTemplate(CodeTemplateDir, "QueryForm.txt").Render();
|
||||||
var result = tpl.Render();
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成vue页面table
|
/// 生成vue页面table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GenerateVueTableList()
|
public static string GenerateVueTableList()
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("TableList.txt");
|
return JnHelper.ReadTemplate(CodeTemplateDir, "TableList.txt").Render();
|
||||||
var result = tpl.Render();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成vue表单
|
/// 生成vue表单
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GenerateCurdForm()
|
public static string GenerateCurdForm()
|
||||||
{
|
{
|
||||||
var tpl = FileHelper.ReadJtTemplate("CurdForm.txt");
|
return JnHelper.ReadTemplate(CodeTemplateDir, "CurdForm.txt").Render();
|
||||||
var result = tpl.Render();
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -534,7 +529,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// <param name="replaceDto"></param>
|
/// <param name="replaceDto"></param>
|
||||||
private static void InitJntTemplate(GenerateDto dto, ReplaceDto replaceDto)
|
private static void InitJntTemplate(GenerateDto dto, ReplaceDto replaceDto)
|
||||||
{
|
{
|
||||||
Engine.Current.Clean();
|
//Engine.Current.Clean();
|
||||||
dto.GenTable.Columns = dto.GenTable.Columns.OrderBy(x => x.Sort).ToList();
|
dto.GenTable.Columns = dto.GenTable.Columns.OrderBy(x => x.Sort).ToList();
|
||||||
bool showCustomInput = dto.GenTable.Columns.Any(f => f.HtmlType.Equals(GenConstants.HTML_CUSTOM_INPUT, StringComparison.OrdinalIgnoreCase));
|
bool showCustomInput = dto.GenTable.Columns.Any(f => f.HtmlType.Equals(GenConstants.HTML_CUSTOM_INPUT, StringComparison.OrdinalIgnoreCase));
|
||||||
//jnt模板引擎全局变量
|
//jnt模板引擎全局变量
|
||||||
@ -554,7 +549,6 @@ namespace ZR.CodeGenerator
|
|||||||
options.Data.Set("replaceDto", replaceDto);
|
options.Data.Set("replaceDto", replaceDto);
|
||||||
options.Data.Set("options", dto.GenOptions);
|
options.Data.Set("options", dto.GenOptions);
|
||||||
options.Data.Set("genTable", dto.GenTable);
|
options.Data.Set("genTable", dto.GenTable);
|
||||||
//options.Data.Set("btns", dto.CheckedBtn);
|
|
||||||
options.Data.Set("showCustomInput", showCustomInput);
|
options.Data.Set("showCustomInput", showCustomInput);
|
||||||
options.Data.Set("tool", new CodeGeneratorTool());
|
options.Data.Set("tool", new CodeGeneratorTool());
|
||||||
options.Data.Set("codeTool", new CodeGenerateTemplate());
|
options.Data.Set("codeTool", new CodeGenerateTemplate());
|
||||||
|
|||||||
@ -1,190 +0,0 @@
|
|||||||
using JinianNet.JNTemplate;
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.IO.Compression;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace ZR.CodeGenerator
|
|
||||||
{
|
|
||||||
public class FileHelper
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 创建文件夹
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static bool CreateDirectory(string path)
|
|
||||||
{
|
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
||||||
{
|
|
||||||
path = path.Replace("\\", "/").Replace("//", "/");
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!Directory.Exists(path))
|
|
||||||
{
|
|
||||||
DirectoryInfo info = Directory.CreateDirectory(path);
|
|
||||||
Console.WriteLine("不存在创建文件夹" + info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"创建文件夹出错了,{ex.Message}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 写文件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">完整路径带扩展名的</param>
|
|
||||||
/// <param name="content"></param>
|
|
||||||
public static void WriteAndSave(string path, string content)
|
|
||||||
{
|
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
||||||
{
|
|
||||||
path = path.Replace("\\", "/").Replace("//", "/");
|
|
||||||
}
|
|
||||||
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
||||||
}
|
|
||||||
Console.WriteLine("写入文件:" + path);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//实例化一个文件流--->与写入文件相关联
|
|
||||||
using var fs = new FileStream(path, FileMode.Create, FileAccess.Write);
|
|
||||||
//实例化一个StreamWriter-->与fs相关联
|
|
||||||
using var sw = new StreamWriter(fs);
|
|
||||||
//开始写入
|
|
||||||
sw.Write(content);
|
|
||||||
//清空缓冲区
|
|
||||||
sw.Flush();
|
|
||||||
//关闭流
|
|
||||||
sw.Close();
|
|
||||||
fs.Close();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine("写入文件出错了:" + ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 从代码模板中读取内容
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="templateName">模板名称,应包括文件扩展名称。比如:template.txt</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string ReadTemplate(string tplName)
|
|
||||||
{
|
|
||||||
string path = Environment.CurrentDirectory;
|
|
||||||
string fullName = Path.Combine(path, "wwwroot", "CodeGenTemplate", tplName);
|
|
||||||
|
|
||||||
Console.WriteLine("开始读取模板=" + fullName);
|
|
||||||
string temp = fullName;
|
|
||||||
string str = "";
|
|
||||||
if (!File.Exists(temp))
|
|
||||||
{
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
StreamReader sr = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
sr = new StreamReader(temp);
|
|
||||||
str = sr.ReadToEnd(); // 读取文件
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"读取模板出错了{ex.Message}");
|
|
||||||
}
|
|
||||||
sr?.Close();
|
|
||||||
sr?.Dispose();
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ITemplate ReadJtTemplate(string tplName)
|
|
||||||
{
|
|
||||||
string path = Environment.CurrentDirectory;
|
|
||||||
string fullName = Path.Combine(path, "wwwroot", "CodeGenTemplate", tplName);
|
|
||||||
if (File.Exists(fullName))
|
|
||||||
{
|
|
||||||
return Engine.LoadTemplate(fullName);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 压缩代码
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="zipPath"></param>
|
|
||||||
/// <param name="genCodePath"></param>
|
|
||||||
/// <param name="zipFileName">压缩后的文件名</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static bool ZipGenCode(string zipPath, string genCodePath,string zipFileName)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(zipPath)) return false;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
CreateDirectory(genCodePath);
|
|
||||||
string zipFileFullName = Path.Combine(zipPath, zipFileName);
|
|
||||||
if (File.Exists(zipFileFullName))
|
|
||||||
{
|
|
||||||
File.Delete(zipFileFullName);
|
|
||||||
}
|
|
||||||
|
|
||||||
ZipFile.CreateFromDirectory(genCodePath, zipFileFullName);
|
|
||||||
DeleteDirectory(genCodePath);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine("压缩文件出错。" + ex.Message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 删除指定目录下的所有文件及文件夹(保留目录)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="file">文件目录</param>
|
|
||||||
public static void DeleteDirectory(string file)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//判断文件夹是否还存在
|
|
||||||
if (Directory.Exists(file))
|
|
||||||
{
|
|
||||||
DirectoryInfo fileInfo = new DirectoryInfo(file);
|
|
||||||
//去除文件夹的只读属性
|
|
||||||
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
|
|
||||||
foreach (string f in Directory.GetFileSystemEntries(file))
|
|
||||||
{
|
|
||||||
if (File.Exists(f))
|
|
||||||
{
|
|
||||||
//去除文件的只读属性
|
|
||||||
File.SetAttributes(file, FileAttributes.Normal);
|
|
||||||
//如果有子文件删除文件
|
|
||||||
File.Delete(f);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//循环递归删除子文件夹
|
|
||||||
DeleteDirectory(f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//删除空文件夹
|
|
||||||
Directory.Delete(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex) // 异常处理
|
|
||||||
{
|
|
||||||
Console.WriteLine("代码生成异常" + ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
26
ZR.Common/JnHelper.cs
Normal file
26
ZR.Common/JnHelper.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using JinianNet.JNTemplate;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace ZR.Common
|
||||||
|
{
|
||||||
|
public class JnHelper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 读取Jn模板
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dirPath"></param>
|
||||||
|
/// <param name="tplName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ITemplate ReadTemplate(string dirPath, string tplName)
|
||||||
|
{
|
||||||
|
string path = Environment.CurrentDirectory;
|
||||||
|
string fullName = Path.Combine(path, "wwwroot", dirPath, tplName);
|
||||||
|
if (File.Exists(fullName))
|
||||||
|
{
|
||||||
|
return Engine.LoadTemplate(fullName);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@
|
|||||||
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
|
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
|
||||||
<PackageReference Include="CSRedisCore" Version="3.8.2" />
|
<PackageReference Include="CSRedisCore" Version="3.8.2" />
|
||||||
<PackageReference Include="EPPlus" Version="6.0.4" />
|
<PackageReference Include="EPPlus" Version="6.0.4" />
|
||||||
|
<PackageReference Include="JinianNet.JNTemplate" Version="2.3.0" />
|
||||||
<PackageReference Include="MailKit" Version="3.3.0" />
|
<PackageReference Include="MailKit" Version="3.3.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user