2
0
Fork 0
mirror of https://github.com/ethauvin/bld.git synced 2025-04-25 00:07:12 -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 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<T extends AbstractToolProvid
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.
* <p>
@ -85,6 +47,7 @@ public abstract class AbstractToolProviderOperation<T extends AbstractToolProvid
System.err.println("No " + toolName_ + " command line arguments specified.");
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
}
var tool = ToolProvider.findFirst(toolName_).orElseThrow(() ->
new IllegalStateException("No " + toolName_ + " tool found."));
@ -92,17 +55,92 @@ public abstract class AbstractToolProviderOperation<T extends AbstractToolProvid
if (status != 0) {
System.out.println(tool.name() + ' ' + String.join(" ", toolArgs_));
}
ExitStatusException.throwOnFailure(status);
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
*/
public List<String> 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;
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<JlinkOperation> {
private final List<String> disabledPlugins_ = new ArrayList<>();
private final JlinkOptions jlinkOptions_ = new JlinkOptions();
private final List<String> options_ = new ArrayList<>();
private final List<String> fileOptions_ = new ArrayList<>();
public JlinkOperation() {
super("jlink");
@ -40,9 +37,9 @@ public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation
@Override
public void execute() throws Exception {
disabledPlugins_.forEach(plugin -> 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<JlinkOperation
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.
*
* @param filename one or more file
* @param file one or more file
* @return this operation instance
*/
public JlinkOperation options(String... filename) {
options_.addAll(List.of(filename));
public JlinkOperation fileOptions(String... file) {
fileOptions_.addAll(List.of(file));
return this;
}
@ -96,31 +83,17 @@ public class JlinkOperation extends AbstractToolProviderOperation<JlinkOperation
*
* @return the list of files
*/
public List<String> options() {
return options_;
public List<String> fileOptions() {
return fileOptions_;
}
// Shouldn't be needed, but jlink doesn't support @filename when called via ToolProvider
private List<String> parseOptions() throws FileNotFoundException {
var list = new ArrayList<String>();
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;
}
}

View file

@ -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<JmodOperation> {
private final List<String> fileOptions_ = new ArrayList<>();
private final JmodOptions jmodOptions_ = new JmodOptions();
private final List<String> options_ = new ArrayList<>();
private String jmodFile_;
private OperationMode operationMode_;
@ -29,19 +27,40 @@ public class JmodOperation extends AbstractToolProviderOperation<JmodOperation>
@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<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.
*
@ -101,26 +120,6 @@ public class JmodOperation extends AbstractToolProviderOperation<JmodOperation>
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.
*/

View file

@ -16,8 +16,8 @@ import java.util.Map;
* @since 2.0.2
*/
public class JpackageOperation extends AbstractToolProviderOperation<JpackageOperation> {
private final List<String> fileOptions_ = new ArrayList<>();
private final JpackageOptions jpackageOptions_ = new JpackageOptions();
private final List<String> options_ = new ArrayList<>();
public JpackageOperation() {
super("jpackage");
@ -25,8 +25,8 @@ public class JpackageOperation extends AbstractToolProviderOperation<JpackageOpe
@Override
public void execute() throws Exception {
addArgs(jpackageOptions_);
addArgs(options_.stream().map(opt -> '@' + opt).toList());
toolArgs(fileOptions_.stream().map(opt -> '@' + opt).toList());
toolArgs(jpackageOptions_);
super.execute();
}
@ -35,17 +35,18 @@ public class JpackageOperation extends AbstractToolProviderOperation<JpackageOpe
*
* @return the list of files
*/
public List<String> options(){
return options_;
public List<String> 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;
}