feat: 完成yi-abp模块生成

This commit is contained in:
橙子
2024-06-02 14:12:37 +08:00
parent bca3ce1e5f
commit d9c9db332c
5 changed files with 37 additions and 18 deletions

View File

@@ -6,7 +6,7 @@ namespace Yi.Abp.Tool.Application.Contracts
{
public interface ITemplateGenService: IApplicationService
{
Task<IActionResult> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto);
Task<IActionResult> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto);
Task<byte[]> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto);
Task<byte[]> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto);
}
}

View File

@@ -25,7 +25,7 @@ namespace Yi.Abp.Tool.Application
/// 下载模块文件
/// </summary>
/// <returns></returns>
public async Task<IActionResult> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto)
public async Task<byte[]> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto)
{
moduleCreateInputDto.SetNameReplace();
@@ -33,17 +33,17 @@ namespace Yi.Abp.Tool.Application
input.SetTemplateFilePath(_templateGenManager._toolOptions.ModuleTemplateFilePath);
var filePath = await _templateGenManager.CreateTemplateAsync(input);
//考虑从路径中获取
var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
////考虑从路径中获取
//var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
//设置附件下载,下载名称
return new FileContentResult(await File.ReadAllBytesAsync(filePath), fileContentType);
return await File.ReadAllBytesAsync(filePath);
}
/// <summary>
/// 下载模块文件
/// </summary>
/// <returns></returns>
public async Task<IActionResult> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto)
public async Task<byte[]> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto)
{
moduleCreateInputDto.SetNameReplace();
@@ -52,9 +52,9 @@ namespace Yi.Abp.Tool.Application
var filePath = await _templateGenManager.CreateTemplateAsync(input);
//考虑从路径中获取
var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
// var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
//设置附件下载,下载名称
return new FileContentResult(await File.ReadAllBytesAsync(filePath), fileContentType);
return await File.ReadAllBytesAsync(filePath);
}
}
}

View File

@@ -19,6 +19,7 @@
<ItemGroup>
<Folder Include="wwwroot\" />
<Folder Include="wwwroot\temp\" />
</ItemGroup>
</Project>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -22,6 +23,7 @@ namespace Yi.Abp.Tool.Commands
public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
{
var id = Guid.NewGuid().ToString("N");
//只有一个new
if (args.Length <= 1)
{
@@ -30,22 +32,22 @@ namespace Yi.Abp.Tool.Commands
string name = args[1];
options.TryGetValue("t", out var templateType);
var zipPath = string.Empty;
if (templateType == "module")
{
//代表模块生成
var fileResult = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
var fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
{
Name = name,
});
var fileContent = fileResult as FileContentResult;
File.WriteAllText("./", Encoding.UTF8.GetString(fileContent.FileContents));
zipPath = $"{id}.zip";
await File.WriteAllBytesAsync(zipPath, fileByteArray);
}
else
{
//暂未实现
throw new NotImplementedException();
throw new NotImplementedException("暂未实现");
//代表模块生成
var fileResult = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
{
@@ -53,8 +55,26 @@ namespace Yi.Abp.Tool.Commands
});
}
//默认是当前目录
var unzipDirPath = "./";
//如果创建解决方案文件夹
if (templateType == "module"&&options.TryGetValue("csf", out _))
{
var moduleName = name.ToLower().Replace(".", "-");
if (Directory.Exists(moduleName))
{
throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试");
}
Directory.CreateDirectory(moduleName);
unzipDirPath = moduleName;
}
ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
//创建压缩包后删除临时目录
File.Delete(zipPath);
await Console.Out.WriteLineAsync("模块已生成!");
}
}
}

View File

@@ -15,7 +15,8 @@ class Program
//args = ["-h"];
//args = [];
//args = ["12312"];
args = ["new", "Acme.Book","-t", "module", "-csf"];
//args = ["new", "Acme.Book","-t", "module", "-csf"];
//args = ["new", "Acme.Book", "-t", "module"];
#endif
try
{
@@ -26,8 +27,6 @@ class Program
})
.UseAutofac()
.Build();
var sss= host.Services.GetRequiredService<ITemplateGenService>();
var commandSelector = host.Services.GetRequiredService<CommandSelector>();
await commandSelector.SelectorAsync(args);
}
@@ -36,7 +35,6 @@ class Program
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
Console.ReadKey();
}
}