Files
Yi.Admin/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/NewCommand.cs

85 lines
2.7 KiB
C#
Raw Normal View History

2024-06-02 13:09:25 +08:00
using System;
using System.Collections.Generic;
2024-06-02 14:12:37 +08:00
using System.IO.Compression;
2024-06-02 13:09:25 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Yi.Abp.Tool.Application.Contracts;
using Yi.Abp.Tool.Application.Contracts.Dtos;
namespace Yi.Abp.Tool.Commands
{
public class NewCommand : ICommand
{
private readonly ITemplateGenService _templateGenService;
public NewCommand(ITemplateGenService templateGenService)
{
_templateGenService = templateGenService;
}
public List<string> CommandStrs => new List<string>() { "new" };
public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
{
2024-06-02 14:12:37 +08:00
var id = Guid.NewGuid().ToString("N");
2024-06-02 13:09:25 +08:00
//只有一个new
if (args.Length <= 1)
{
throw new UserFriendlyException("命令错误new命令后必须添加 名称");
}
string name = args[1];
2024-06-02 17:20:38 +08:00
#region
2024-06-02 13:09:25 +08:00
options.TryGetValue("t", out var templateType);
2024-06-02 14:12:37 +08:00
var zipPath = string.Empty;
2024-06-02 17:20:38 +08:00
byte[] fileByteArray;
2024-06-02 13:09:25 +08:00
if (templateType == "module")
{
//代表模块生成
2024-06-02 17:20:38 +08:00
fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
2024-06-02 13:09:25 +08:00
{
Name = name,
});
}
else
{
//代表模块生成
2024-06-02 17:20:38 +08:00
fileByteArray = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
2024-06-02 13:09:25 +08:00
{
Name = name,
});
}
2024-06-02 17:20:38 +08:00
zipPath = $"{id}.zip";
await File.WriteAllBytesAsync(zipPath, fileByteArray);
#endregion
2024-06-02 13:09:25 +08:00
2024-06-02 17:20:38 +08:00
#region
2024-06-02 14:12:37 +08:00
//默认是当前目录
var unzipDirPath = "./";
//如果创建解决方案文件夹
2024-06-02 17:20:38 +08:00
if (options.TryGetValue("csf", out _))
2024-06-02 14:12:37 +08:00
{
var moduleName = name.ToLower().Replace(".", "-");
if (Directory.Exists(moduleName))
{
throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试");
}
Directory.CreateDirectory(moduleName);
unzipDirPath = moduleName;
}
2024-06-02 17:20:38 +08:00
#endregion
2024-06-02 14:12:37 +08:00
ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
//创建压缩包后删除临时目录
File.Delete(zipPath);
2024-06-02 13:09:25 +08:00
2024-06-02 17:20:38 +08:00
await Console.Out.WriteLineAsync("恭喜~模块已生成!");
2024-06-02 13:09:25 +08:00
}
}
}