Make execRemote work for both action and closure

This commit is contained in:
Pavel Punegov
2017-11-07 16:41:30 +03:00
committed by Pavel Punegov
parent a3ad2101f5
commit 629cbd8eed
@@ -33,7 +33,7 @@ import java.util.function.Function
class ExecRemote { class ExecRemote {
private final Project project private final Project project
private final Function<Closure<? super ExecSpec>, ExecResult> executor private final Function<Action<? super ExecSpec>, ExecResult> executor
ExecRemote(Project project) { ExecRemote(Project project) {
this.project = project this.project = project
@@ -41,37 +41,41 @@ class ExecRemote {
def remote = project.property('remote') as String def remote = project.property('remote') as String
executor = new SSHExecutor(remote) executor = new SSHExecutor(remote)
} else { } else {
executor = { project.exec(ConfigureUtil.configureUsing(it)) } executor = { project.exec(it) }
} }
} }
ExecResult execRemote(Action<? super ExecSpec> action) {
project.exec(action)
}
ExecResult execRemote(Closure<? super ExecSpec> closure) { ExecResult execRemote(Closure<? super ExecSpec> closure) {
this.executor.apply(closure) this.execRemote(ConfigureUtil.configureUsing(closure))
} }
private class SSHExecutor implements Function<Closure<? super ExecSpec>, ExecResult> { ExecResult execRemote(Action<? super ExecSpec> action) {
this.executor.apply(action)
}
private class SSHExecutor implements Function<Action<? super ExecSpec>, ExecResult> {
private final String remote private final String remote
private final String remoteDir private final String remoteDir
SSHExecutor(String remote) { SSHExecutor(String remote) {
this.remote = remote this.remote = remote
this.remoteDir = uniqueSessionName() this.remoteDir = uniqueSessionName()
createRemoteDir()
} }
@Override @Override
ExecResult apply(Closure<? super ExecSpec> closure) { ExecResult apply(Action<? super ExecSpec> action) {
String execFile String execFile
def execResult = project.exec closure >> {
upload(executable) createRemoteDir()
execFile = executable = "$remoteDir/${new File(executable).name}" def execResult = project.exec { ExecSpec execSpec ->
commandLine = ['/usr/bin/ssh', remote] + commandLine action.execute(execSpec)
execSpec.with {
upload(executable)
execFile = executable = "$remoteDir/${new File(executable).name}"
commandLine = ['/usr/bin/ssh', remote] + commandLine
}
} }
if (execFile != null) cleanup(execFile) cleanup(execFile)
return execResult return execResult
} }