Skip to content

Mirror Source Folder Hierarchy In JavaTCSharpCli Output Directory #145

Description

@lordmilko

In JavaToCSharpCli, when processing a folder with subdirectories, all discovered files are output to the same output directory. I think it would be useful to able to retain the original folder structure, however I can also see scenarios where somebody might want to flatten everything, so I'm not sure to what extent the default should be changed and/or an option to configure this behavior should be added. I added support for mirroring the source folder structure in my local repo as follows

-private static void ConvertToCSharpDir(DirectoryInfo inputDirectory, DirectoryInfo outputDirectory, JavaConversionOptions options)
+private static void ConvertToCSharpDir(DirectoryInfo inputDirectory, DirectoryInfo rootOutputDirectory, JavaConversionOptions options)
{
    if (inputDirectory.Exists)
    {
        var searchOption = options.IncludeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
        foreach (var f in inputDirectory.GetFiles("*.java", searchOption))
        {
            string? directoryName = f.DirectoryName;
            if (string.IsNullOrWhiteSpace(directoryName))
            {
                continue;
            }

+            var relativePath = Path.GetDirectoryName(f.FullName.Replace(inputDirectory.FullName, string.Empty)?.TrimStart(Path.DirectorySeparatorChar));
+
+            var outputDirectory = string.IsNullOrEmpty(relativePath) ? rootOutputDirectory : new DirectoryInfo(Path.Combine(rootOutputDirectory.FullName, relativePath));
+
            if (!outputDirectory.Exists)
            {
                outputDirectory.Create();
            }

            ConvertToCSharpFile(f,
                new FileInfo(Path.Combine(outputDirectory.FullName, Path.ChangeExtension(f.Name, ".cs"))),
                options,
                false);
        }
    }
    else
    {
        _logger.LogError("Java input folder {path} doesn't exist!", inputDirectory);
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions