2024-06-02 13:09:25 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
using Yi.Abp.Tool.Domain.Shared.Dtos;
|
|
|
|
|
|
using Yi.Abp.Tool.Domain.Shared.Options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Abp.Tool.Domain
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TemplateGenManager : ITransientDependency
|
|
|
|
|
|
{
|
2024-11-07 17:35:22 +08:00
|
|
|
|
private readonly ToolOptions _toolOptions;
|
|
|
|
|
|
private readonly GiteeManager _giteeManager;
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
public TemplateGenManager(IOptionsMonitor<ToolOptions> toolOptions, GiteeManager giteeManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
_giteeManager = giteeManager;
|
|
|
|
|
|
_toolOptions = toolOptions.CurrentValue;
|
|
|
|
|
|
}
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-06-02 13:09:25 +08:00
|
|
|
|
public async Task<string> CreateTemplateAsync(TemplateGenCreateDto input)
|
|
|
|
|
|
{
|
2024-11-07 17:35:22 +08:00
|
|
|
|
//这里判断gitee上是否有这个分支
|
2024-11-08 11:10:45 +08:00
|
|
|
|
if (!await _giteeManager.IsExsitBranchAsync(input.GiteeRef))
|
2024-06-02 13:09:25 +08:00
|
|
|
|
{
|
2024-11-07 17:35:22 +08:00
|
|
|
|
throw new UserFriendlyException($"Gitee分支未找到{input.GiteeRef},请检查,[{input.GiteeRef}]分支是否存在");
|
2024-06-02 13:09:25 +08:00
|
|
|
|
}
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-06-02 13:09:25 +08:00
|
|
|
|
if (string.IsNullOrEmpty(_toolOptions.TempDirPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"临时目录路径无法找到,请检查,[{_toolOptions.TempDirPath}]路径");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var id = Guid.NewGuid().ToString("N");
|
|
|
|
|
|
var tempFileDirPath = Path.Combine(_toolOptions.TempDirPath, $"{id}");
|
|
|
|
|
|
if (!Directory.Exists(tempFileDirPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(tempFileDirPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
//下载的模板存放文件路径
|
2024-11-08 11:10:45 +08:00
|
|
|
|
var downloadPath = Path.Combine(_toolOptions.TempDirPath, "download");
|
|
|
|
|
|
if (!Directory.Exists(downloadPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(downloadPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var downloadFilePath = Path.Combine(downloadPath, $"{id}.zip");
|
|
|
|
|
|
var gitSteam = await _giteeManager.DownLoadFileAsync(input.GiteeRef);
|
|
|
|
|
|
using (FileStream fileStream = new FileStream(downloadFilePath, FileMode.Create, FileAccess.Write))
|
|
|
|
|
|
{
|
|
|
|
|
|
await gitSteam.CopyToAsync(fileStream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
//文件解压覆盖,将刚刚下载的模板,解压即可
|
|
|
|
|
|
ZipFile.ExtractToDirectory(downloadFilePath, tempFileDirPath, true);
|
2024-06-02 13:09:25 +08:00
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
|
|
|
|
|
//注意,这里下载的zip包,其实多了一层,我们进行操作的时候,要将操作目录进一步
|
|
|
|
|
|
var operPath = Directory.GetDirectories(tempFileDirPath)[0];
|
|
|
|
|
|
await ReplaceContentAsync(operPath, input.ReplaceStrData);
|
2024-06-02 13:09:25 +08:00
|
|
|
|
var tempFilePath = Path.Combine(_toolOptions.TempDirPath, $"{id}.zip");
|
2024-11-08 11:10:45 +08:00
|
|
|
|
ZipFile.CreateFromDirectory(operPath, tempFilePath);
|
2024-06-02 13:09:25 +08:00
|
|
|
|
|
|
|
|
|
|
//创建压缩包后删除临时目录
|
|
|
|
|
|
Directory.Delete(tempFileDirPath, true);
|
|
|
|
|
|
return tempFilePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-11-07 17:35:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取全部模板列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<List<string>> GetAllTemplatesAsync()
|
|
|
|
|
|
{
|
2024-11-08 11:10:45 +08:00
|
|
|
|
var refs = await _giteeManager.GetAllBranchAsync();
|
|
|
|
|
|
|
|
|
|
|
|
//移除主分支
|
|
|
|
|
|
refs.Remove("master");
|
|
|
|
|
|
return refs;
|
2024-11-07 17:35:22 +08:00
|
|
|
|
}
|
2024-11-08 11:10:45 +08:00
|
|
|
|
|
2024-06-02 13:09:25 +08:00
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//替换目录名
|
|
|
|
|
|
static async Task ReplaceInDirectory(string directoryPath, string searchString, string replaceString)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 替换当前目录下的文件和文件夹名称
|
|
|
|
|
|
var newDirPath = await ReplaceInFiles(directoryPath, searchString, replaceString);
|
|
|
|
|
|
|
|
|
|
|
|
// 递归遍历子目录
|
|
|
|
|
|
string[] subDirectories = Directory.GetDirectories(newDirPath);
|
|
|
|
|
|
foreach (string subDirectory in subDirectories)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ReplaceInDirectory(subDirectory, searchString, replaceString);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//替换文件名
|
|
|
|
|
|
static async Task<string> ReplaceInFiles(string directoryPath, string searchString, string replaceString)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 替换目录名
|
|
|
|
|
|
string directoryName = new DirectoryInfo(directoryPath).Name;
|
|
|
|
|
|
string newDirectoryName = directoryName.Replace(searchString, replaceString);
|
|
|
|
|
|
if (directoryName != newDirectoryName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string parentDirectory = Path.GetDirectoryName(directoryPath);
|
|
|
|
|
|
string newDirectoryPath = Path.Combine(parentDirectory, newDirectoryName);
|
|
|
|
|
|
Directory.Move(directoryPath, newDirectoryPath);
|
|
|
|
|
|
directoryPath = newDirectoryPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 替换文件名
|
|
|
|
|
|
string[] files = Directory.GetFiles(directoryPath);
|
|
|
|
|
|
foreach (string file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
string newFileName = file.Replace(searchString, replaceString);
|
|
|
|
|
|
if (file != newFileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Move(file, newFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
files = Directory.GetFiles(directoryPath);
|
|
|
|
|
|
// 替换文件内容
|
|
|
|
|
|
foreach (string file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
string fileContent = await File.ReadAllTextAsync(file);
|
|
|
|
|
|
string newFileContent = fileContent.Replace(searchString, replaceString);
|
|
|
|
|
|
await File.WriteAllTextAsync(file, newFileContent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return directoryPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-08 11:10:45 +08:00
|
|
|
|
}
|