Skip to content

Commit

Permalink
Fix cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 7, 2025
1 parent 4f9a312 commit 344e1cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -136,10 +138,6 @@ public final int invoke(InvokerRequest invokerRequest) {
} catch (Exception e) {
// other exceptions (including InvokerException but sans Exit, see above): we need to inform user
throw handleException(context, e);
} finally {
if (context.terminal != null) {
context.terminal.writer().flush();
}
}
} finally {
Thread.currentThread().setContextClassLoader(oldCL);
Expand Down Expand Up @@ -308,14 +306,16 @@ protected void createTerminal(C context) {
if (context.terminal == null) {
MessageUtils.systemInstall(
builder -> {
builder.streams(
context.invokerRequest.stdIn().orElse(null),
context.invokerRequest.stdOut().orElse(null));
builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
// The exec builder suffers from https://github.com/jline/jline3/issues/1098
// We could re-enable it when fixed to provide support for non-standard architectures,
// for which JLine does not provide any native library.
builder.exec(false);
if (context.invokerRequest.embedded()) {
InputStream in = context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
OutputStream out = context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
builder.streams(in, out);
builder.provider("exec");
context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
context.closeables.add(out::flush);
} else {
builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
}
if (context.coloredOutput != null) {
builder.color(context.coloredOutput);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package org.apache.maven.cling.invoker.mvn;

import java.io.ByteArrayOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -99,18 +101,34 @@ protected Map<String, String> invoke(Path cwd, Path userHome, Collection<String>
Parser parser = createParser();
try (Invoker invoker = createInvoker()) {
for (String goal : goals) {
Path logFile =
cwd.resolve(goal.replace(':', '-') + "-build.log").toAbsolutePath();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
List<String> mvnArgs = new ArrayList<>(args);
mvnArgs.addAll(List.of("-l", logFile.toString(), goal));
mvnArgs.add(goal);
int exitCode = invoker.invoke(
parser.parseInvocation(ParserRequest.mvn(mvnArgs, new JLineMessageBuilderFactory())
.cwd(cwd)
.userHome(userHome)
.stdOut(stdout)
.stdErr(stderr)
.embedded(true)
.build()));
String log = Files.readString(logFile);
logs.put(goal, log);
assertEquals(0, exitCode, log);

// dump things out
System.out.println("===================================================");
System.out.println("args: " + Arrays.toString(mvnArgs.toArray()));
System.out.println("===================================================");
System.out.println("stdout: " + stdout);
System.out.println("===================================================");

System.err.println("===================================================");
System.err.println("args: " + Arrays.toString(mvnArgs.toArray()));
System.err.println("===================================================");
System.err.println("stderr: " + stderr);
System.err.println("===================================================");

logs.put(goal, stdout.toString());
assertEquals(0, exitCode, "OUT:" + stdout + "\nERR:" + stderr);
}
}
return logs;
Expand Down

0 comments on commit 344e1cf

Please sign in to comment.