From e16997a204d488bd5b0f6f36779f5265bd3af968 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Tue, 5 Jul 2022 18:04:59 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=94=9F?=
=?UTF-8?q?=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Infrastructure/Helper/FileUtil.cs | 136 +++++++++++++
.../System/CodeGeneratorController.cs | 2 +-
ZR.CodeGenerator/CodeGeneratorTool.cs | 48 ++---
ZR.CodeGenerator/FileHelper.cs | 190 ------------------
ZR.Common/JnHelper.cs | 26 +++
ZR.Common/ZR.Common.csproj | 1 +
6 files changed, 185 insertions(+), 218 deletions(-)
delete mode 100644 ZR.CodeGenerator/FileHelper.cs
create mode 100644 ZR.Common/JnHelper.cs
diff --git a/Infrastructure/Helper/FileUtil.cs b/Infrastructure/Helper/FileUtil.cs
index 8448ba2..2460bcf 100644
--- a/Infrastructure/Helper/FileUtil.cs
+++ b/Infrastructure/Helper/FileUtil.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.IO.Compression;
+using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
@@ -39,5 +41,139 @@ namespace Infrastructure
MD5CryptoServiceProvider md5 = new();
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
}
+
+ ///
+ /// 删除指定目录下的所有文件及文件夹(保留目录)
+ ///
+ /// 文件目录
+ 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);
+ }
+ }
+
+ ///
+ /// 压缩代码
+ ///
+ ///
+ ///
+ /// 压缩后的文件名
+ ///
+ 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;
+ }
+ }
+
+ ///
+ /// 创建文件夹
+ ///
+ ///
+ ///
+ 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;
+ }
+
+ ///
+ /// 写文件
+ ///
+ /// 完整路径带扩展名的
+ ///
+ 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);
+ }
+ }
}
}
diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
index 20e0a00..75621ad 100644
--- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
@@ -284,7 +284,7 @@ namespace ZR.Admin.WebApi.Controllers
//生成代码到指定文件夹
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 });
}
diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs
index 8ec02cc..30a4a4d 100644
--- a/ZR.CodeGenerator/CodeGeneratorTool.cs
+++ b/ZR.CodeGenerator/CodeGeneratorTool.cs
@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using ZR.CodeGenerator.Model;
+using ZR.Common;
using ZR.Model.System.Generate;
namespace ZR.CodeGenerator
@@ -17,6 +18,7 @@ namespace ZR.CodeGenerator
///
public class CodeGeneratorTool
{
+ private static readonly string CodeTemplateDir = "CodeGenTemplate";
///
/// 代码生成器配置
///
@@ -104,7 +106,7 @@ namespace ZR.CodeGenerator
foreach (var item in dto.GenCodes)
{
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
/// 替换实体
private static void GenerateModels(ReplaceDto replaceDto, GenerateDto generateDto)
{
- var tpl = FileHelper.ReadJtTemplate("TplModel.txt");
- var tplDto = FileHelper.ReadJtTemplate("TplDto.txt");
+ var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplModel.txt");
+ var tplDto = JnHelper.ReadTemplate(CodeTemplateDir, "TplDto.txt");
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");
@@ -134,7 +136,7 @@ namespace ZR.CodeGenerator
/// 替换实体
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 fullPath = Path.Combine(_option.RepositoriesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Repository.cs");
@@ -146,16 +148,14 @@ namespace ZR.CodeGenerator
///
private static void GenerateService(ReplaceDto replaceDto, GenerateDto generateDto)
{
- var tpl = FileHelper.ReadJtTemplate("TplService.txt");
- var tpl2 = FileHelper.ReadJtTemplate("TplIService.txt");
- var result = tpl.Render();
- var result2 = tpl2.Render();
+ var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplService.txt");
+ var tpl2 = JnHelper.ReadTemplate(CodeTemplateDir, "TplIService.txt");
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");
- generateDto.GenCodes.Add(new GenCode(4, "Service.cs", fullPath, result));
- generateDto.GenCodes.Add(new GenCode(4, "IService.cs", fullPath2, result2));
+ generateDto.GenCodes.Add(new GenCode(4, "Service.cs", fullPath, tpl.Render()));
+ generateDto.GenCodes.Add(new GenCode(4, "IService.cs", fullPath2, tpl2.Render()));
}
///
@@ -163,7 +163,7 @@ namespace ZR.CodeGenerator
///
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);
var result = tpl.Render();
@@ -196,7 +196,7 @@ namespace ZR.CodeGenerator
default:
break;
}
- var tpl = FileHelper.ReadJtTemplate(fileName);
+ var tpl = JnHelper.ReadTemplate(CodeTemplateDir, fileName);
tpl.Set("vueQueryFormHtml", replaceDto.VueQueryFormHtml);
tpl.Set("VueViewFormContent", replaceDto.VueViewFormHtml);//添加、修改表单
tpl.Set("VueViewListContent", replaceDto.VueViewListHtml);//查询 table列
@@ -224,7 +224,7 @@ namespace ZR.CodeGenerator
_ => "Vue.txt",
};
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("treeName", generateDto.GenTable?.Options?.TreeName?.FirstLowerCase());
tpl.Set("treeParentCode", generateDto.GenTable?.Options?.TreeParentCode?.FirstLowerCase());
@@ -242,7 +242,7 @@ namespace ZR.CodeGenerator
///
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();
string fileName;
@@ -279,7 +279,7 @@ namespace ZR.CodeGenerator
default:
break;
}
- var tpl = FileHelper.ReadJtTemplate($"{tempName}.txt");
+ var tpl = JnHelper.ReadTemplate(CodeTemplateDir, $"{tempName}.txt");
tpl.Set("parentId", generateDto.GenTable?.Options?.ParentMenuId ?? 0);
var result = tpl.Render();
string fullPath = Path.Combine(generateDto.GenCodePath, "sql", generateDto.GenTable.BusinessName + ".sql");
@@ -293,30 +293,25 @@ namespace ZR.CodeGenerator
///
public static string GenerateVueQueryForm()
{
- var tpl = FileHelper.ReadJtTemplate("QueryForm.txt");
- var result = tpl.Render();
- return result;
+ return JnHelper.ReadTemplate(CodeTemplateDir, "QueryForm.txt").Render();
}
+
///
/// 生成vue页面table
///
///
public static string GenerateVueTableList()
{
- var tpl = FileHelper.ReadJtTemplate("TableList.txt");
- var result = tpl.Render();
-
- return result;
+ return JnHelper.ReadTemplate(CodeTemplateDir, "TableList.txt").Render();
}
+
///
/// 生成vue表单
///
///
public static string GenerateCurdForm()
{
- var tpl = FileHelper.ReadJtTemplate("CurdForm.txt");
- var result = tpl.Render();
- return result;
+ return JnHelper.ReadTemplate(CodeTemplateDir, "CurdForm.txt").Render();
}
#endregion
@@ -534,7 +529,7 @@ namespace ZR.CodeGenerator
///
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();
bool showCustomInput = dto.GenTable.Columns.Any(f => f.HtmlType.Equals(GenConstants.HTML_CUSTOM_INPUT, StringComparison.OrdinalIgnoreCase));
//jnt模板引擎全局变量
@@ -554,7 +549,6 @@ namespace ZR.CodeGenerator
options.Data.Set("replaceDto", replaceDto);
options.Data.Set("options", dto.GenOptions);
options.Data.Set("genTable", dto.GenTable);
- //options.Data.Set("btns", dto.CheckedBtn);
options.Data.Set("showCustomInput", showCustomInput);
options.Data.Set("tool", new CodeGeneratorTool());
options.Data.Set("codeTool", new CodeGenerateTemplate());
diff --git a/ZR.CodeGenerator/FileHelper.cs b/ZR.CodeGenerator/FileHelper.cs
deleted file mode 100644
index 47a992d..0000000
--- a/ZR.CodeGenerator/FileHelper.cs
+++ /dev/null
@@ -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
- {
- ///
- /// 创建文件夹
- ///
- ///
- ///
- 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;
- }
-
- ///
- /// 写文件
- ///
- /// 完整路径带扩展名的
- ///
- 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);
- }
- }
-
-
- ///
- /// 从代码模板中读取内容
- ///
- /// 模板名称,应包括文件扩展名称。比如:template.txt
- ///
- 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;
- }
-
- ///
- /// 压缩代码
- ///
- ///
- ///
- /// 压缩后的文件名
- ///
- 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;
- }
- }
-
- ///
- /// 删除指定目录下的所有文件及文件夹(保留目录)
- ///
- /// 文件目录
- 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);
- }
- }
-
- }
-}
diff --git a/ZR.Common/JnHelper.cs b/ZR.Common/JnHelper.cs
new file mode 100644
index 0000000..c0a3b28
--- /dev/null
+++ b/ZR.Common/JnHelper.cs
@@ -0,0 +1,26 @@
+using JinianNet.JNTemplate;
+using System;
+using System.IO;
+
+namespace ZR.Common
+{
+ public class JnHelper
+ {
+ ///
+ /// 读取Jn模板
+ ///
+ ///
+ ///
+ ///
+ 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;
+ }
+ }
+}
diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj
index be78c30..c737253 100644
--- a/ZR.Common/ZR.Common.csproj
+++ b/ZR.Common/ZR.Common.csproj
@@ -8,6 +8,7 @@
+