2024-11-05 22:12:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.CommandLineUtils;
|
|
|
|
|
|
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 string Command => "new";
|
2024-11-08 12:35:54 +08:00
|
|
|
|
public string? Description => "创建项目模板` yi-abp new <name> -csf `";
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
|
|
|
|
|
public void CommandLineApplication(CommandLineApplication application)
|
|
|
|
|
|
{
|
2024-11-08 12:35:54 +08:00
|
|
|
|
application.HelpOption("-h|--help");
|
|
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
var templateTypeOption = application.Option("-t|--template", "模板类型:`module`|`porject`",
|
2024-11-05 22:36:22 +08:00
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
var pathOption = application.Option("-p|--path", "创建路径", CommandOptionType.SingleValue);
|
|
|
|
|
|
var csfOption = application.Option("-csf", "是否创建解决方案文件夹", CommandOptionType.NoValue);
|
2024-11-07 17:35:22 +08:00
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
var soureOption = application.Option("-s|--soure", "模板来源,gitee模板库分支名称: 默认值`default`",
|
2024-11-07 17:35:22 +08:00
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
|
2024-11-29 15:25:16 +08:00
|
|
|
|
var dbmsOption = application.Option("-dbms|--dataBaseMs", "数据库类型,支持目前主流数据库",
|
|
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var moduleNameArgument = application.Argument("moduleName", "模块名", (_) => { });
|
2024-11-08 12:35:54 +08:00
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
//子命令,new list
|
|
|
|
|
|
application.Command("list",(applicationlist) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
applicationlist.OnExecute(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("正在远程搜索中...");
|
|
|
|
|
|
var list=_templateGenService.GetAllTemplatesAsync().Result;
|
|
|
|
|
|
var tip = $"""
|
|
|
|
|
|
全部模板包括:
|
|
|
|
|
|
模板名称
|
|
|
|
|
|
----------------
|
|
|
|
|
|
{list.JoinAsString("\n")}
|
|
|
|
|
|
""";
|
|
|
|
|
|
Console.WriteLine(tip);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
application.OnExecute(() =>
|
|
|
|
|
|
{
|
2024-11-29 15:25:16 +08:00
|
|
|
|
if (dbmsOption.HasValue())
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"检测到使用数据库类型-{dbmsOption.Value()},请在生成后,只需在配置文件中,更改DbConnOptions:Url及DbType即可,支持目前主流数据库20+");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
var path = string.Empty;
|
|
|
|
|
|
if (pathOption.HasValue())
|
|
|
|
|
|
{
|
|
|
|
|
|
path = pathOption.Value();
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
#region 处理生成类型
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var id = Guid.NewGuid().ToString("N");
|
|
|
|
|
|
var zipPath = string.Empty;
|
|
|
|
|
|
byte[] fileByteArray;
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
var soure= soureOption.HasValue() ? soureOption.Value() : "default";
|
2024-11-07 17:35:22 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var templateType = templateTypeOption.HasValue() ? templateTypeOption.Value() : "module";
|
|
|
|
|
|
if (templateType == "module")
|
2024-11-05 22:12:30 +08:00
|
|
|
|
{
|
2024-11-05 22:22:42 +08:00
|
|
|
|
//代表模块生成
|
|
|
|
|
|
fileByteArray = (_templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = moduleNameArgument.Value,
|
2024-11-07 17:35:22 +08:00
|
|
|
|
ModuleSoure = soure
|
2024-11-05 22:22:42 +08:00
|
|
|
|
}).Result);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2024-11-05 22:12:30 +08:00
|
|
|
|
{
|
2024-11-07 17:35:22 +08:00
|
|
|
|
//还是代表模块生成
|
|
|
|
|
|
fileByteArray = _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
2024-11-05 22:22:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = moduleNameArgument.Value,
|
|
|
|
|
|
}).Result;
|
|
|
|
|
|
}
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-11-05 22:36:22 +08:00
|
|
|
|
|
|
|
|
|
|
zipPath = Path.Combine(path, $"{id}.zip");
|
2024-11-05 22:22:42 +08:00
|
|
|
|
File.WriteAllBytes(zipPath, fileByteArray);
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
#endregion
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
#region 处理解决方案文件夹
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
//默认是当前目录
|
|
|
|
|
|
var unzipDirPath = "./";
|
|
|
|
|
|
//如果创建解决方案文件夹
|
|
|
|
|
|
if (csfOption.HasValue())
|
2024-11-05 22:12:30 +08:00
|
|
|
|
{
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var moduleName = moduleNameArgument.Value.ToLower().Replace(".", "-");
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-06 00:05:29 +08:00
|
|
|
|
unzipDirPath = Path.Combine(path, moduleName);
|
2024-11-05 22:36:22 +08:00
|
|
|
|
if (Directory.Exists(unzipDirPath))
|
2024-11-05 22:22:42 +08:00
|
|
|
|
{
|
2024-11-05 22:36:22 +08:00
|
|
|
|
throw new UserFriendlyException($"文件夹[{unzipDirPath}]已存在,请删除后重试");
|
2024-11-05 22:22:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-05 22:36:22 +08:00
|
|
|
|
Directory.CreateDirectory(unzipDirPath);
|
2024-11-05 22:22:42 +08:00
|
|
|
|
}
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
#endregion
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:36:22 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
|
|
|
|
|
|
//创建压缩包后删除临时目录
|
|
|
|
|
|
File.Delete(zipPath);
|
2024-11-05 22:12:30 +08:00
|
|
|
|
|
2024-11-05 22:22:42 +08:00
|
|
|
|
Console.WriteLine("恭喜~模块已生成!");
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
});
|
2024-11-05 22:12:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|