2727import io .serverlessworkflow .impl .expressions .ExpressionUtils ;
2828import java .io .IOException ;
2929import java .io .OutputStream ;
30+ import java .nio .file .Path ;
3031import java .util .Map ;
3132import java .util .concurrent .CompletableFuture ;
3233
@@ -37,7 +38,8 @@ public class RunShellExecutor implements RunnableTask<RunShell> {
3738
3839 @ FunctionalInterface
3940 private interface ProcessResultSupplier {
40- ProcessResult apply (TaskContext taskContext , ProcessBuilder processBuilder );
41+ WorkflowModel apply (
42+ TaskContext taskContext , ProcessBuilder processBuilder , WorkflowModel input );
4143 }
4244
4345 private interface CommandSupplier {
@@ -48,9 +50,9 @@ private interface CommandSupplier {
4850 public CompletableFuture <WorkflowModel > apply (
4951 WorkflowContext workflowContext , TaskContext taskContext , WorkflowModel input ) {
5052 ProcessBuilder processBuilder = this .commandSupplier .apply (taskContext , workflowContext );
51- ProcessResult processResult = this . processResultSupplier . apply ( taskContext , processBuilder );
52- return CompletableFuture . completedFuture (
53- workflowContext . definition (). application (). modelFactory (). fromAny ( processResult ) );
53+ WorkflowModel workflowModel =
54+ this . processResultSupplier . apply ( taskContext , processBuilder , input );
55+ return CompletableFuture . completedFuture ( workflowModel );
5456 }
5557
5658 @ Override
@@ -87,27 +89,37 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) {
8789 }
8890 }
8991
92+ builder .directory (Path .of (System .getProperty ("user.dir" )).toFile ());
93+
9094 return builder ;
9195 };
9296
9397 this .processResultSupplier =
94- (taskContext , processBuilder ) -> {
98+ (taskContext , processBuilder , input ) -> {
9599 if (shellCommand == null || shellCommand .isBlank ()) {
96100 throw new IllegalStateException (
97101 "Missing shell command in RunShell task: " + taskContext .taskName ());
98102 }
99103
100104 try {
101105 Process process = processBuilder .start ();
102- StringBuilder stdout = new StringBuilder ();
103- StringBuilder stderr = new StringBuilder ();
104-
105- process .getInputStream ().transferTo (the (stdout ));
106- process .getErrorStream ().transferTo (the (stderr ));
107106
108- int exitCode = process .waitFor ();
109-
110- return new ProcessResult (exitCode , stdout .toString ().trim (), stderr .toString ().trim ());
107+ if (taskConfiguration .isAwait ()) {
108+ StringBuilder stdout = new StringBuilder ();
109+ StringBuilder stderr = new StringBuilder ();
110+
111+ process .getInputStream ().transferTo (the (stdout ));
112+ process .getErrorStream ().transferTo (the (stderr ));
113+ int exitCode = process .waitFor ();
114+ return definition
115+ .application ()
116+ .modelFactory ()
117+ .fromAny (
118+ new ProcessResult (
119+ exitCode , stdout .toString ().trim (), stderr .toString ().trim ()));
120+ } else {
121+ return input ;
122+ }
111123
112124 } catch (IOException | InterruptedException e ) {
113125 throw new RuntimeException (e );
0 commit comments