From b84f9de184817a58f76853009d27313832cdf930 Mon Sep 17 00:00:00 2001 From: Javier <10879637+javiertuya@users.noreply.github.com> Date: Sun, 25 Aug 2024 20:53:17 +0200 Subject: [PATCH] Option to convert files excluding subdirectories (#123) --- JavaToCSharp/JavaConversionOptions.cs | 2 ++ JavaToCSharpCli/Program.cs | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/JavaToCSharp/JavaConversionOptions.cs b/JavaToCSharp/JavaConversionOptions.cs index 2d0c0e57..6aa25cf6 100644 --- a/JavaToCSharp/JavaConversionOptions.cs +++ b/JavaToCSharp/JavaConversionOptions.cs @@ -14,6 +14,8 @@ public class JavaConversionOptions public IList StaticUsingEnumNames { get; } = new List(); + public bool IncludeSubdirectories { get; set; } = true; + public bool IncludeUsings { get; set; } = true; public bool IncludeNamespace { get; set; } = true; diff --git a/JavaToCSharpCli/Program.cs b/JavaToCSharpCli/Program.cs index 4107ac80..b61acef6 100644 --- a/JavaToCSharpCli/Program.cs +++ b/JavaToCSharpCli/Program.cs @@ -13,6 +13,11 @@ 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", @@ -82,6 +87,7 @@ public static async Task Main(string[] args) rootCommand.AddCommand(CreateFileCommand()); rootCommand.AddCommand(CreateDirectoryCommand()); + rootCommand.AddGlobalOption(_includeSubdirectoriesOption); rootCommand.AddGlobalOption(_includeUsingsOption); rootCommand.AddGlobalOption(_includeNamespaceOption); rootCommand.AddGlobalOption(_includeCommentsOption); @@ -131,6 +137,7 @@ private static JavaConversionOptions GetJavaConversionOptions(InvocationContext { var options = new JavaConversionOptions { + IncludeSubdirectories = context.ParseResult.GetValueForOption(_includeSubdirectoriesOption), IncludeUsings = context.ParseResult.GetValueForOption(_includeUsingsOption), IncludeComments = context.ParseResult.GetValueForOption(_includeCommentsOption), IncludeNamespace = context.ParseResult.GetValueForOption(_includeNamespaceOption), @@ -193,7 +200,8 @@ private static void ConvertToCSharpDir(DirectoryInfo inputDirectory, DirectoryIn { if (inputDirectory.Exists) { - foreach (var f in inputDirectory.GetFiles("*.java", SearchOption.AllDirectories)) + var searchOption = options.IncludeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + foreach (var f in inputDirectory.GetFiles("*.java", searchOption)) { string? directoryName = f.DirectoryName; if (string.IsNullOrWhiteSpace(directoryName))