feat: 实现 Blazor 前端界面 (Task 4.3) 和 CLI 工具 (Task 5.1)

Task 4.3 - Blazor 前端界面:
- 创建 CodePlay.WebUI Blazor Server 项目
- 实现 Converter.razor 代码转换页面
- 提供代码输入、语言选择、转换按钮
- 显示转换结果、TODO 列表和问题列表
- 更新导航菜单
- 注册 ConversionService 服务

Task 5.1 - CLI 命令行工具:
- 集成 System.CommandLine 库
- 实现 convert 命令(代码转换)
- 实现 list 命令(列出支持的转换)
- 实现 check 命令(检查是否支持)
- 支持 -s/-t 指定源/目标语言
- 支持 -i/-o 指定输入/输出文件
- 支持 -v 指定验证轮次
- 支持 -c 指定配置文件

测试:
- CLI help 命令: 
- CLI list 命令: 
- CLI convert 命令:  (成功转换 C# 到 Java)
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
monkeycode-ai
2026-06-03 08:23:31 +00:00
parent c09982e252
commit 4ed56f46e2
28 changed files with 1113 additions and 324 deletions
+8
View File
@@ -7,4 +7,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodePlay.Core\CodePlay.Core.csproj" />
</ItemGroup>
</Project>
+263 -2
View File
@@ -1,2 +1,263 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
using CodePlay.Core.Services;
namespace CodePlay.CLI;
public class Program
{
public static async Task<int> Main(string[] args)
{
// 定义源语言选项
var sourceLanguageOption = new Option<LanguageType>(
name: "--source-language",
description: "源语言 (CSharp, Java, CPlusPlus)",
parseArgument: result =>
{
var value = result.Tokens.Single().Value;
if (Enum.TryParse<LanguageType>(value, true, out var language))
{
return language;
}
result.ErrorMessage = $"Invalid language: {value}. Valid values are: CSharp, Java, CPlusPlus";
return LanguageType.None;
}
);
sourceLanguageOption.AddAlias("-s");
sourceLanguageOption.IsRequired = true;
// 定义目标语言选项
var targetLanguageOption = new Option<LanguageType>(
name: "--target-language",
description: "目标语言 (CSharp, Java, CPlusPlus)",
parseArgument: result =>
{
var value = result.Tokens.Single().Value;
if (Enum.TryParse<LanguageType>(value, true, out var language))
{
return language;
}
result.ErrorMessage = $"Invalid language: {value}. Valid values are: CSharp, Java, CPlusPlus";
return LanguageType.None;
}
);
targetLanguageOption.AddAlias("-t");
targetLanguageOption.IsRequired = true;
// 定义输入文件选项
var inputOption = new Option<FileInfo>(
name: "--input",
description: "输入文件路径"
);
inputOption.AddAlias("-i");
inputOption.IsRequired = true;
// 定义输出文件选项
var outputOption = new Option<FileInfo>(
name: "--output",
description: "输出文件路径"
);
outputOption.AddAlias("-o");
// 定义验证轮次选项
var validationRoundsOption = new Option<int>(
name: "--validation-rounds",
getDefaultValue: () => 2,
description: "验证轮次 (1-3)"
);
validationRoundsOption.AddAlias("-v");
// 定义配置文件选项
var configOption = new Option<FileInfo>(
name: "--config",
description: "配置文件路径"
);
configOption.AddAlias("-c");
// 定义转换命令
var convertCommand = new Command("convert", "转换代码文件")
{
sourceLanguageOption,
targetLanguageOption,
inputOption,
outputOption,
validationRoundsOption,
configOption
};
convertCommand.SetHandler(async (context) =>
{
var sourceLang = context.ParseResult.GetValueForOption(sourceLanguageOption);
var targetLang = context.ParseResult.GetValueForOption(targetLanguageOption);
var inputFile = context.ParseResult.GetValueForOption(inputOption);
var outputFile = context.ParseResult.GetValueForOption(outputOption);
var validationRounds = context.ParseResult.GetValueForOption(validationRoundsOption);
var configFile = context.ParseResult.GetValueForOption(configOption);
try
{
// 读取输入文件
Console.WriteLine($"正在读取文件:{inputFile.FullName}");
var sourceCode = await File.ReadAllTextAsync(inputFile.FullName);
// 加载配置(如果有)
ConversionOptions? options = null;
if (configFile != null && configFile.Exists)
{
options = LoadConfiguration(configFile.FullName);
}
// 创建转换请求
var request = new ConversionRequest
{
SourceCode = sourceCode,
SourceLanguage = sourceLang,
TargetLanguage = targetLang,
ValidationRounds = validationRounds,
Options = options
};
// 执行转换
Console.WriteLine($"正在转换:{sourceLang} → {targetLang}");
var conversionService = new ConversionService();
var result = await conversionService.ConvertAsync(request);
if (result.Success)
{
Console.WriteLine($"转换成功!");
Console.WriteLine($"转换行数:{result.Report?.LinesConverted}");
Console.WriteLine($"转换类数:{result.Report?.ClassesConverted}");
Console.WriteLine($"转换方法数:{result.Report?.MethodsConverted}");
// 输出结果
if (outputFile != null)
{
await File.WriteAllTextAsync(outputFile.FullName, result.TransformedCode);
Console.WriteLine($"已输出到:{outputFile.FullName}");
}
else
{
Console.WriteLine("\n==== 转换结果 ====");
Console.WriteLine(result.TransformedCode);
}
// 显示 TODO 和问题
if (result.Report?.TodoItems.Count > 0)
{
Console.WriteLine("\n⚠️ 需要注意的 TODO 项:");
foreach (var todo in result.Report.TodoItems)
{
Console.WriteLine($" - {todo.Description}");
Console.WriteLine($" 原因:{todo.WhyNotDirect}");
Console.WriteLine($" 建议:{todo.RecommendedAlternative}");
}
}
if (result.Report?.Issues.Count > 0)
{
Console.WriteLine("\n⚠️ 需要注意的问题:");
foreach (var issue in result.Report.Issues)
{
Console.WriteLine($" - {issue.Description}");
Console.WriteLine($" 建议:{issue.Suggestion}");
}
}
context.ExitCode = 0;
}
else
{
Console.WriteLine($"❌ 转换失败:{result.ErrorMessage}");
context.ExitCode = 1;
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ 错误:{ex.Message}");
context.ExitCode = 1;
}
});
// 定义 list 命令
var listCommand = new Command("list", "列出支持的转换");
listCommand.SetHandler((context) =>
{
var conversionService = new ConversionService();
var supported = conversionService.GetSupportedConversions();
Console.WriteLine("支持的转换:");
foreach (var (source, target) in supported)
{
Console.WriteLine($" {source} → {target}");
}
context.ExitCode = 0;
});
// 定义 check 命令
var checkCommand = new Command("check", "检查是否支持指定的转换")
{
sourceLanguageOption,
targetLanguageOption
};
checkCommand.SetHandler((context) =>
{
var sourceLang = context.ParseResult.GetValueForOption(sourceLanguageOption);
var targetLang = context.ParseResult.GetValueForOption(targetLanguageOption);
var conversionService = new ConversionService();
var isSupported = conversionService.IsConversionSupported(sourceLang, targetLang);
if (isSupported)
{
Console.WriteLine($"✅ 支持 {sourceLang} → {targetLang} 转换");
context.ExitCode = 0;
}
else
{
Console.WriteLine($"❌ 不支持 {sourceLang} → {targetLang} 转换");
context.ExitCode = 1;
}
});
// 创建根命令
var rootCommand = new RootCommand("CodePlay 代码转换工具 - 支持 C#、Java、C++ 之间的代码转换")
{
convertCommand,
listCommand,
checkCommand
};
// 配置并运行解析器
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.Build();
return await parser.InvokeAsync(args);
}
private static ConversionOptions? LoadConfiguration(string configFile)
{
try
{
if (configFile.EndsWith(".json"))
{
// 这里可以添加 JSON 配置加载逻辑
// MVP 版本简化处理
return new ConversionOptions
{
KeepComments = true,
KeepDocStrings = true
};
}
}
catch (Exception ex)
{
Console.WriteLine($"⚠️ 加载配置文件失败:{ex.Message},使用默认配置");
}
return null;
}
}