2
0
Fork 0
mirror of https://github.com/ethauvin/bld.git synced 2025-04-25 16:27:11 -07:00

Normalized tool arguments setup and processing

This commit is contained in:
Erik C. Thauvin 2024-08-03 07:36:35 -07:00
parent 94225dfb7a
commit f6aa5258ef
Signed by: erik
GPG key ID: 776702A6A2DA330E
4 changed files with 137 additions and 126 deletions

View file

@ -6,9 +6,12 @@ package rife.bld.operations;
import rife.bld.operations.exceptions.ExitStatusException; import rife.bld.operations.exceptions.ExitStatusException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
import java.util.spi.ToolProvider; import java.util.spi.ToolProvider;
/** /**
@ -31,47 +34,6 @@ public abstract class AbstractToolProviderOperation<T extends AbstractToolProvid
toolName_ = toolName; toolName_ = toolName;
} }
/**
* Adds tool command line arguments.
*
* @param args the argument-value pairs to add
* @return this operation
*/
@SuppressWarnings({"unchecked", "UnusedReturnValue"})
protected T addArgs(Map<String, String> 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<String> 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. * Runs an instance of the tool.
* <p> * <p>
@ -85,6 +47,7 @@ public abstract class AbstractToolProviderOperation<T extends AbstractToolProvid
System.err.println("No " + toolName_ + " command line arguments specified."); System.err.println("No " + toolName_ + " command line arguments specified.");
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} }
var tool = ToolProvider.findFirst(toolName_).orElseThrow(() -> var tool = ToolProvider.findFirst(toolName_).orElseThrow(() ->
new IllegalStateException("No " + toolName_ + " tool found.")); new IllegalStateException("No " + toolName_ + " tool found."));
@ -92,17 +55,92 @@ public abstract class AbstractToolProviderOperation<T extends AbstractToolProvid
if (status != 0) { if (status != 0) {
System.out.println(tool.name() + ' ' + String.join(" ", toolArgs_)); System.out.println(tool.name() + ' ' + String.join(" ", toolArgs_));
} }
ExitStatusException.throwOnFailure(status); ExitStatusException.throwOnFailure(status);
toolArgs_.clear(); toolArgs_.clear();
} }
/** /**
* Returns the tool command line arguments. * Adds arguments to pass to the tool.
*
* @param arg one or more argument
* @return this operation
*/
@SuppressWarnings("unchecked")
public T toolArgs(String... arg) {
toolArgs(List.of(arg));
return (T) this;
}
/**
* Adds arguments to pass to the tool.
*
* @param args the argument-value pairs to add
* @return this operation
*/
@SuppressWarnings({"unchecked", "UnusedReturnValue"})
protected T toolArgs(Map<String, String> 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<String> args) {
toolArgs_.addAll(args);
return (T) this;
}
/**
* Returns the tool's arguments.
* *
* @return the arguments * @return the arguments
*/ */
public List<String> toolArgs() { public List<String> toolArgs() {
return 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<String> files) throws FileNotFoundException {
var list = new ArrayList<String>();
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;
}
} }

View file

