diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5fec5114..cdff9d1c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,10 +11,10 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v1 - - name: Setup .NET 8 + - name: Setup .NET 10 uses: actions/setup-dotnet@v1 with: - dotnet-version: 8.x + dotnet-version: 10.x - name: Build JavaToCSharp run: dotnet build ./JavaToCSharp/JavaToCSharp.csproj --configuration Release @@ -30,10 +30,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - - name: Setup .NET 8 + - name: Setup .NET 10 uses: actions/setup-dotnet@v1 with: - dotnet-version: 8.x + dotnet-version: 10.x - name: Build JavaToCSharp run: dotnet build ./JavaToCSharp/JavaToCSharp.csproj --configuration Release diff --git a/JavaToCSharp.Tests/JavaToCSharp.Tests.csproj b/JavaToCSharp.Tests/JavaToCSharp.Tests.csproj index a21d46f3..54e5aede 100644 --- a/JavaToCSharp.Tests/JavaToCSharp.Tests.csproj +++ b/JavaToCSharp.Tests/JavaToCSharp.Tests.csproj @@ -1,7 +1,7 @@  - net8.0 + net10.0 false enable latest @@ -10,17 +10,17 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + diff --git a/JavaToCSharp/JavaToCSharp.csproj b/JavaToCSharp/JavaToCSharp.csproj index aa310b6e..66903f14 100644 --- a/JavaToCSharp/JavaToCSharp.csproj +++ b/JavaToCSharp/JavaToCSharp.csproj @@ -1,7 +1,7 @@  4.0.0 - net8.0 + net10.0 enable enable nullable @@ -16,10 +16,10 @@ - - - - + + + + diff --git a/JavaToCSharpCli/JavaToCSharpCli.csproj b/JavaToCSharpCli/JavaToCSharpCli.csproj index a2a5b7cd..6c36c40a 100644 --- a/JavaToCSharpCli/JavaToCSharpCli.csproj +++ b/JavaToCSharpCli/JavaToCSharpCli.csproj @@ -2,15 +2,15 @@ 4.0.0 Exe - net8.0 + net10.0 latest enable enable nullable - - + + diff --git a/JavaToCSharpCli/Program.cs b/JavaToCSharpCli/Program.cs index 1301eb8d..5c831389 100644 --- a/JavaToCSharpCli/Program.cs +++ b/JavaToCSharpCli/Program.cs @@ -13,66 +13,76 @@ public class Program private static readonly ILoggerFactory _loggerFactory; private static readonly ILogger _logger; - private static readonly Option _includeSubdirectoriesOption = new( - name: "--include-subdirectories", - description: "When the command is dir, converts files in all subdirectories", - getDefaultValue: () => true); - - private static readonly Option _includeUsingsOption = new( - name: "--include-usings", - description: "Include using directives in output", - getDefaultValue: () => true); - - private static readonly Option _includeNamespaceOption = new( - name: "--include-namespace", - description: "Include namespace in output", - getDefaultValue: () => true); - - private static readonly Option _includeCommentsOption = new( - name: "--include-comments", - description: "Include comments in output", - getDefaultValue: () => true); - - private static readonly Option _useDebugAssertOption = new( - name: "--use-debug-assert", - description: "Use Debug.Assert for asserts", - getDefaultValue: () => false); - - private static readonly Option _startInterfaceNamesWithIOption = new( - name: "--start-interface-names-with-i", - description: "Prefix interface names with the letter I", - getDefaultValue: () => true); - - private static readonly Option _commentUnrecognizedCodeOption = new( - name: "--comment-unrecognized-code", - description: "Include unrecognized code in output as commented-out code", - getDefaultValue: () => true); - - private static readonly Option _systemOutToConsoleOption = new( - name: "--system-out-to-console", - description: "Convert System.out calls to Console", - getDefaultValue: () => false); - - private static readonly Option _fileScopedNamespacesOption = new( - name: "--file-scoped-namespaces", - description: "Use file-scoped namespaces in C# output", - getDefaultValue: () => false); - - private static readonly Option _clearDefaultUsingsOption = new( - name: "--clear-usings", - description: "Remove all default usings provided by this app", - getDefaultValue: () => false); - - private static readonly Option> _addUsingsOption = new( - name: "--add-using", - description: "Adds a using directive to the collection of usings") + private static readonly Option _includeSubdirectoriesOption = new("--include-subdirectories") { - ArgumentHelpName = "namespace" + Description = "When the command is dir, converts files in all subdirectories", + DefaultValueFactory = _ => true, }; - private static readonly Option _mappingsFileNameOption = new( - name: "--mappings-file", - description: "A yaml file with syntax mappings from imports, methods and annotations"); + private static readonly Option _includeUsingsOption = new("--include-usings") + { + Description = "Include using directives in output", + DefaultValueFactory = _ => true, + }; + + private static readonly Option _includeNamespaceOption = new("--include-namespace") + { + Description = "Include namespace in output", + DefaultValueFactory = _ => true, + }; + + private static readonly Option _includeCommentsOption = new("--include-comments") + { + Description = "Include comments in output", + DefaultValueFactory = _ => true, + }; + + private static readonly Option _useDebugAssertOption = new("--use-debug-assert") + { + Description = "Use Debug.Assert for asserts", + DefaultValueFactory = _ => false, + }; + + private static readonly Option _startInterfaceNamesWithIOption = new("--start-interface-names-with-i") + { + Description = "Prefix interface names with the letter I", + DefaultValueFactory = _ => true, + }; + + private static readonly Option _commentUnrecognizedCodeOption = new("--comment-unrecognized-code") + { + Description = "Include unrecognized code in output as commented-out code", + DefaultValueFactory = _ => true, + }; + + private static readonly Option _systemOutToConsoleOption = new("--system-out-to-console") + { + Description = "Convert System.out calls to Console", + DefaultValueFactory = _ => false, + }; + + private static readonly Option _fileScopedNamespacesOption = new("--file-scoped-namespaces") + { + Description = "Use file-scoped namespaces in C# output", + DefaultValueFactory = _ => false, + }; + + private static readonly Option _clearDefaultUsingsOption = new("--clear-usings") + { + Description = "Remove all default usings provided by this app", + DefaultValueFactory = _ => false, + }; + + private static readonly Option> _addUsingsOption = new("--add-using") + { + Description = "Adds a using directive to the collection of usings", + HelpName = "namespace", + }; + + private static readonly Option _mappingsFileNameOption = new("--mappings-file") + { + Description = "A yaml file with syntax mappings from imports, methods and annotations", + }; static Program() { @@ -85,26 +95,26 @@ public static async Task Main(string[] args) { var rootCommand = new RootCommand("Java to C# Converter") { - Description = "A syntactic transformer of source code from Java to C#." + Description = "A syntactic transformer of source code from Java to C#.", }; - rootCommand.AddCommand(CreateFileCommand()); - rootCommand.AddCommand(CreateDirectoryCommand()); + rootCommand.Subcommands.Add(CreateFileCommand()); + rootCommand.Subcommands.Add(CreateDirectoryCommand()); - rootCommand.AddGlobalOption(_includeSubdirectoriesOption); - rootCommand.AddGlobalOption(_includeUsingsOption); - rootCommand.AddGlobalOption(_includeNamespaceOption); - rootCommand.AddGlobalOption(_includeCommentsOption); - rootCommand.AddGlobalOption(_useDebugAssertOption); - rootCommand.AddGlobalOption(_startInterfaceNamesWithIOption); - rootCommand.AddGlobalOption(_commentUnrecognizedCodeOption); - rootCommand.AddGlobalOption(_systemOutToConsoleOption); - rootCommand.AddGlobalOption(_fileScopedNamespacesOption); - rootCommand.AddGlobalOption(_clearDefaultUsingsOption); - rootCommand.AddGlobalOption(_addUsingsOption); - rootCommand.AddGlobalOption(_mappingsFileNameOption); + rootCommand.Options.Add(_includeSubdirectoriesOption); + rootCommand.Options.Add(_includeUsingsOption); + rootCommand.Options.Add(_includeNamespaceOption); + rootCommand.Options.Add(_includeCommentsOption); + rootCommand.Options.Add(_useDebugAssertOption); + rootCommand.Options.Add(_startInterfaceNamesWithIOption); + rootCommand.Options.Add(_commentUnrecognizedCodeOption); + rootCommand.Options.Add(_systemOutToConsoleOption); + rootCommand.Options.Add(_fileScopedNamespacesOption); + rootCommand.Options.Add(_clearDefaultUsingsOption); + rootCommand.Options.Add(_addUsingsOption); + rootCommand.Options.Add(_mappingsFileNameOption); - await rootCommand.InvokeAsync(args); + await rootCommand.Parse(args).InvokeAsync(); // flush logs _loggerFactory.Dispose(); @@ -112,48 +122,50 @@ public static async Task Main(string[] args) private static Command CreateFileCommand() { - var inputArgument = new Argument( - name: "input", - description: "A Java source code file to convert"); + var inputArgument = new Argument("input") + { + Description = "A Java source code file to convert", + }; - var outputArgument = new Argument( - name: "output", - description: "Path to place the C# output file, or stdout if omitted", - getDefaultValue: () => null); + var outputArgument = new Argument("output") + { + Description = "Path to place the C# output file, or stdout if omitted", + DefaultValueFactory = _ => null, + }; var fileCommand = new Command("file", "Convert a Java file to C#"); - fileCommand.AddArgument(inputArgument); - fileCommand.AddArgument(outputArgument); + fileCommand.Arguments.Add(inputArgument); + fileCommand.Arguments.Add(outputArgument); - fileCommand.SetHandler(context => + fileCommand.SetAction(context => { - var input = context.ParseResult.GetValueForArgument(inputArgument); - var output = context.ParseResult.GetValueForArgument(outputArgument); + var input = context.GetValue(inputArgument); + var output = context.GetValue(outputArgument); var options = GetJavaConversionOptions(context); - ConvertToCSharpFile(input, output, options); + ConvertToCSharpFile(input!, output, options); }); return fileCommand; } - private static JavaConversionOptions GetJavaConversionOptions(InvocationContext context) + private static JavaConversionOptions GetJavaConversionOptions(ParseResult context) { var options = new JavaConversionOptions { - IncludeSubdirectories = context.ParseResult.GetValueForOption(_includeSubdirectoriesOption), - IncludeUsings = context.ParseResult.GetValueForOption(_includeUsingsOption), - IncludeComments = context.ParseResult.GetValueForOption(_includeCommentsOption), - IncludeNamespace = context.ParseResult.GetValueForOption(_includeNamespaceOption), - ConvertSystemOutToConsole = context.ParseResult.GetValueForOption(_systemOutToConsoleOption), - StartInterfaceNamesWithI = context.ParseResult.GetValueForOption(_startInterfaceNamesWithIOption), - UseDebugAssertForAsserts = context.ParseResult.GetValueForOption(_useDebugAssertOption), - UseUnrecognizedCodeToComment = context.ParseResult.GetValueForOption(_commentUnrecognizedCodeOption), - UseFileScopedNamespaces = context.ParseResult.GetValueForOption(_fileScopedNamespacesOption), + IncludeSubdirectories = context.GetValue(_includeSubdirectoriesOption), + IncludeUsings = context.GetValue(_includeUsingsOption), + IncludeComments = context.GetValue(_includeCommentsOption), + IncludeNamespace = context.GetValue(_includeNamespaceOption), + ConvertSystemOutToConsole = context.GetValue(_systemOutToConsoleOption), + StartInterfaceNamesWithI = context.GetValue(_startInterfaceNamesWithIOption), + UseDebugAssertForAsserts = context.GetValue(_useDebugAssertOption), + UseUnrecognizedCodeToComment = context.GetValue(_commentUnrecognizedCodeOption), + UseFileScopedNamespaces = context.GetValue(_fileScopedNamespacesOption), }; - if (context.ParseResult.GetValueForOption(_clearDefaultUsingsOption)) + if (context.GetValue(_clearDefaultUsingsOption)) { options.ClearUsings(); } @@ -166,12 +178,12 @@ private static JavaConversionOptions GetJavaConversionOptions(InvocationContext options.AddUsing("System.Text"); } - foreach (string ns in context.ParseResult.GetValueForOption(_addUsingsOption) ?? new List()) + foreach (string ns in context.GetValue(_addUsingsOption) ?? new List()) { options.AddUsing(ns); } - var mappingsFile = context.ParseResult.GetValueForOption(_mappingsFileNameOption); + var mappingsFile = context.GetValue(_mappingsFileNameOption); if (!string.IsNullOrEmpty(mappingsFile)) { options.SyntaxMappings = ReadMappingsFile(mappingsFile); @@ -189,26 +201,28 @@ private static SyntaxMapping ReadMappingsFile(string mappingsFile) private static Command CreateDirectoryCommand() { - var inputArgument = new Argument( - name: "input", - description: "A directory containing Java source code files to convert"); + var inputArgument = new Argument("input") + { + Description = "A directory containing Java source code files to convert" + }; - var outputArgument = new Argument( - name: "output", - description: "Path to place the C# output files"); + var outputArgument = new Argument("output") + { + Description = "Path to place the C# output files" + }; var dirCommand = new Command("dir", "Convert a directory containing Java files to C#"); - dirCommand.AddArgument(inputArgument); - dirCommand.AddArgument(outputArgument); + dirCommand.Arguments.Add(inputArgument); + dirCommand.Arguments.Add(outputArgument); - dirCommand.SetHandler(context => + dirCommand.SetAction(context => { - var input = context.ParseResult.GetValueForArgument(inputArgument); - var output = context.ParseResult.GetValueForArgument(outputArgument); + var input = context.GetValue(inputArgument); + var output = context.GetValue(outputArgument); var options = GetJavaConversionOptions(context); - ConvertToCSharpDir(input, output, options); + ConvertToCSharpDir(input!, output!, options); }); return dirCommand; @@ -239,13 +253,17 @@ private static void ConvertToCSharpDir(DirectoryInfo inputDirectory, DirectoryIn } } else + { _logger.LogError("Java input folder {path} doesn't exist!", inputDirectory); + } } private static void ConvertToCSharpFile(FileSystemInfo inputFile, FileSystemInfo? outputFile, JavaConversionOptions options, bool overwrite = true) { if (!overwrite && outputFile is { Exists: true }) + { _logger.LogInformation("{outputFilePath} exists, skip to next.", outputFile); + } else if (inputFile.Exists) { try @@ -283,7 +301,9 @@ private static void ConvertToCSharpFile(FileSystemInfo inputFile, FileSystemInfo } } else + { _logger.LogError("Java input file {filePath} doesn't exist!", inputFile.FullName); + } } private static void OutputFileOrPrint(string? fileName, string contents) diff --git a/JavaToCSharpGui/App.axaml b/JavaToCSharpGui/App.axaml index 3253d7b7..3128bd0d 100644 --- a/JavaToCSharpGui/App.axaml +++ b/JavaToCSharpGui/App.axaml @@ -1,5 +1,6 @@  @@ -9,7 +10,7 @@ - - + + diff --git a/JavaToCSharpGui/JavaToCSharpGui.csproj b/JavaToCSharpGui/JavaToCSharpGui.csproj index 7c2ed3c4..170f0084 100644 --- a/JavaToCSharpGui/JavaToCSharpGui.csproj +++ b/JavaToCSharpGui/JavaToCSharpGui.csproj @@ -4,7 +4,7 @@ true app.manifest WinExe - net8.0 + net10.0 latest enable enable @@ -16,21 +16,22 @@ - - - + + + - + - - - - + + + + - - - - + + + + +