From f6aa5258efcb4b1c9ff655b14773709d1235adab Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 3 Aug 2024 07:36:35 -0700 Subject: [PATCH] Normalized tool arguments setup and processing --- .../AbstractToolProviderOperation.java | 122 ++++++++++++------ .../rife/bld/operations/JlinkOperation.java | 61 +++------ .../rife/bld/operations/JmodOperation.java | 63 +++++---- .../bld/operations/JpackageOperation.java | 17 +-- 4 files changed, 137 insertions(+), 126 deletions(-) diff --git a/src/main/java/rife/bld/operations/AbstractToolProviderOperation.java b/src/main/java/rife/bld/operations/AbstractToolProviderOperation.java index a1f1863..c2ec9bc 100644 --- a/src/main/java/rife/bld/operations/AbstractToolProviderOperation.java +++ b/src/main/java/rife/bld/operations/AbstractToolProviderOperation.java @@ -6,9 +6,12 @@ package rife.bld.operations; import rife.bld.operations.exceptions.ExitStatusException; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Scanner; import java.util.spi.ToolProvider; /** @@ -31,47 +34,6 @@ public abstract class AbstractToolProviderOperation args) { - args.forEach((k, v) -> { - toolArgs_.add(k); - if (v != null && !v.isEmpty()) { - toolArgs_.add(v); - } - }); - return (T) this; - } - - /** - * Adds tool command line arguments. - * - * @param args the argument to add - * @return this operation - */ - @SuppressWarnings({"unchecked", "UnusedReturnValue"}) - public T addArgs(List args) { - toolArgs_.addAll(args); - return (T) this; - } - - /** - * Adds tool command line arguments. - * - * @param arg one or more argument - * @return this operation - */ - @SuppressWarnings("unchecked") - public T addArgs(String... arg) { - addArgs(List.of(arg)); - return (T) this; - } - /** * Runs an instance of the tool. *

@@ -85,6 +47,7 @@ public abstract class AbstractToolProviderOperation new IllegalStateException("No " + toolName_ + " tool found.")); @@ -92,17 +55,92 @@ public abstract class AbstractToolProviderOperation args) { + args.forEach((k, v) -> { + toolArgs_.add(k); + if (v != null && !v.isEmpty()) { + toolArgs_.add(v); + } + }); + return (T) this; + } + + /** + * Adds arguments to pass to the tool. + * + * @param args the argument to add + * @return this operation + */ + @SuppressWarnings({"unchecked", "UnusedReturnValue"}) + public T toolArgs(List args) { + toolArgs_.addAll(args); + return (T) this; + } + + /** + * Returns the tool's arguments. * * @return the arguments */ public List toolArgs() { return toolArgs_; } + + /** + * Parses arguments to pass to the tool from the given files. + * + * @param files the list of files + * @return this operation instance + * @throws FileNotFoundException if a file cannot be found + */ + @SuppressWarnings({"unchecked", "UnusedReturnValue"}) + public T toolArgsFromFile(List files) throws FileNotFoundException { + var list = new ArrayList(); + + for (var option : files) { + try (var scanner = new Scanner(new File(option))) { + while (scanner.hasNext()) { + var splitLine = scanner.nextLine().split("--"); + for (String args : splitLine) { + if (!args.isBlank()) { + var splitArgs = args.split(" ", 2); + list.add("--" + splitArgs[0]); + if (splitArgs.length > 1 && !splitArgs[1].isBlank()) { + list.add(splitArgs[1]); + } + } + } + } + } + } + + toolArgs(list); + + return (T) this; + } } diff --git a/src/main/java/rife/bld/operations/JlinkOperation.java b/src/main/java/rife/bld/operations/JlinkOperation.java index 907a817..0956063 100644 --- a/src/main/java/rife/bld/operations/JlinkOperation.java +++ b/src/main/java/rife/bld/operations/JlinkOperation.java @@ -5,12 +5,9 @@ package rife.bld.operations; -import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Scanner; /** * Create run-time images using the jlink tool. @@ -21,7 +18,7 @@ import java.util.Scanner; public class JlinkOperation extends AbstractToolProviderOperation { private final List disabledPlugins_ = new ArrayList<>(); private final JlinkOptions jlinkOptions_ = new JlinkOptions(); - private final List options_ = new ArrayList<>(); + private final List fileOptions_ = new ArrayList<>(); public JlinkOperation() { super("jlink"); @@ -40,9 +37,9 @@ public class JlinkOperation extends AbstractToolProviderOperation addArgs("--disable-plugin", plugin)); - addArgs(jlinkOptions_); - addArgs(parseOptions()); + toolArgsFromFile(fileOptions_); + disabledPlugins_.forEach(plugin -> toolArgs("--disable-plugin", plugin)); + toolArgs(jlinkOptions_); super.execute(); } @@ -70,24 +67,14 @@ public class JlinkOperation extends AbstractToolProviderOperation options() { - return options_; + public List fileOptions() { + return fileOptions_; } - // Shouldn't be needed, but jlink doesn't support @filename when called via ToolProvider - private List parseOptions() throws FileNotFoundException { - var list = new ArrayList(); - - for (var option : options_) { - try (var scanner = new Scanner(new File(option))) { - while (scanner.hasNext()) { - var splitLine = scanner.nextLine().split("--"); - for (String args : splitLine) { - if (!args.isBlank()) { - var splitArgs = args.split(" ", 2); - list.add("--" + splitArgs[0]); - if (splitArgs.length > 1 && !splitArgs[1].isBlank()) { - list.add(splitArgs[1]); - } - } - } - } - } - } - - return list; + /** + * List available plugins. + * + * @return this operation instance + */ + public JlinkOperation listPlugins() { + toolArgs("--list-plugins"); + return this; } } diff --git a/src/main/java/rife/bld/operations/JmodOperation.java b/src/main/java/rife/bld/operations/JmodOperation.java index 58ed970..1821598 100644 --- a/src/main/java/rife/bld/operations/JmodOperation.java +++ b/src/main/java/rife/bld/operations/JmodOperation.java @@ -5,8 +5,6 @@ package rife.bld.operations; -import rife.bld.operations.exceptions.ExitStatusException; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -18,8 +16,8 @@ import java.util.Map; * @since 2.0.2 */ public class JmodOperation extends AbstractToolProviderOperation { + private final List fileOptions_ = new ArrayList<>(); private final JmodOptions jmodOptions_ = new JmodOptions(); - private final List options_ = new ArrayList<>(); private String jmodFile_; private OperationMode operationMode_; @@ -29,19 +27,40 @@ public class JmodOperation extends AbstractToolProviderOperation @Override public void execute() throws Exception { - if (operationMode_ == null) { - System.err.println("Operation mode not set."); - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); - } else if (jmodFile_ == null) { - System.err.println("Jmod file not set."); - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + if (operationMode_ != null) { + toolArgs(operationMode_.mode); } - addArgs(operationMode_.mode); - addArgs(jmodOptions_); - addArgs(jmodFile_); + + toolArgsFromFile(fileOptions_); + toolArgs(jmodOptions_); + + if (jmodFile_ != null) { + toolArgs(jmodFile_); + } + super.execute(); } + /** + * Retrieves the list of files containing options or mode. + * + * @return the list of files + */ + public List fileOptions() { + return fileOptions_; + } + + /** + * Read options and/or mode from a file. + * + * @param file one or more file + * @return this operation instance + */ + public JmodOperation fileOptions(String... file) { + fileOptions_.addAll(List.of(file)); + return this; + } + /** * Retrieves the name of the JMOD file to create or from which to retrieve information. * @@ -101,26 +120,6 @@ public class JmodOperation extends AbstractToolProviderOperation return this; } - /** - * Retrieves the list of files containing options or mode. - * - * @return the list of files - */ - public List options() { - return options_; - } - - /** - * Read options and/or mode from a file. - * - * @param filename one or more file - * @return this operation instance - */ - public JmodOperation options(String... filename) { - options_.addAll(List.of(filename)); - return this; - } - /** * The operation modes. */ diff --git a/src/main/java/rife/bld/operations/JpackageOperation.java b/src/main/java/rife/bld/operations/JpackageOperation.java index fabada1..ca47e49 100644 --- a/src/main/java/rife/bld/operations/JpackageOperation.java +++ b/src/main/java/rife/bld/operations/JpackageOperation.java @@ -16,8 +16,8 @@ import java.util.Map; * @since 2.0.2 */ public class JpackageOperation extends AbstractToolProviderOperation { + private final List fileOptions_ = new ArrayList<>(); private final JpackageOptions jpackageOptions_ = new JpackageOptions(); - private final List options_ = new ArrayList<>(); public JpackageOperation() { super("jpackage"); @@ -25,8 +25,8 @@ public class JpackageOperation extends AbstractToolProviderOperation '@' + opt).toList()); + toolArgs(fileOptions_.stream().map(opt -> '@' + opt).toList()); + toolArgs(jpackageOptions_); super.execute(); } @@ -35,17 +35,18 @@ public class JpackageOperation extends AbstractToolProviderOperation options(){ - return options_; + public List fileOptions() { + return fileOptions_; } + /** * Read options and/or mode from a file. * - * @param filename one or more file + * @param file one or more file * @return this operation instance */ - public JpackageOperation options(String... filename) { - options_.addAll(List.of(filename)); + public JpackageOperation fileOptions(String... file) { + fileOptions_.addAll(List.of(file)); return this; }