@ -5,12 +5,9 @@
package rife.bld.operations; package rife.bld.operations;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
/** /**
* Create run-time images using the jlink tool. * Create run-time images using the jlink tool.
@ -21,7 +18,7 @@ import java.util.Scanner;
public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation> { public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation> {
private final List<String> disabledPlugins_ = new ArrayList<>(); private final List<String> disabledPlugins_ = new ArrayList<>();
private final JlinkOptions jlinkOptions_ = new JlinkOptions(); private final JlinkOptions jlinkOptions_ = new JlinkOptions();
private final List<String> options_ = new ArrayList<>(); private final List<String> fileOptions_ = new ArrayList<>();
public JlinkOperation() { public JlinkOperation() {
super("jlink"); super("jlink");
@ -40,9 +37,9 @@ public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
disabledPlugins_.forEach(plugin -> addArgs("--disable-plugin", plugin)); toolArgsFromFile(fileOptions_);
addArgs(jlinkOptions_); disabledPlugins_.forEach(plugin -> toolArgs("--disable-plugin", plugin));
addArgs(parseOptions()); toolArgs(jlinkOptions_);
super.execute(); super.execute();
} }
@ -70,24 +67,14 @@ public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation
return this; return this;
} }
/**
* List available plugins.
*
* @return this operation instance
*/
public JlinkOperation listPlugins() {
addArgs("--list-plugins");
return this;
}
/** /**
* Read options and/or mode from a file. * Read options and/or mode from a file.
* *
* @param filename one or more file * @param file one or more file
* @return this operation instance * @return this operation instance
*/ */
public JlinkOperation options(String... filename) { public JlinkOperation fileOptions(String... file) {
options_.addAll(List.of(filename)); fileOptions_.addAll(List.of(file));
return this; return this;
} }
@ -96,31 +83,17 @@ public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation
* *
* @return the list of files * @return the list of files
*/ */
public List<String> options() { public List<String> fileOptions() {
return options_; return fileOptions_;
} }
// Shouldn't be needed, but jlink doesn't support @filename when called via ToolProvider /**
private List<String> parseOptions() throws FileNotFoundException { * List available plugins.
var list = new ArrayList<String>(); *
* @return this operation instance
for (var option : options_) { */
try (var scanner = new Scanner(new File(option))) { public JlinkOperation listPlugins() {
while (scanner.hasNext()) { toolArgs("--list-plugins");
var splitLine = scanner.nextLine().split("--"); return this;
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;
} }
} }

View file

@ -5,8 +5,6 @@
package rife.bld.operations; package rife.bld.operations;
import rife.bld.operations.exceptions.ExitStatusException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,8 +16,8 @@ import java.util.Map;
* @since 2.0.2 * @since 2.0.2
*/ */
public class JmodOperation extends AbstractToolProviderOperation<JmodOperation> { public class JmodOperation extends AbstractToolProviderOperation<JmodOperation> {
private final List<String> fileOptions_ = new ArrayList<>();
private final JmodOptions jmodOptions_ = new JmodOptions(); private final JmodOptions jmodOptions_ = new JmodOptions();
private final List<String> options_ = new ArrayList<>();
private String jmodFile_; private String jmodFile_;
private OperationMode operationMode_; private OperationMode operationMode_;
@ -29,19 +27,40 @@ public class JmodOperation extends AbstractToolProviderOperation<JmodOperation>
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
if (operationMode_ == null) { if (operationMode_ != null) {
System.err.println("Operation mode not set."); toolArgs(operationMode_.mode);
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else if (jmodFile_ == null) {
System.err.println("Jmod file not set.");
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} }
addArgs(operationMode_.mode);
addArgs(jmodOptions_); toolArgsFromFile(fileOptions_);
addArgs(jmodFile_); toolArgs(jmodOptions_);
if (jmodFile_ != null) {
toolArgs(jmodFile_);
}
super.execute(); super.execute();
} }
/**
* Retrieves the list of files containing options or mode.
*
* @return the list of files
*/
public List<String> 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. * Retrieves the name of the JMOD file to create or from which to retrieve information.
* *
@ -101,26 +120,6 @@ public class JmodOperation extends AbstractToolProviderOperation<JmodOperation>
return this; return this;
} }
/**
* Retrieves the list of files containing options or mode.
*
* @return the list of files
*/
public List<String> 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. * The operation modes.
*/ */

View file

@ -16,8 +16,8 @@ import java.util.Map;
* @since 2.0.2 * @since 2.0.2
*/ */
public class JpackageOperation extends AbstractToolProviderOperation<JpackageOperation> { public class JpackageOperation extends AbstractToolProviderOperation<JpackageOperation> {
private final List<String> fileOptions_ = new ArrayList<>();
private final JpackageOptions jpackageOptions_ = new JpackageOptions(); private final JpackageOptions jpackageOptions_ = new JpackageOptions();
private final List<String> options_ = new ArrayList<>();
public JpackageOperation() { public JpackageOperation() {
super("jpackage"); super("jpackage");
@ -25,8 +25,8 @@ public class JpackageOperation extends AbstractToolProviderOperation<JpackageOpe
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
addArgs(jpackageOptions_); toolArgs(fileOptions_.stream().map(opt -> '@' + opt).toList());
addArgs(options_.stream().map(opt -> '@' + opt).toList()); toolArgs(jpackageOptions_);
super.execute(); super.execute();
} }
@ -35,17 +35,18 @@ public class JpackageOperation extends AbstractToolProviderOperation<JpackageOpe
* *
* @return the list of files * @return the list of files
*/ */
public List<String> options(){ public List<String> fileOptions() {
return options_; return fileOptions_;
} }
/** /**
* Read options and/or mode from a file. * Read options and/or mode from a file.
* *
* @param filename one or more file * @param file one or more file
* @return this operation instance * @return this operation instance
*/ */
public JpackageOperation options(String... filename) { public JpackageOperation fileOptions(String... file) {
options_.addAll(List.of(filename)); fileOptions_.addAll(List.of(file));
return this; return this;
} }