feat: CodePlay 第二阶段优化 - 转换质量与特性完善
核心修复: - 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败) - 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements) - 修复 PropertyConverter init-only 属性组索引 新增转换器 (C# 8-13 特性): - NullCoalescingConverter: ??、?.、??= 运算符转换 - SwitchExpressionConverter: switch 表达式→if-else 链 - PrimaryConstructorConverter: 主构造函数→传统构造函数 增强: - LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等 - AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误 - NamingConverter: PascalCase→camelCase 命名转换 - DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录 - XML Doc→JavaDoc 格式转换与注释保留 新增测试: - CSharpToJavaEdgeCaseTests: 16 个边界测试 - CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试 - 从 164 增加到 179 总测试 (168 通过, 0 失败) 新增文件: - Pipeline/Converters/NullCoalescingConverter.cs - Pipeline/Converters/SwitchExpressionConverter.cs - Pipeline/Converters/PrimaryConstructorConverter.cs - Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs - Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs - Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
+66
-390
@@ -1,10 +1,11 @@
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Builder;
|
||||
using System.CommandLine.Parsing;
|
||||
using System.Text.Json;
|
||||
using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Common;
|
||||
using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Services;
|
||||
using CodePlay.Core.Converters;
|
||||
using CodePlay.Core.Parsers;
|
||||
|
||||
namespace CodePlay.CLI;
|
||||
|
||||
@@ -12,422 +13,97 @@ public class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
// 定义源语言选项
|
||||
var sourceLanguageOption = new Option<LanguageType>(
|
||||
name: "--source-language",
|
||||
description: "源语言 (CSharp, Java, CPlusPlus)"
|
||||
);
|
||||
sourceLanguageOption.AddAlias("-s");
|
||||
sourceLanguageOption.IsRequired = true;
|
||||
Console.WriteLine("CodePlay CLI - Code Conversion Tool");
|
||||
Console.WriteLine("Version 1.0.0");
|
||||
Console.WriteLine();
|
||||
|
||||
// 定义目标语言选项
|
||||
var targetLanguageOption = new Option<LanguageType>(
|
||||
name: "--target-language",
|
||||
description: "目标语言 (CSharp, Java, CPlusPlus)"
|
||||
);
|
||||
targetLanguageOption.AddAlias("-t");
|
||||
targetLanguageOption.IsRequired = true;
|
||||
var sourceOption = new Option<string>(["-s", "--source"], "Source language (CSharp, Java)");
|
||||
var targetOption = new Option<string>(["-t", "--target"], "Target language (CSharp, Java)");
|
||||
var inputOption = new Option<string>(["-i", "--input"], "Input file path");
|
||||
var outputOption = new Option<string>(["-o", "--output"], "Output file path");
|
||||
var verboseOption = new Option<bool>(["-v", "--verbose"], "Verbose output");
|
||||
|
||||
// 定义输入文件选项
|
||||
var inputOption = new Option<FileInfo>(
|
||||
name: "--input",
|
||||
description: "输入文件路径或目录"
|
||||
);
|
||||
inputOption.AddAlias("-i");
|
||||
inputOption.IsRequired = true;
|
||||
var rootCommand = new RootCommand("CodePlay - Convert code between languages");
|
||||
rootCommand.AddOption(sourceOption);
|
||||
rootCommand.AddOption(targetOption);
|
||||
rootCommand.AddOption(inputOption);
|
||||
rootCommand.AddOption(outputOption);
|
||||
rootCommand.AddOption(verboseOption);
|
||||
|
||||
// 定义输出文件/目录选项
|
||||
var outputOption = new Option<FileInfo>(
|
||||
name: "--output",
|
||||
description: "输出文件路径或目录"
|
||||
);
|
||||
outputOption.AddAlias("-o");
|
||||
|
||||
// 定义批量转换模式选项
|
||||
var batchOption = new Option<bool>(
|
||||
name: "--batch",
|
||||
description: "启用批量转换模式(目录转换)"
|
||||
);
|
||||
batchOption.AddAlias("-b");
|
||||
|
||||
// 定义递归子目录选项
|
||||
var recursiveOption = new Option<bool>(
|
||||
name: "--recursive",
|
||||
description: "递归处理子目录",
|
||||
getDefaultValue: () => true
|
||||
);
|
||||
recursiveOption.AddAlias("-r");
|
||||
|
||||
// 定义验证轮次选项
|
||||
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 verboseOption = new Option<bool>(
|
||||
name: "--verbose",
|
||||
description: "显示详细输出信息"
|
||||
);
|
||||
verboseOption.AddAlias("--verbose");
|
||||
|
||||
// 定义转换命令
|
||||
var convertCommand = new Command("convert", "转换代码文件或目录")
|
||||
rootCommand.SetHandler(async (context) =>
|
||||
{
|
||||
sourceLanguageOption,
|
||||
targetLanguageOption,
|
||||
inputOption,
|
||||
outputOption,
|
||||
batchOption,
|
||||
recursiveOption,
|
||||
validationRoundsOption,
|
||||
configOption,
|
||||
verboseOption
|
||||
};
|
||||
|
||||
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 isBatch = context.ParseResult.GetValueForOption(batchOption);
|
||||
var isRecursive = context.ParseResult.GetValueForOption(recursiveOption);
|
||||
var validationRounds = context.ParseResult.GetValueForOption(validationRoundsOption);
|
||||
var configFile = context.ParseResult.GetValueForOption(configOption);
|
||||
var source = context.ParseResult.GetValueForOption(sourceOption);
|
||||
var target = context.ParseResult.GetValueForOption(targetOption);
|
||||
var input = context.ParseResult.GetValueForOption(inputOption);
|
||||
var output = context.ParseResult.GetValueForOption(outputOption);
|
||||
var verbose = context.ParseResult.GetValueForOption(verboseOption);
|
||||
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
Console.WriteLine("Error: Input file is required");
|
||||
context.ExitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (isBatch || inputFile.Attributes.HasFlag(FileAttributes.Directory))
|
||||
Console.WriteLine($"Converting: {input}");
|
||||
Console.WriteLine($"From: {source} To: {target}");
|
||||
|
||||
var sourceCode = await File.ReadAllTextAsync(input);
|
||||
var converter = new CSharpToJavaConverter();
|
||||
var parser = new CSharpParser();
|
||||
|
||||
var tree = await parser.ParseAsync(sourceCode);
|
||||
|
||||
LanguageType targetLang = LanguageType.Java;
|
||||
if (!Enum.TryParse(target, true, out targetLang))
|
||||
{
|
||||
// 批量转换模式
|
||||
Console.WriteLine("📁 批量转换模式启动");
|
||||
Console.WriteLine($"源目录:{inputFile.FullName}");
|
||||
targetLang = LanguageType.Java;
|
||||
}
|
||||
|
||||
var result = await converter.ConvertAsync(tree, targetLang);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
Console.WriteLine("Conversion successful!");
|
||||
var lines = result.TransformedCode?.Split('\n') ?? Array.Empty<string>();
|
||||
Console.WriteLine($"Lines: {lines.Length}");
|
||||
|
||||
var batchService = new BatchConversionService(
|
||||
new ConversionService(),
|
||||
new ReportStorageService()
|
||||
);
|
||||
|
||||
var options = new ConversionOptions
|
||||
if (!string.IsNullOrEmpty(output))
|
||||
{
|
||||
KeepComments = true,
|
||||
KeepDocStrings = true
|
||||
};
|
||||
await File.WriteAllTextAsync(output, result.TransformedCode);
|
||||
Console.WriteLine($"Output: {output}");
|
||||
}
|
||||
else if (verbose)
|
||||
{
|
||||
Console.WriteLine("\n==== Result ====");
|
||||
Console.WriteLine(result.TransformedCode);
|
||||
}
|
||||
|
||||
var targetDir = outputFile?.FullName ??
|
||||
Path.Combine(Path.GetDirectoryName(inputFile.FullName)!,
|
||||
$"{sourceLang}_to_{targetLang}_output");
|
||||
|
||||
Console.WriteLine($"目标目录:{targetDir}");
|
||||
Console.WriteLine($"递归:{isRecursive}");
|
||||
Console.WriteLine();
|
||||
|
||||
var result = await batchService.ConvertDirectoryAsync(
|
||||
inputFile.FullName,
|
||||
targetDir,
|
||||
sourceLang,
|
||||
targetLang,
|
||||
options,
|
||||
context.GetCancellationToken()
|
||||
);
|
||||
|
||||
PrintBatchResult(result, verbose);
|
||||
context.ExitCode = result.Success ? 0 : 1;
|
||||
context.ExitCode = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 单文件转换模式
|
||||
Console.WriteLine($"📄 正在读取文件:{inputFile.FullName}");
|
||||
var sourceCode = await File.ReadAllTextAsync(inputFile.FullName);
|
||||
|
||||
var options = LoadConfiguration(configFile.FullName);
|
||||
|
||||
Console.WriteLine($"$\color{green}{正在转换:{sourceLang} → {targetLang}}");
|
||||
var conversionService = new ConversionService();
|
||||
var result = await conversionService.ConvertAsync(
|
||||
sourceCode, sourceLang, targetLang, options, context.GetCancellationToken());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
PrintConversionDetails(result, verbose);
|
||||
context.ExitCode = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"❌ 转换失败:{result.ErrorMessage}");
|
||||
context.ExitCode = 1;
|
||||
}
|
||||
Console.WriteLine($"Conversion failed: {result.ErrorMessage}");
|
||||
context.ExitCode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"❌ 错误:{ex.Message}");
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
if (verbose)
|
||||
{
|
||||
Console.WriteLine($"详情:{ex}");
|
||||
Console.WriteLine($"Details: {ex}");
|
||||
}
|
||||
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)
|
||||
var parser2 = new CommandLineBuilder(rootCommand)
|
||||
.UseDefaults()
|
||||
.Build();
|
||||
|
||||
return await parser.InvokeAsync(args);
|
||||
}
|
||||
|
||||
private static void PrintBatchResult(BatchConversionResult result, bool verbose)
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("==== 批量转换完成 ====");
|
||||
Console.WriteLine($"源目录:{result.SourceDirectory}");
|
||||
Console.WriteLine($"目标目录:{result.TargetDirectory}");
|
||||
Console.WriteLine($"总文件数:{result.TotalFiles}");
|
||||
Console.WriteLine($"成功:{result.SuccessfulFiles}");
|
||||
Console.WriteLine($"失败:{result.FailedFiles}");
|
||||
Console.WriteLine($"耗时:{result.Duration.TotalSeconds:F2} 秒");
|
||||
|
||||
if (result.ConvertedFiles.Any())
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("成功转换的文件:");
|
||||
foreach (var file in result.ConvertedFiles)
|
||||
{
|
||||
Console.WriteLine($" ✅ {Path.GetFileName(file.SourceFile)} → {Path.GetFileName(file.TargetFile)}");
|
||||
if (verbose)
|
||||
{
|
||||
Console.WriteLine($" 行数:{file.LinesConverted}, 类:{file.ClassesConverted}, 方法:{file.MethodsConverted}");
|
||||
if (file.Warnings > 0 || file.Issues > 0)
|
||||
{
|
||||
Console.WriteLine($" ⚠️ 警告:{file.Warnings}, 问题:{file.Issues}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.FailedFileList.Any())
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("转换失败的文件:");
|
||||
foreach (var file in result.FailedFileList)
|
||||
{
|
||||
Console.WriteLine($" ❌ {Path.GetFileName(file.SourceFile)}");
|
||||
if (verbose)
|
||||
{
|
||||
Console.WriteLine($" 错误:{file.ErrorMessage}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("🎉 所有文件转换成功!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"⚠️ {result.FailedFiles} 个文件转换失败");
|
||||
}
|
||||
}
|
||||
|
||||
private static void PrintConversionDetails(ConversionResult result, bool verbose)
|
||||
{
|
||||
if (!verbose) return;
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ConversionOptions? LoadConfiguration(string? configPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath))
|
||||
{
|
||||
if (configPath.EndsWith(".json"))
|
||||
{
|
||||
var json = File.ReadAllText(configPath);
|
||||
var options = JsonSerializer.Deserialize<ConversionOptions>(json);
|
||||
return options;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"⚠️ 加载配置文件失败:{ex.Message},使用默认配置");
|
||||
}
|
||||
|
||||
return new ConversionOptions
|
||||
{
|
||||
KeepComments = true,
|
||||
KeepDocStrings = true
|
||||
};
|
||||
return await parser2.InvokeAsync(args);
|
||||
}
|
||||
}
|
||||
|
||||
// 定义 stats 命令 - 显示转换统计
|
||||
var statsCommand = new Command("stats", "显示转换统计信息");
|
||||
statsCommand.SetHandler(async (context) =>
|
||||
{
|
||||
Console.WriteLine("📊 CodePlay 转换统计");
|
||||
Console.WriteLine("====================");
|
||||
|
||||
var reportService = new ReportStorageService();
|
||||
var stats = await reportService.GetStatisticsAsync();
|
||||
|
||||
Console.WriteLine($"总转换次数:{stats.TotalConversions}");
|
||||
Console.WriteLine($"总项目数:{stats.TotalProjects}");
|
||||
Console.WriteLine($"平均每行转换:{stats.AverageLinesConverted:F0}");
|
||||
Console.WriteLine($"总问题数:{stats.TotalIssuesDetected}");
|
||||
Console.WriteLine($"总 TODO 数:{stats.TotalTODOs}");
|
||||
|
||||
if (stats.ConversionsByLanguage.Any())
|
||||
{
|
||||
Console.WriteLine("\n按目标语言统计:");
|
||||
foreach (var (lang, count) in stats.ConversionsByLanguage)
|
||||
{
|
||||
Console.WriteLine($" {lang}: {count} 次");
|
||||
}
|
||||
}
|
||||
|
||||
context.ExitCode = 0;
|
||||
});
|
||||
|
||||
// 定义 config 命令 - 配置 CLI
|
||||
var configCommand = new Command("config", "配置 CLI 参数")
|
||||
{
|
||||
new Option<string>("--set", "设置配置项 (key=value)"),
|
||||
new Option<bool>("--show", "显示当前配置")
|
||||
};
|
||||
configCommand.SetHandler(async (context) =>
|
||||
{
|
||||
var show = context.ParseResult.GetValueForOption(configCommand.Options.First(o => o.Name == "--show")!);
|
||||
var set = context.ParseResult.GetValueForOption(configCommand.Options.First(o => o.Name == "--set")!);
|
||||
|
||||
var config = await Config.CliConfiguration.LoadAsync();
|
||||
|
||||
if (show)
|
||||
{
|
||||
Console.WriteLine("当前配置:");
|
||||
Console.WriteLine($" 默认源语言:{config.DefaultSourceLanguage}");
|
||||
Console.WriteLine($" 默认目标语言:{config.DefaultTargetLanguage}");
|
||||
Console.WriteLine($" 验证轮次:{config.DefaultValidationRounds}");
|
||||
Console.WriteLine($" 保持注释:{config.KeepComments}");
|
||||
Console.WriteLine($" 并发数:{config.MaxConcurrency}");
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(set))
|
||||
{
|
||||
var parts = set.Split('=');
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
var key = parts[0];
|
||||
var value = parts[1];
|
||||
|
||||
// TODO: 动态设置配置
|
||||
Console.WriteLine($"✅ 配置已更新:{key}={value}");
|
||||
}
|
||||
}
|
||||
|
||||
context.ExitCode = 0;
|
||||
});
|
||||
|
||||
// 添加到根命令
|
||||
rootCommand.Add(statsCommand);
|
||||
rootCommand.Add(configCommand);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using CodePlay.Core.Parsers;
|
||||
using CodePlay.Core.Converters;
|
||||
using CodePlay.Core.Common;
|
||||
|
||||
var parser = new CSharpParser();
|
||||
var converter = new CSharpToJavaConverter();
|
||||
|
||||
var code = @"
|
||||
namespace Test
|
||||
{
|
||||
public class Model
|
||||
{
|
||||
public int? Age { get; set; }
|
||||
}
|
||||
}
|
||||
";
|
||||
|
||||
var tree = await parser.ParseAsync(code);
|
||||
var result = await converter.ConvertAsync(tree, LanguageType.Java);
|
||||
|
||||
Console.WriteLine("Success: " + result.Success);
|
||||
Console.WriteLine("=== Code ===");
|
||||
Console.WriteLine(result.TransformedCode ?? "NULL");
|
||||
Reference in New Issue
Block a user