Skip to content

Commit 1ab01b0

Browse files
authored
Merge pull request #50598 from dmlloyd/rere-cli
2 parents e5c34c5 + 2f3df08 commit 1ab01b0

File tree

3 files changed

+44
-61
lines changed

3 files changed

+44
-61
lines changed

devtools/cli-common/src/main/java/io/quarkus/cli/common/build/ExecuteUtil.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import static picocli.CommandLine.ExitCode.OK;
44
import static picocli.CommandLine.ExitCode.SOFTWARE;
55

6-
import java.io.BufferedReader;
76
import java.io.File;
87
import java.io.IOException;
9-
import java.io.InputStreamReader;
108
import java.nio.file.Path;
11-
import java.util.concurrent.ExecutorService;
12-
import java.util.concurrent.Executors;
13-
import java.util.concurrent.TimeUnit;
9+
import java.util.List;
1410

1511
import io.quarkus.cli.common.OutputOptionMixin;
1612
import io.quarkus.devtools.exec.ExecSupport;
@@ -41,39 +37,31 @@ public static int executeProcess(OutputOptionMixin output, String[] args, File p
4137
output.out().println();
4238
}
4339

44-
int exit = SOFTWARE;
40+
var holder = new Object() {
41+
int exitCode;
42+
};
43+
io.smallrye.common.process.ProcessBuilder.Input<Void> pb = io.smallrye.common.process.ProcessBuilder.newBuilder(args[0])
44+
.arguments(List.of(args).subList(1, args.length))
45+
.directory(parentDir.toPath())
46+
.exitCodeChecker(ec -> {
47+
holder.exitCode = ec;
48+
return true;
49+
})
50+
.softExitTimeout(null)
51+
.hardExitTimeout(null)
52+
.input().inherited();
4553
if (output.isCliTest()) {
4654
// We have to capture IO differently in tests..
47-
Process process = new ProcessBuilder()
48-
.command(args)
49-
.redirectInput(ProcessBuilder.Redirect.INHERIT)
50-
.directory(parentDir)
51-
.start();
52-
53-
// Drain the output/errors streams
54-
ExecutorService service = Executors.newFixedThreadPool(2);
55-
service.submit(() -> {
56-
new BufferedReader(new InputStreamReader(process.getInputStream())).lines()
57-
.forEach(output.out()::println);
58-
});
59-
service.submit(() -> {
60-
new BufferedReader(new InputStreamReader(process.getErrorStream())).lines()
61-
.forEach(output.err()::println);
62-
});
63-
process.waitFor(5, TimeUnit.MINUTES);
64-
service.shutdown();
65-
66-
exit = process.exitValue();
55+
pb.output().consumeWith(br -> br.lines().forEach(output.out()::println))
56+
.error().consumeWith(br -> br.lines().forEach(output.err()::println))
57+
.run();
6758
} else {
68-
Process process = new ProcessBuilder()
69-
.command(args)
70-
.inheritIO()
71-
.directory(parentDir)
72-
.start();
73-
exit = process.waitFor();
59+
pb.output().inherited()
60+
.error().inherited()
61+
.run();
7462
}
7563

76-
if (exit != 0) {
64+
if (holder.exitCode != 0) {
7765
return SOFTWARE;
7866
} else {
7967
return OK;

devtools/cli/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
<groupId>org.jboss.logmanager</groupId>
6767
<artifactId>log4j-jboss-logmanager</artifactId>
6868
</dependency>
69+
<dependency>
70+
<groupId>io.smallrye.common</groupId>
71+
<artifactId>smallrye-common-process</artifactId>
72+
</dependency>
6973
<dependency>
7074
<groupId>io.quarkus</groupId>
7175
<artifactId>quarkus-devtools-testing</artifactId>

devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.io.PrintStream;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
11+
import java.time.Duration;
1112
import java.util.ArrayList;
13+
import java.util.Arrays;
1214
import java.util.Collection;
1315
import java.util.Collections;
1416
import java.util.Comparator;
@@ -33,7 +35,7 @@ public class CliDriver {
3335
public static class CliDriverBuilder {
3436

3537
private Path startingDir;
36-
private List<String> args = new ArrayList<>();
38+
private final List<String> args = new ArrayList<>();
3739
private String mavenLocalRepo;
3840
private String mavenSettings;
3941

@@ -46,9 +48,7 @@ public CliDriverBuilder setStartingDir(Path startingDir) {
4648
}
4749

4850
public CliDriverBuilder addArgs(String... args) {
49-
for (String s : args) {
50-
this.args.add(s);
51-
}
51+
Collections.addAll(this.args, args);
5252
return this;
5353
}
5454

@@ -151,34 +151,25 @@ public static void preserveLocalRepoSettings(Collection<String> args) {
151151
getMavenSettingsProperty().map(SETTINGS_ARG_FORMATTER).ifPresent(args::add);
152152
}
153153

154-
public static Result executeArbitraryCommand(Path startingDir, String... args) throws Exception {
154+
public static Result executeArbitraryCommand(Path startingDir, String... args) {
155155
System.out.println("$ " + String.join(" ", args));
156156

157-
ByteArrayOutputStream out = new ByteArrayOutputStream();
158-
PrintStream outPs = new PrintStream(out);
159-
System.setOut(outPs);
157+
Result result = new Result();
160158

161-
ByteArrayOutputStream err = new ByteArrayOutputStream();
162-
PrintStream errPs = new PrintStream(err);
163-
System.setErr(errPs);
159+
io.smallrye.common.process.ProcessBuilder.newBuilder(Path.of(args[0]))
160+
.arguments(Arrays.copyOfRange(args, 1, args.length))
161+
.exitCodeChecker(ec -> {
162+
result.exitCode = ec;
163+
return true;
164+
})
165+
.directory(startingDir)
166+
// since there is no I/O, we need an explicit timeout
167+
.softExitTimeout(Duration.ofMinutes(5))
168+
.hardExitTimeout(Duration.ofMinutes(5))
169+
.output().inherited()
170+
.error().logOnSuccess(false).gatherOnFail(false).inherited()
171+
.run();
164172

165-
Result result = new Result();
166-
try {
167-
ProcessBuilder pb = new ProcessBuilder(args);
168-
pb.directory(startingDir.toFile());
169-
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
170-
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
171-
172-
Process p = pb.start();
173-
p.waitFor();
174-
outPs.flush();
175-
errPs.flush();
176-
} finally {
177-
System.setOut(stdout);
178-
System.setErr(stderr);
179-
}
180-
result.stdout = out.toString();
181-
result.stderr = err.toString();
182173
return result;
183174
}
184175

0 commit comments

Comments
 (0)