Scoop API

scoop:scoop.platform:0.1.0

src/process/command_builder.scoop

Selected production source · src/process/command_builder.scoop

1package scoop.platform.process
3import scoop.collections.Map
4import scoop.collections.toMap
5import scoop.platform.env.currentDirectory
6import scoop.platform.env.environment as currentEnvironment
7import scoop.platform.fs.*
8import scoop.platform.io.*
9import scoop.platform.system.*
10import scoop.collections.HashMap
12/** Configures one complete value; execution remains explicit. */
13public fun command(executable: Path, configure: CommandBuilder.() -> Unit = {}): Command {
14 val builder = CommandBuilder()
15 builder.configure()
16 return builder.snapshot(executable)
19public fun Command.copy(configure: CommandBuilder.() -> Unit = {}): Command {
20 val builder = CommandBuilder(this.environment, this.workingDirectory)
21 builder.arguments = this.arguments
22 builder.environment = this.environment
23 builder.workingDirectory = this.workingDirectory
24 builder.stdin = this.stdin
25 builder.stdout = this.stdout
26 builder.stderr = this.stderr
27 builder.mergeError = this.mergeError
28 builder.configure()
29 return builder.snapshot(this.executable)
32public 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> = initialEnvironment
38 public var workingDirectory: Path = initialDirectory
39 public var stdin: Stdio = Stdio.Inherit
40 public var stdout: Stdio = Stdio.Inherit
41 public var stderr: Stdio = Stdio.Inherit
42 public var mergeError: Boolean = false
44 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 = next
65 }
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 = next
72 }
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 mergeError
86 )