Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide bwc build output on success #42102

Merged
merged 10 commits into from May 16, 2019
Merged
Expand Up @@ -3,14 +3,21 @@
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Exec;
import org.gradle.process.BaseExecSpec;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.gradle.process.JavaExecSpec;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.function.Function;

/**
Expand All @@ -19,21 +26,52 @@
@SuppressWarnings("unchecked")
public class LoggedExec extends Exec {

// helper to allow lambda creation using Files methods
private interface ThrowingStringSupplier {
String get() throws Exception;
}

private Property<Boolean> useFileBuffer;

public LoggedExec() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
this.useFileBuffer = getProject().getObjects().property(Boolean.class);
this.useFileBuffer.set(false); // default to in memory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the "convention" is to use convention() for setting default values on properties.

if (getLogger().isInfoEnabled() == false) {
setStandardOutput(output);
setErrorOutput(error);
setIgnoreExitValue(true);
doLast((unused) -> {
getProject().afterEvaluate(p -> {
mark-vieira marked this conversation as resolved.
Show resolved Hide resolved
OutputStream stdout;
OutputStream stderr;
ThrowingStringSupplier readStdout;
ThrowingStringSupplier readStderr;

try {
if (useFileBuffer.get()) {
String outputdir = p.getBuildDir() + "/buffered-output/" + this.getName();
p.mkdir(outputdir);
mark-vieira marked this conversation as resolved.
Show resolved Hide resolved
stdout = new FileOutputStream(outputdir + "/stdout");
stderr = new FileOutputStream(outputdir + "/stderr");
readStdout = () -> Files.readString(Paths.get(outputdir + "/stdout"));
readStderr = () -> Files.readString(Paths.get(outputdir + "/stderr"));
} else {
stdout = new ByteArrayOutputStream();
stderr = new ByteArrayOutputStream();
readStdout = () -> ((ByteArrayOutputStream) stdout).toString(StandardCharsets.UTF_8);
readStderr = () -> ((ByteArrayOutputStream) stderr).toString(StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new RuntimeException(e);
}

setStandardOutput(stdout);
mark-vieira marked this conversation as resolved.
Show resolved Hide resolved
setErrorOutput(stderr);
setIgnoreExitValue(true);
doLast(task -> {
if (getExecResult().getExitValue() != 0) {
try {
getLogger().error("Standard output:");
getLogger().error(output.toString("UTF-8"));
getLogger().error(readStdout.get());
getLogger().error("Standard error:");
getLogger().error(error.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
getLogger().error(readStderr.get());
} catch (Exception e) {
throw new GradleException("Failed to read exec output", e);
}
throw new GradleException(
Expand All @@ -45,11 +83,15 @@ public LoggedExec() {
)
);
}
}
);
});
});
}
}

public void setUseFileBuffer(boolean useFileBuffer) {
mark-vieira marked this conversation as resolved.
Show resolved Hide resolved
this.useFileBuffer.set(useFileBuffer);
}

public static ExecResult exec(Project project, Action<ExecSpec> action) {
return genericExec(project, project::exec, action);
}
Expand Down
3 changes: 2 additions & 1 deletion distribution/bwc/build.gradle
Expand Up @@ -121,8 +121,9 @@ bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInfo unreleased
}

Closure createRunBwcGradleTask = { name, extraConfig ->
return tasks.create(name: "$name", type: Exec) {
return tasks.create(name: "$name", type: LoggedExec) {
dependsOn checkoutBwcBranch, writeBuildMetadata
useFileBuffer = true
workingDir = checkoutDir
doFirst {
// Execution time so that the checkouts are available
Expand Down