mirror of
https://github.com/ethauvin/bld.git
synced 2025-04-25 00:07:12 -07:00
Fixed #58 : Add an option to set environment variables before spawining a process
This commit is contained in:
parent
f9f20e62ef
commit
ca7fa0c4b8
2 changed files with 40 additions and 1 deletions
|
@ -11,7 +11,9 @@ import rife.tools.exceptions.FileUtilsErrorException;
|
|||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
|
@ -24,6 +26,7 @@ public abstract class AbstractProcessOperation<T extends AbstractProcessOperatio
|
|||
public static final String DEFAULT_JAVA_TOOL = "java";
|
||||
|
||||
protected File workDirectory_ = new File(System.getProperty("user.dir"));
|
||||
protected final Map<String, String> environment_ = new HashMap<>();
|
||||
protected String javaTool_ = DEFAULT_JAVA_TOOL;
|
||||
protected final JavaOptions javaOptions_ = new JavaOptions();
|
||||
protected final List<String> classpath_ = new ArrayList<>();
|
||||
|
@ -87,6 +90,10 @@ public abstract class AbstractProcessOperation<T extends AbstractProcessOperatio
|
|||
var builder = new ProcessBuilder(executeConstructProcessCommandList());
|
||||
builder.directory(workDirectory());
|
||||
|
||||
if (!environment_.isEmpty()) {
|
||||
builder.environment().putAll(environment_);
|
||||
}
|
||||
|
||||
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
|
||||
|
||||
final var output_processor = outputProcessor();
|
||||
|
@ -163,6 +170,18 @@ public abstract class AbstractProcessOperation<T extends AbstractProcessOperatio
|
|||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides environment variable entries to use for the operation.
|
||||
*
|
||||
* @param environment environment entries for the operation
|
||||
* @return this operation instance
|
||||
* @since 2.2.1
|
||||
*/
|
||||
public T environment(Map<String, String> environment) {
|
||||
environment_.putAll(environment);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the name of the tool to use for {@code java} execution.
|
||||
* <p>
|
||||
|
@ -303,6 +322,18 @@ public abstract class AbstractProcessOperation<T extends AbstractProcessOperatio
|
|||
return workDirectory_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the environment to use for the operation.
|
||||
* <p>
|
||||
* This is a modifiable map that can be retrieved and changed.
|
||||
*
|
||||
* @return the operation's environment
|
||||
* @since 2.2.1
|
||||
*/
|
||||
public Map<String, String> environment() {
|
||||
return environment_;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieves the name of the tool to use for {@code java} execution.
|
||||
*
|
||||
|
|
|
@ -11,6 +11,7 @@ import rife.tools.FileUtils;
|
|||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.jar.Attributes;
|
||||
|
||||
|
@ -24,6 +25,7 @@ public class TestRunOperation {
|
|||
assertTrue(operation.workDirectory().exists());
|
||||
assertTrue(operation.workDirectory().isDirectory());
|
||||
assertTrue(operation.workDirectory().canWrite());
|
||||
assertTrue(operation.environment().isEmpty());
|
||||
assertEquals("java", operation.javaTool());
|
||||
assertTrue(operation.javaOptions().isEmpty());
|
||||
assertTrue(operation.classpath().isEmpty());
|
||||
|
@ -38,6 +40,7 @@ public class TestRunOperation {
|
|||
@Test
|
||||
void testPopulation()
|
||||
throws Exception {
|
||||
var environment = Map.of("env1", "val1", "env2", "val2", "env3", "val3");
|
||||
var work_directory = Files.createTempDirectory("test").toFile();
|
||||
try {
|
||||
var java_tool = "javatool";
|
||||
|
@ -55,6 +58,7 @@ public class TestRunOperation {
|
|||
var operation1 = new RunOperation();
|
||||
operation1
|
||||
.workDirectory(work_directory)
|
||||
.environment(environment)
|
||||
.javaTool(java_tool)
|
||||
.javaOptions(List.of(run_java_option1, run_java_option2))
|
||||
.classpath(List.of(run_classpath1, run_classpath2))
|
||||
|
@ -65,6 +69,7 @@ public class TestRunOperation {
|
|||
.errorProcessor(run_error_consumer);
|
||||
|
||||
assertEquals(work_directory, operation1.workDirectory());
|
||||
assertEquals(environment, operation1.environment());
|
||||
assertEquals(java_tool, operation1.javaTool());
|
||||
assertTrue(operation1.javaOptions().contains(run_java_option1));
|
||||
assertTrue(operation1.javaOptions().contains(run_java_option2));
|
||||
|
@ -79,6 +84,7 @@ public class TestRunOperation {
|
|||
|
||||
var operation2 = new RunOperation();
|
||||
operation2.workDirectory(work_directory);
|
||||
operation2.environment(environment);
|
||||
operation2.javaTool(java_tool);
|
||||
operation2.javaOptions().add(run_java_option1);
|
||||
operation2.javaOptions().add(run_java_option2);
|
||||
|
@ -92,6 +98,7 @@ public class TestRunOperation {
|
|||
operation2.errorProcessor(run_error_consumer);
|
||||
|
||||
assertEquals(work_directory, operation2.workDirectory());
|
||||
assertEquals(environment, operation2.environment());
|
||||
assertEquals(java_tool, operation2.javaTool());
|
||||
assertTrue(operation2.javaOptions().contains(run_java_option1));
|
||||
assertTrue(operation2.javaOptions().contains(run_java_option2));
|
||||
|
@ -129,7 +136,7 @@ public class TestRunOperation {
|
|||
public class Source1 {
|
||||
public final String name_;
|
||||
public Source1() {
|
||||
name_ = "source1";
|
||||
name_ = System.getenv("execute_name");
|
||||
}
|
||||
|
||||
public static void main(String[] arguments)
|
||||
|
@ -149,6 +156,7 @@ public class TestRunOperation {
|
|||
|
||||
var output = new StringBuilder();
|
||||
var run_operation = new RunOperation()
|
||||
.environment(Map.of("execute_name", "source1"))
|
||||
.mainClass("Source1")
|
||||
.classpath(List.of(build_main.getAbsolutePath()))
|
||||
.outputProcessor(s -> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue