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

ExecUtils refactoring #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
51 changes: 15 additions & 36 deletions src/main/java/org/terracotta/build/ExecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Task;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
Expand All @@ -22,59 +21,39 @@
public class ExecUtils {
private static final Logger LOGGER = Logging.getLogger(ExecUtils.class);

public static String execUnder(Task task, Action<ExecSpec> action) {
return execUnder(task, LogLevel.INFO, LogLevel.ERROR, action);
public static String execute(ExecOperations execOperations, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, LOGGER, action);
}

public static String execQuietlyUnder(Task task, Action<ExecSpec> action) {
return execUnder(task, LogLevel.DEBUG, LogLevel.DEBUG, action);
public static String execute(ExecOperations execOperations, Logger logger, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, logger, LogLevel.INFO, LogLevel.ERROR, action);
}

public static String execUnder(Task task, LogLevel output, LogLevel failure, Action<ExecSpec> action) {
Logger logger = task.getLogger();

ByteArrayOutputStream mergedBytes = new ByteArrayOutputStream();
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
try (OutputStream standardOut = tee(logTo(logger, output), outBytes, mergedBytes);
OutputStream errorOut = tee(logTo(logger, output), mergedBytes)) {
task.getProject().exec(spec -> {
spec.setStandardOutput(standardOut);
spec.setErrorOutput(errorOut);
action.execute(spec);
}).assertNormalExitValue();
} catch (ExecException e) {
if (!logger.isEnabled(output) && logger.isEnabled(failure)) {
logger.log(failure, StandardCharsets.UTF_8.decode(ByteBuffer.wrap(mergedBytes.toByteArray())).toString());
}
throw e;
} catch (IOException e) {
throw new GradleException("Unexpected exception closing process output streams", e);
}

return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(outBytes.toByteArray())).toString();
public static String executeQuietly(ExecOperations execOperations, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, LOGGER, action);
}

public static String execute(ExecOperations execOperations, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, LogLevel.INFO, LogLevel.ERROR, action);
public static String executeQuietly(ExecOperations execOperations, Logger logger, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, logger, LogLevel.DEBUG, LogLevel.DEBUG, action);
}

public static String executeQuietly(ExecOperations execOperations, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, LogLevel.DEBUG, LogLevel.DEBUG, action);
public static String execute(ExecOperations execOperations, LogLevel output, LogLevel failure, Action<ExecSpec> action) throws ExecException {
return execute(execOperations, LOGGER, output, failure, action);
}

public static String execute(ExecOperations execOperations, LogLevel output, LogLevel failure, Action<ExecSpec> action) throws ExecException {
public static String execute(ExecOperations execOperations, Logger logger, LogLevel output, LogLevel failure, Action<ExecSpec> action) throws ExecException {
ByteArrayOutputStream mergedBytes = new ByteArrayOutputStream();
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
try (OutputStream standardOut = tee(logTo(LOGGER, output), outBytes, mergedBytes);
OutputStream errorOut = tee(logTo(LOGGER, output), mergedBytes)) {
try (OutputStream standardOut = tee(logTo(logger, output), outBytes, mergedBytes);
OutputStream errorOut = tee(logTo(logger, output), mergedBytes)) {
execOperations.exec(spec -> {
spec.setStandardOutput(standardOut);
spec.setErrorOutput(errorOut);
action.execute(spec);
}).assertNormalExitValue();
} catch (ExecException e) {
if (!LOGGER.isEnabled(output) && LOGGER.isEnabled(failure)) {
LOGGER.log(failure, StandardCharsets.UTF_8.decode(ByteBuffer.wrap(mergedBytes.toByteArray())).toString());
if (!logger.isEnabled(output) && logger.isEnabled(failure)) {
logger.log(failure, StandardCharsets.UTF_8.decode(ByteBuffer.wrap(mergedBytes.toByteArray())).toString());
}
throw e;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import org.gradle.api.provider.Property;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Input;
import org.gradle.process.ExecOperations;
import org.gradle.process.ExecSpec;
import org.gradle.process.internal.ExecException;

import javax.inject.Inject;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
Expand All @@ -24,8 +26,8 @@
import static java.lang.Thread.sleep;
import static java.util.Collections.emptyMap;
import static org.gradle.internal.Actions.composite;
import static org.terracotta.build.ExecUtils.execQuietlyUnder;
import static org.terracotta.build.ExecUtils.execUnder;
import static org.terracotta.build.ExecUtils.execute;
import static org.terracotta.build.ExecUtils.executeQuietly;
import static org.terracotta.build.PluginUtils.capitalize;

public abstract class DockerTask extends DefaultTask {
Expand All @@ -41,15 +43,18 @@ public DockerTask() {
@Input
public abstract MapProperty<String, String> getDockerEnv();

@Inject
public abstract ExecOperations getExecOperations();

public String docker(Action<ExecSpec> action) {
return execUnder(this, composite(exec -> {
return execute(getExecOperations(), this.getLogger(), composite(exec -> {
exec.environment(getDockerEnv().getOrElse(emptyMap()));
exec.executable(getDocker().get());
}, action));
}

public String dockerQuietly(Action<ExecSpec> action) {
return execQuietlyUnder(this, composite(exec -> {
return executeQuietly(getExecOperations(), this.getLogger(), composite(exec -> {
exec.environment(getDockerEnv().getOrElse(emptyMap()));
exec.executable(getDocker().get());
}, action));
Expand Down