scoop:scoop.platform:0.1.0
src/process/command_builder.scoop
Selected production source · src/process/command_builder.scoop
1
package scoop.platform.process3
import scoop.collections.Map4
import scoop.collections.toMap5
import scoop.platform.env.currentDirectory6
import scoop.platform.env.environment as currentEnvironment7
import scoop.platform.fs.*8
import scoop.platform.io.*9
import scoop.platform.system.*10
import scoop.collections.HashMap12
/** Configures one complete value; execution remains explicit. */13
public fun command(executable: Path, configure: CommandBuilder.() -> Unit = {}): Command {14
val builder = CommandBuilder()15
builder.configure()16
return builder.snapshot(executable)17
}19
public fun Command.copy(configure: CommandBuilder.() -> Unit = {}): Command {20
val builder = CommandBuilder(this.environment, this.workingDirectory)21
builder.arguments = this.arguments22
builder.environment = this.environment23
builder.workingDirectory = this.workingDirectory24
builder.stdin = this.stdin25
builder.stdout = this.stdout26
builder.stderr = this.stderr27
builder.mergeError = this.mergeError28
builder.configure()29
return builder.snapshot(this.executable)30
}32
public class CommandBuilder internal constructor(33
initialEnvironment: Map<OsString, OsString> = currentEnvironment(),34
initialDirectory: Path = currentDirectory()35
) {36
public var arguments: Array<OsString> = []37
public var environment: Map<OsString, OsString> = initialEnvironment38
public var workingDirectory: Path = initialDirectory39
public var stdin: Stdio = Stdio.Inherit40
public var stdout: Stdio = Stdio.Inherit41
public var stderr: Stdio = Stdio.Inherit42
public var mergeError: Boolean = false44
public fun arg(value: OsString): Unit {45
val next = ArrayList<OsString>()46
for (item in arguments)47
next.add(item)48
next.add(value)49
arguments = next.toArray()50
}51
public fun args(values: Array<OsString>): Unit {52
val next = ArrayList<OsString>()53
for (item in arguments)54
next.add(item)55
for (item in values)56
next.add(item)57
arguments = next.toArray()58
}59
public fun env(name: OsString, value: OsString): Unit {60
val next = HashMap<OsString, OsString>()61
for ((key, item) in environment)62
next.put(key, item)63
next.put(name, value)64
environment = next65
}66
public fun removeEnv(name: OsString): Unit {67
val next = HashMap<OsString, OsString>()68
for ((key, item) in environment)69
if (key != name)70
next.put(key, item)71
environment = next72
}73
public fun clearEnv(): Unit {74
environment = HashMap<OsString, OsString>()75
}76
internal fun snapshot(executable: Path): Command =77
Command(78
executable,79
arguments,80
environment,81
workingDirectory,82
stdin,83
stdout,84
stderr,85
mergeError86
)87
}