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

Polish reworked LoggedExec task #88424

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
import org.elasticsearch.gradle.LoggedExec
tasks.register('loggedExec', LoggedExec) {
commandLine 'ls', '-lh'
spoolOutput = $spooling
getSpoolOutput().set($spooling)
}
"""
when:
Expand All @@ -54,7 +54,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
import org.elasticsearch.gradle.LoggedExec
tasks.register('loggedExec', LoggedExec) {
commandLine 'ls', 'wtf'
spoolOutput = $spooling
getSpoolOutput().set($spooling)
}
"""
when:
Expand Down Expand Up @@ -97,7 +97,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
tasks.register('loggedExec', LoggedExec) {
commandLine 'echo', 'HELLO'
getCaptureOutput().set(true)
spoolOutput = true
getSpoolOutput().set(true)
}
"""
when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public abstract class LoggedExec extends DefaultTask implements FileSystemOperat
protected FileSystemOperations fileSystemOperations;
private ProjectLayout projectLayout;
private ExecOperations execOperations;
private boolean spoolOutput;

@Input
@Optional
Expand Down Expand Up @@ -84,6 +83,9 @@ public abstract class LoggedExec extends DefaultTask implements FileSystemOperat
@Input
abstract public Property<File> getWorkingDir();

@Internal
abstract public Property<Boolean> getSpoolOutput();

private String output;

@Inject
Expand All @@ -95,14 +97,16 @@ public LoggedExec(ProjectLayout projectLayout, ExecOperations execOperations, Fi
// For now mimic default behaviour of Gradle Exec task here
getEnvironment().putAll(System.getenv());
getCaptureOutput().convention(false);
getSpoolOutput().convention(false);
}

@TaskAction
public void run() {
boolean spoolOutput = getSpoolOutput().get();
if (spoolOutput && getCaptureOutput().get()) {
throw new GradleException("Capturing output is not supported when spoolOutput is true.");
}
if (getCaptureOutput().getOrElse(false) && getIndentingConsoleOutput().isPresent()) {
if (getCaptureOutput().get() && getIndentingConsoleOutput().isPresent()) {
throw new GradleException("Capturing output is not supported when indentingConsoleOutput is configured.");
}
Consumer<Logger> outputLogger;
Expand Down Expand Up @@ -156,7 +160,9 @@ public void run() {
if (getLogger().isInfoEnabled() == false) {
if (exitValue != 0) {
try {
getLogger().error("Output for " + getExecutable().get() + ":");
if (getIndentingConsoleOutput().isPresent() == false) {
getLogger().error("Output for " + getExecutable().get() + ":");
}
outputLogger.accept(getLogger());
} catch (Exception e) {
throw new GradleException("Failed to read exec output", e);
Expand All @@ -173,10 +179,6 @@ private String byteStreamToString(OutputStream out) {
return ((ByteArrayOutputStream) out).toString(StandardCharsets.UTF_8);
}

public void setSpoolOutput(boolean spoolOutput) {
this.spoolOutput = spoolOutput;
}

public static ExecResult exec(ExecOperations execOperations, Action<ExecSpec> action) {
return genericExec(execOperations::exec, action);
}
Expand Down