feat: 完成模板从gitee上获取
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -269,6 +269,7 @@ dist
|
||||
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Production.json
|
||||
/Yi.Abp.Net8/test/Yi.Abp.Test/appsettings.Development.json
|
||||
/Yi.Abp.Net8/test/Yi.Abp.Test/appsettings.Production.json
|
||||
/Yi.Abp.Net8/tool/Yi.Abp.Tool.Web/appsettings.Development.json
|
||||
database_backup
|
||||
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Staging.json
|
||||
/Yi.Abp.Net8/src/Yi.Abp.Web/logs/
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace Yi.Abp.Tool.Application.Contracts.Dtos
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模块类型
|
||||
/// </summary>
|
||||
public string ModuleSoure { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据库提供者
|
||||
|
||||
@@ -7,6 +7,6 @@ namespace Yi.Abp.Tool.Application.Contracts
|
||||
public interface ITemplateGenService: IApplicationService
|
||||
{
|
||||
Task<byte[]> CreateModuleAsync(TemplateGenCreateInputDto moduleCreateInputDto);
|
||||
Task<byte[]> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto);
|
||||
Task<List<string>> GetAllTemplatesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@ using Yi.Framework.Core.Helper;
|
||||
|
||||
namespace Yi.Abp.Tool.Application
|
||||
{
|
||||
public class TemplateGenService : ApplicationService,ITemplateGenService
|
||||
public class TemplateGenService : ApplicationService, ITemplateGenService
|
||||
{
|
||||
private readonly TemplateGenManager _templateGenManager;
|
||||
public TemplateGenService(TemplateGenManager templateGenManager) { _templateGenManager = templateGenManager; }
|
||||
|
||||
public TemplateGenService(TemplateGenManager templateGenManager)
|
||||
{
|
||||
_templateGenManager = templateGenManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载模块文件
|
||||
@@ -29,8 +33,10 @@ namespace Yi.Abp.Tool.Application
|
||||
{
|
||||
moduleCreateInputDto.SetNameReplace();
|
||||
|
||||
//模块类型,就是分支小写
|
||||
var input = moduleCreateInputDto.Adapt<TemplateGenCreateDto>();
|
||||
input.SetTemplateFilePath(_templateGenManager._toolOptions.ModuleTemplateFilePath);
|
||||
input.SetTemplateGiteeRef(moduleCreateInputDto.ModuleSoure);
|
||||
|
||||
var filePath = await _templateGenManager.CreateTemplateAsync(input);
|
||||
|
||||
////考虑从路径中获取
|
||||
@@ -39,22 +45,15 @@ namespace Yi.Abp.Tool.Application
|
||||
return await File.ReadAllBytesAsync(filePath);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 下载模块文件
|
||||
/// 获取全部模板列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<byte[]> CreateProjectAsync(TemplateGenCreateInputDto moduleCreateInputDto)
|
||||
[HttpGet("template-gen/template")]
|
||||
public async Task<List<string>> GetAllTemplatesAsync()
|
||||
{
|
||||
moduleCreateInputDto.SetNameReplace();
|
||||
|
||||
var input = moduleCreateInputDto.Adapt<TemplateGenCreateDto>();
|
||||
input.SetTemplateFilePath(_templateGenManager._toolOptions.ProjectTemplateFilePath);
|
||||
var filePath = await _templateGenManager.CreateTemplateAsync(input);
|
||||
|
||||
//考虑从路径中获取
|
||||
// var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(filePath));
|
||||
//设置附件下载,下载名称
|
||||
return await File.ReadAllBytesAsync(filePath);
|
||||
return await _templateGenManager.GetAllTemplatesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,21 +9,19 @@ namespace Yi.Abp.Tool.Domain.Shared.Dtos
|
||||
{
|
||||
public class TemplateGenCreateDto
|
||||
{
|
||||
public void SetTemplateFilePath(string templateFilePath)
|
||||
public void SetTemplateGiteeRef(string moduleType)
|
||||
{
|
||||
this.TemplateFilePath = templateFilePath;
|
||||
this.GiteeRef = moduleType.ToLower();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 模板文件路径
|
||||
/// </summary>
|
||||
public string TemplateFilePath { get; set; }
|
||||
/// <summary>
|
||||
/// 模块名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模块所属gitee分支
|
||||
/// </summary>
|
||||
public string GiteeRef { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据库提供者
|
||||
|
||||
@@ -8,17 +8,6 @@ namespace Yi.Abp.Tool.Domain.Shared.Options
|
||||
{
|
||||
public class ToolOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 模块模板zip文件路径
|
||||
/// </summary>
|
||||
public string ModuleTemplateFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 项目模板zip文件路径
|
||||
/// </summary>
|
||||
public string ProjectTemplateFilePath { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 临时文件目录
|
||||
/// </summary>
|
||||
|
||||
@@ -9,5 +9,8 @@
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Options\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
85
Yi.Abp.Net8/tool/Yi.Abp.Tool.Domain/GiteeManager.cs
Normal file
85
Yi.Abp.Net8/tool/Yi.Abp.Tool.Domain/GiteeManager.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Yi.Abp.Tool.Domain;
|
||||
|
||||
public class GiteeManager : ITransientDependency
|
||||
{
|
||||
private readonly string _accessToken;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private const string GiteeHost = "https://gitee.com/api/v5";
|
||||
private const string Owner = "ccnetcore";
|
||||
private const string Repo = "yi-template";
|
||||
|
||||
public GiteeManager(IConfiguration configuration, IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_accessToken = configuration.GetValue<string>("GiteeAccession");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在当前分支
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> IsExsitBranchAsync(string branch)
|
||||
{
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
var response =
|
||||
await client.GetAsync(
|
||||
$"{GiteeHost}/repos/{Owner}/{Repo}/branches/{branch}?access_token={_accessToken}");
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有分支
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<string>> GetAllBranchAsync()
|
||||
{
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
var response =
|
||||
await client.GetAsync(
|
||||
$"{GiteeHost}/repos/{Owner}/{Repo}/branches?access_token={_accessToken}&sort=name&direction=asc&page=1&per_page=100");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result= await response.Content.ReadAsStringAsync();
|
||||
JArray jsonArray= JArray.Parse(result);
|
||||
// 创建一个列表来存储名字
|
||||
List<string> names = new List<string>();
|
||||
|
||||
// 遍历每个对象,获取 name 字段
|
||||
foreach (JObject obj in jsonArray)
|
||||
{
|
||||
// 获取 name 字段的值
|
||||
string name = obj["name"]?.ToString();
|
||||
if (name != null)
|
||||
{
|
||||
names.Add(name);
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 下载仓库分支代码
|
||||
/// </summary>
|
||||
/// <param name="branch"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Stream> DownLoadFileAsync(string branch)
|
||||
{
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
var response =
|
||||
await client.GetAsync(
|
||||
$"{GiteeHost}/repos/{Owner}/{Repo}/zipball?access_token={_accessToken}&ref={branch}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -13,13 +13,19 @@ namespace Yi.Abp.Tool.Domain
|
||||
{
|
||||
public class TemplateGenManager : ITransientDependency
|
||||
{
|
||||
public readonly ToolOptions _toolOptions;
|
||||
public TemplateGenManager(IOptionsMonitor<ToolOptions> toolOptions) { _toolOptions = toolOptions.CurrentValue; }
|
||||
private readonly ToolOptions _toolOptions;
|
||||
private readonly GiteeManager _giteeManager;
|
||||
public TemplateGenManager(IOptionsMonitor<ToolOptions> toolOptions, GiteeManager giteeManager)
|
||||
{
|
||||
_giteeManager = giteeManager;
|
||||
_toolOptions = toolOptions.CurrentValue;
|
||||
}
|
||||
public async Task<string> CreateTemplateAsync(TemplateGenCreateDto input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input.TemplateFilePath))
|
||||
//这里判断gitee上是否有这个分支
|
||||
if (await _giteeManager.IsExsitBranchAsync(input.GiteeRef))
|
||||
{
|
||||
throw new UserFriendlyException($"模板路径无法找到,请检查,[{input.TemplateFilePath}]路径");
|
||||
throw new UserFriendlyException($"Gitee分支未找到{input.GiteeRef},请检查,[{input.GiteeRef}]分支是否存在");
|
||||
}
|
||||
if (string.IsNullOrEmpty(_toolOptions.TempDirPath))
|
||||
{
|
||||
@@ -33,8 +39,15 @@ namespace Yi.Abp.Tool.Domain
|
||||
Directory.CreateDirectory(tempFileDirPath);
|
||||
}
|
||||
|
||||
//文件解压覆盖
|
||||
ZipFile.ExtractToDirectory(input.TemplateFilePath, tempFileDirPath, true);
|
||||
//下载的模板存放文件路径
|
||||
var downloadFilePath = Path.Combine(_toolOptions.TempDirPath,"download" ,$"{id}.zip");
|
||||
var gitSteam= await _giteeManager.DownLoadFileAsync(input.GiteeRef);
|
||||
using (FileStream fileStream = new FileStream(downloadFilePath, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
await gitSteam.CopyToAsync(fileStream);
|
||||
}
|
||||
//文件解压覆盖,将刚刚下载的模板,解压即可
|
||||
ZipFile.ExtractToDirectory(downloadFilePath, tempFileDirPath, true);
|
||||
|
||||
await ReplaceContentAsync(tempFileDirPath, input.ReplaceStrData);
|
||||
var tempFilePath = Path.Combine(_toolOptions.TempDirPath, $"{id}.zip");
|
||||
@@ -45,13 +58,22 @@ namespace Yi.Abp.Tool.Domain
|
||||
return tempFilePath;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取全部模板列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<string>> GetAllTemplatesAsync()
|
||||
{
|
||||
return await _giteeManager.GetAllBranchAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 替换内容,key为要替换的内容,value为替换成的内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task ReplaceContentAsync(string rootDirectory, Dictionary<string, string> dic)
|
||||
{
|
||||
|
||||
foreach (var dicEntry in dic)
|
||||
{
|
||||
await ReplaceInDirectory(rootDirectory, dicEntry.Key, dicEntry.Value);
|
||||
@@ -110,5 +132,6 @@ namespace Yi.Abp.Tool.Domain
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@@ -12,6 +15,7 @@ using Yi.Abp.Tool.Application;
|
||||
using Yi.Framework.AspNetCore;
|
||||
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
|
||||
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
|
||||
using Yi.Framework.Core.Json;
|
||||
|
||||
namespace Yi.Abp.Tool.Web
|
||||
{
|
||||
@@ -41,15 +45,17 @@ namespace Yi.Abp.Tool.Web
|
||||
});
|
||||
|
||||
//设置api格式
|
||||
service.AddControllers().AddNewtonsoftJson(options =>
|
||||
Configure<JsonOptions>(options =>
|
||||
{
|
||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
options.JsonSerializerOptions.TypeInfoResolver = new DefaultJsonTypeInfoResolver();
|
||||
options.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Configure<AbpAntiForgeryOptions>(options =>
|
||||
{
|
||||
options.AutoValidate = false;
|
||||
@@ -81,7 +87,7 @@ namespace Yi.Abp.Tool.Web
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
service.AddHttpClient();
|
||||
//速率限制
|
||||
//每60秒限制100个请求,滑块添加,分6段
|
||||
service.AddRateLimiter(_ =>
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
"GiteeAccession": "8d9f05faec154475f121079579a0abf0"
|
||||
}
|
||||
|
||||
@@ -15,5 +15,6 @@
|
||||
"TempDirPath": "wwwroot/temp",
|
||||
"ModuleTemplateFilePath": "wwwroot/ModuleTemplate.zip",
|
||||
"ProjectTemplateFilePath": "wwwroot/ProjectTemplate.zip"
|
||||
}
|
||||
},
|
||||
"GiteeAccession": ""
|
||||
}
|
||||
|
||||
@@ -26,12 +26,33 @@ namespace Yi.Abp.Tool.Commands
|
||||
|
||||
public void CommandLineApplication(CommandLineApplication application)
|
||||
{
|
||||
var templateTypeOption = application.Option("-t|--moduleType", "模板类型:`module`|`porject`",
|
||||
var templateTypeOption = application.Option("-t|--template", "模板类型:`module`|`porject`",
|
||||
CommandOptionType.SingleValue);
|
||||
var pathOption = application.Option("-p|--path", "创建路径", CommandOptionType.SingleValue);
|
||||
var csfOption = application.Option("-csf", "是否创建解决方案文件夹", CommandOptionType.NoValue);
|
||||
|
||||
var soureOption = application.Option("-s|--soure", "模板来源,gitee模板库分支名称: 默认值`defualt`",
|
||||
CommandOptionType.SingleValue);
|
||||
|
||||
var moduleNameArgument = application.Argument("moduleName", "模块名", (_) => { });
|
||||
|
||||
//子命令,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;
|
||||
});
|
||||
});
|
||||
|
||||
application.OnExecute(() =>
|
||||
{
|
||||
@@ -41,6 +62,8 @@ namespace Yi.Abp.Tool.Commands
|
||||
var zipPath = string.Empty;
|
||||
byte[] fileByteArray;
|
||||
|
||||
var soure= soureOption.HasValue() ? soureOption.Value() : "defualt";
|
||||
|
||||
var templateType = templateTypeOption.HasValue() ? templateTypeOption.Value() : "module";
|
||||
if (templateType == "module")
|
||||
{
|
||||
@@ -48,12 +71,13 @@ namespace Yi.Abp.Tool.Commands
|
||||
fileByteArray = (_templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
||||
{
|
||||
Name = moduleNameArgument.Value,
|
||||
ModuleSoure = soure
|
||||
}).Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
//代表模块生成
|
||||
fileByteArray = _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
|
||||
//还是代表模块生成
|
||||
fileByteArray = _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
||||
{
|
||||
Name = moduleNameArgument.Value,
|
||||
}).Result;
|
||||
|
||||
@@ -13,13 +13,15 @@ class Program
|
||||
//args = ["-h"];
|
||||
|
||||
//版本
|
||||
args = ["-v"];
|
||||
// args = ["-v"];
|
||||
|
||||
//清理
|
||||
// args = ["clear"];
|
||||
|
||||
//创建模块
|
||||
//args = ["new","oooo", "-t","module","-p","D:\\temp","-csf"];
|
||||
//查看模板列表
|
||||
args = ["new","list"];
|
||||
|
||||
//添加模块
|
||||
//args = ["add-module", "kkk"];
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace Yi.Abp.Tool
|
||||
Configure<AbpRemoteServiceOptions>(options =>
|
||||
{
|
||||
options.RemoteServices.Default =
|
||||
new RemoteServiceConfiguration("https://ccnetcore.com:19009");
|
||||
// new RemoteServiceConfiguration("https://ccnetcore.com:19009");
|
||||
new RemoteServiceConfiguration("http://localhost:19002");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user