From 8e4ca7f61fd4ecdf9f6a9bda49c37987f87df44d Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Thu, 3 Oct 2024 12:30:26 -0500 Subject: [PATCH 1/2] Allow JST to be applied with the same configuration to multiple files at once --- .../main/java/net/neoforged/jst/cli/Main.java | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/cli/src/main/java/net/neoforged/jst/cli/Main.java b/cli/src/main/java/net/neoforged/jst/cli/Main.java index 613d2e1..22f9ccf 100644 --- a/cli/src/main/java/net/neoforged/jst/cli/Main.java +++ b/cli/src/main/java/net/neoforged/jst/cli/Main.java @@ -17,24 +17,29 @@ @CommandLine.Command(name = "jst", mixinStandardHelpOptions = true, usageHelpWidth = 100) public class Main implements Callable { - @CommandLine.Parameters(index = "0", paramLabel = "INPUT", description = "Path to a single Java-file, a source-archive or a folder containing the source to transform.") - Path inputPath; + @CommandLine.ArgGroup(exclusive = false, multiplicity = "1..*") + List tasks; + + static class Task { + @CommandLine.Parameters(index = "0", paramLabel = "INPUT", description = "Path to a single Java-file, a source-archive or a folder containing the source to transform.") + Path inputPath; - @CommandLine.Parameters(index = "1", paramLabel = "OUTPUT", description = "Path to where the resulting source should be placed.") - Path outputPath; + @CommandLine.Parameters(index = "1", paramLabel = "OUTPUT", description = "Path to where the resulting source should be placed.") + Path outputPath; - @CommandLine.Option(names = "--in-format", description = "Specify the format of INPUT explicitly. AUTO (the default) performs auto-detection. Other options are SINGLE_FILE for Java files, ARCHIVE for source jars or zips, and FOLDER for folders containing Java code.") - PathType inputFormat = PathType.AUTO; + @CommandLine.Option(names = "--in-format", description = "Specify the format of INPUT explicitly. AUTO (the default) performs auto-detection. Other options are SINGLE_FILE for Java files, ARCHIVE for source jars or zips, and FOLDER for folders containing Java code.") + PathType inputFormat = PathType.AUTO; - @CommandLine.Option(names = "--out-format", description = "Specify the format of OUTPUT explicitly. Allows the same options as --in-format.") - PathType outputFormat = PathType.AUTO; + @CommandLine.Option(names = "--out-format", description = "Specify the format of OUTPUT explicitly. Allows the same options as --in-format.") + PathType outputFormat = PathType.AUTO; + + @CommandLine.Option(names = "--ignore-prefix", description = "Do not apply transformations to paths that start with any of these prefixes.") + List ignoredPrefixes = new ArrayList<>(); + } @CommandLine.Option(names = "--libraries-list", description = "Specifies a file that contains a path to an archive or directory to add to the classpath on each line.") Path librariesList; - @CommandLine.Option(names = "--ignore-prefix", description = "Do not apply transformations to paths that start with any of these prefixes.") - List ignoredPrefixes = new ArrayList<>(); - @CommandLine.Option(names = "--classpath", description = "Additional classpath entries to use. Is combined with --libraries-list.", converter = ClasspathConverter.class) List addToClasspath = new ArrayList<>(); @@ -67,30 +72,31 @@ public static int innerMain(String... args) { @Override public Integer call() throws Exception { var logger = debug ? new Logger(System.out, System.err) : new Logger(null, System.err); - try (var source = FileSources.create(inputPath, inputFormat); - var processor = new SourceFileProcessor(logger)) { + for (var task : tasks) { + try (var source = FileSources.create(task.inputPath, task.inputFormat); + var processor = new SourceFileProcessor(logger)) { - if (librariesList != null) { - processor.addLibrariesList(librariesList); - } - for (Path path : addToClasspath) { - processor.addLibrary(path); - } - for (String ignoredPrefix : ignoredPrefixes) { - processor.addIgnoredPrefix(ignoredPrefix); - } + if (librariesList != null) { + processor.addLibrariesList(librariesList); + } + for (Path path : addToClasspath) { + processor.addLibrary(path); + } + for (String ignoredPrefix : task.ignoredPrefixes) { + processor.addIgnoredPrefix(ignoredPrefix); + } - processor.setMaxQueueDepth(maxQueueDepth); + processor.setMaxQueueDepth(maxQueueDepth); - var orderedTransformers = new ArrayList<>(enabledTransformers); + var orderedTransformers = new ArrayList<>(enabledTransformers); - try (var sink = FileSinks.create(outputPath, outputFormat, source)) { - if (!processor.process(source, sink, orderedTransformers)) { - logger.error("Transformation failed"); - return 1; + try (var sink = FileSinks.create(task.outputPath, task.outputFormat, source)) { + if (!processor.process(source, sink, orderedTransformers)) { + logger.error("Transformation failed"); + return 1; + } } } - } return 0; From 3b29958cd1fb2be5f4e2c12fea5082a61901921c Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Thu, 3 Oct 2024 12:34:28 -0500 Subject: [PATCH 2/2] Avoid repetitive setting up of processor classpath --- .../main/java/net/neoforged/jst/cli/Main.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cli/src/main/java/net/neoforged/jst/cli/Main.java b/cli/src/main/java/net/neoforged/jst/cli/Main.java index 22f9ccf..af20cf8 100644 --- a/cli/src/main/java/net/neoforged/jst/cli/Main.java +++ b/cli/src/main/java/net/neoforged/jst/cli/Main.java @@ -72,28 +72,30 @@ public static int innerMain(String... args) { @Override public Integer call() throws Exception { var logger = debug ? new Logger(System.out, System.err) : new Logger(null, System.err); - for (var task : tasks) { - try (var source = FileSources.create(task.inputPath, task.inputFormat); - var processor = new SourceFileProcessor(logger)) { + try (var processor = new SourceFileProcessor(logger)) { + if (librariesList != null) { + processor.addLibrariesList(librariesList); + } + for (Path path : addToClasspath) { + processor.addLibrary(path); + } - if (librariesList != null) { - processor.addLibrariesList(librariesList); - } - for (Path path : addToClasspath) { - processor.addLibrary(path); - } - for (String ignoredPrefix : task.ignoredPrefixes) { - processor.addIgnoredPrefix(ignoredPrefix); - } + processor.setMaxQueueDepth(maxQueueDepth); - processor.setMaxQueueDepth(maxQueueDepth); + var orderedTransformers = new ArrayList<>(enabledTransformers); - var orderedTransformers = new ArrayList<>(enabledTransformers); - try (var sink = FileSinks.create(task.outputPath, task.outputFormat, source)) { - if (!processor.process(source, sink, orderedTransformers)) { - logger.error("Transformation failed"); - return 1; + for (var task : tasks) { + try (var source = FileSources.create(task.inputPath, task.inputFormat)) { + for (String ignoredPrefix : task.ignoredPrefixes) { + processor.addIgnoredPrefix(ignoredPrefix); + } + + try (var sink = FileSinks.create(task.outputPath, task.outputFormat, source)) { + if (!processor.process(source, sink, orderedTransformers)) { + logger.error("Transformation failed"); + return 1; + } } } }