Skip to content

Commit

Permalink
Polish reworked LoggedExec task
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby committed Jul 11, 2022
1 parent 4cc8484 commit 905f67f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
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

0 comments on commit 905f67f

Please sign in to comment.