From 215b78b75ec2ad25756aa47dadbd29a74e754bfa Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 15 Dec 2023 02:57:54 +0000 Subject: [PATCH] Fix environment variable handling in command builder --- .../io/xpipe/core/process/CommandBuilder.java | 25 +++++++++++++++---- .../io/xpipe/core/process/ShellDialect.java | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/io/xpipe/core/process/CommandBuilder.java b/core/src/main/java/io/xpipe/core/process/CommandBuilder.java index 2b72f2a2d..45575b8db 100644 --- a/core/src/main/java/io/xpipe/core/process/CommandBuilder.java +++ b/core/src/main/java/io/xpipe/core/process/CommandBuilder.java @@ -1,11 +1,9 @@ package io.xpipe.core.process; +import lombok.Getter; import lombok.SneakyThrows; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.SequencedCollection; +import java.util.*; public class CommandBuilder { @@ -16,6 +14,18 @@ public class CommandBuilder { private CommandBuilder() {} private final List elements = new ArrayList<>(); + @Getter + private final Map environmentVariables = new LinkedHashMap<>(); + + public CommandBuilder envrironment(String k, String v) { + environmentVariables.put(k, v); + return this; + } + + public CommandBuilder envrironment(Map map) { + environmentVariables.putAll(map); + return this; + } public interface Element { @@ -155,7 +165,7 @@ public class CommandBuilder { return this; } - public String build(ShellControl sc) throws Exception { + public String buildBase(ShellControl sc) throws Exception { List list = new ArrayList<>(); for (Element element : elements) { String evaluate = element.evaluate(sc); @@ -168,6 +178,11 @@ public class CommandBuilder { return String.join(" ", list); } + public String build(ShellControl sc) throws Exception { + var s = buildBase(sc); + return sc.getShellDialect().addInlineVariablesToCommand(environmentVariables, s); + } + public CommandControl buildCommand(ShellControl sc) { return sc.command(this); } diff --git a/core/src/main/java/io/xpipe/core/process/ShellDialect.java b/core/src/main/java/io/xpipe/core/process/ShellDialect.java index b8778a851..46a16bb7d 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellDialect.java +++ b/core/src/main/java/io/xpipe/core/process/ShellDialect.java @@ -86,7 +86,7 @@ public interface ShellDialect { String getScriptFileEnding(); - void addInlineVariablesToCommand(Map variables, CommandBuilder command); + String addInlineVariablesToCommand(Map variables, String command); Stream listFiles(FileSystem fs, ShellControl control, String dir) throws Exception;