Skip to content

Commit a2ee4c5

Browse files
authored
Polish reworked LoggedExec task (#88424)
Some polishing of reworked LoggedExec task
1 parent 4889650 commit a2ee4c5

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

build-tools/src/integTest/groovy/org/elasticsearch/gradle/LoggedExecFuncTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
3535
import org.elasticsearch.gradle.LoggedExec
3636
tasks.register('loggedExec', LoggedExec) {
3737
commandLine 'ls', '-lh'
38-
spoolOutput = $spooling
38+
getSpoolOutput().set($spooling)
3939
}
4040
"""
4141
when:
@@ -54,7 +54,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
5454
import org.elasticsearch.gradle.LoggedExec
5555
tasks.register('loggedExec', LoggedExec) {
5656
commandLine 'ls', 'wtf'
57-
spoolOutput = $spooling
57+
getSpoolOutput().set($spooling)
5858
}
5959
"""
6060
when:
@@ -97,7 +97,7 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
9797
tasks.register('loggedExec', LoggedExec) {
9898
commandLine 'echo', 'HELLO'
9999
getCaptureOutput().set(true)
100-
spoolOutput = true
100+
getSpoolOutput().set(true)
101101
}
102102
"""
103103
when:

build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public abstract class LoggedExec extends DefaultTask implements FileSystemOperat
5656
protected FileSystemOperations fileSystemOperations;
5757
private ProjectLayout projectLayout;
5858
private ExecOperations execOperations;
59-
private boolean spoolOutput;
6059

6160
@Input
6261
@Optional
@@ -84,6 +83,9 @@ public abstract class LoggedExec extends DefaultTask implements FileSystemOperat
8483
@Input
8584
abstract public Property<File> getWorkingDir();
8685

86+
@Internal
87+
abstract public Property<Boolean> getSpoolOutput();
88+
8789
private String output;
8890

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

100103
@TaskAction
101104
public void run() {
105+
boolean spoolOutput = getSpoolOutput().get();
102106
if (spoolOutput && getCaptureOutput().get()) {
103107
throw new GradleException("Capturing output is not supported when spoolOutput is true.");
104108
}
105-
if (getCaptureOutput().getOrElse(false) && getIndentingConsoleOutput().isPresent()) {
109+
if (getCaptureOutput().get() && getIndentingConsoleOutput().isPresent()) {
106110
throw new GradleException("Capturing output is not supported when indentingConsoleOutput is configured.");
107111
}
108112
Consumer<Logger> outputLogger;
@@ -156,7 +160,9 @@ public void run() {
156160
if (getLogger().isInfoEnabled() == false) {
157161
if (exitValue != 0) {
158162
try {
159-
getLogger().error("Output for " + getExecutable().get() + ":");
163+
if (getIndentingConsoleOutput().isPresent() == false) {
164+
getLogger().error("Output for " + getExecutable().get() + ":");
165+
}
160166
outputLogger.accept(getLogger());
161167
} catch (Exception e) {
162168
throw new GradleException("Failed to read exec output", e);
@@ -173,10 +179,6 @@ private String byteStreamToString(OutputStream out) {
173179
return ((ByteArrayOutputStream) out).toString(StandardCharsets.UTF_8);
174180
}
175181

176-
public void setSpoolOutput(boolean spoolOutput) {
177-
this.spoolOutput = spoolOutput;
178-
}
179-
180182
public static ExecResult exec(ExecOperations execOperations, Action<ExecSpec> action) {
181183
return genericExec(execOperations::exec, action);
182184
}

0 commit comments

Comments
 (0)