scoop:scoop.platform:0.1.0
src/process/command.scoop
Selected production source · src/process/command.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.*11
/**12
* The standard-stream routing chosen when spawning a child process.13
*/14
public enum Stdio {15
/**16
* Inherit the parent's corresponding standard stream.17
*/18
Inherit,19
/**20
* Connect the stream to the platform null device.21
*/22
Null,23
/**24
* Create a pipe exposed through ChildProcess.25
*/26
Pipe,27
/**28
* Redirect through the supplied open file handle.29
*/30
Redirect(val file: File)31
}33
/**34
* A process launch description with copied argument/environment data, an explicit working35
* directory, and standard-stream routing. Arguments are passed directly without shell36
* interpretation. Windows uses CRT argument encoding and leaves ordinary nonempty arguments37
* unquoted. Scripts passed to an explicitly selected interpreter still follow its syntax.38
*/39
public class Command internal constructor(40
/**41
* The executable path selected for spawning.42
*/43
public val executable: Path,44
arguments: Array<OsString> = [],45
environment: Map<OsString, OsString> = currentEnvironment(),46
/**47
* The child's initial working directory.48
*/49
public val workingDirectory: Path = currentDirectory(),50
/**51
* The child standard-input routing.52
*/53
public val stdin: Stdio = Stdio.Inherit,54
/**55
* The child standard-output routing.56
*/57
public val stdout: Stdio = Stdio.Inherit,58
/**59
* The child standard-error routing when not merged.60
*/61
public val stderr: Stdio = Stdio.Inherit,62
/**63
* Whether stderr is redirected to the child's selected stdout.64
*/65
public val mergeError: Boolean = false66
) {67
/**68
* The independent argument snapshot passed to the child.69
*/70
public val arguments: Array<OsString> = Array<OsString>(arguments.size) { arguments[it] }71
/**72
* The environment snapshot supplied to the child.73
*/74
public val environment: Map<OsString, OsString> = environment.toMap()76
init {77
if (mergeError) {78
when (stderr) {79
case Stdio.Inherit -> {}80
case _ ->81
throw IllegalArgumentException(82
Some("merged stderr cannot be redirected separately")83
)84
}85
}86
}88
// Starts the process; PATH lookup is opt-in.89
/**90
* Starts an owned child process. PATH lookup occurs only when searchPath is requested.91
*/92
public fun spawn(searchPath: Boolean = false): ChildProcess {93
val child = ChildProcess(isPipe(stdin), isPipe(stdout), isPipe(stderr))94
val result = MutableArray<NativeFileHandle>(3L) { NativeFileHandle(ULong.MAX_VALUE) }95
return launchProcess("spawn", searchPath) { packed, path ->96
val spawned = packed.spawn(path, stdin, stdout, stderr, mergeError, result)97
if (spawned.process.value != ULong.MAX_VALUE) {98
child.adopt(spawned.process, result)99
(Some(child), 0)100
} else {101
(None, spawned.error)102
}103
}104
}105
}107
internal fun <T> Command.launchProcess(108
operation: String,109
searchPath: Boolean,110
attempt: (SpawnInput, Path) -> (T?, Int)111
): T {112
val packed = SpawnInput(executable, arguments, environment, workingDirectory)113
val paths = if (searchPath) searchCandidates(executable, environment) else [executable]114
var denied: PlatformException? = None115
var missing: PlatformException? = None116
for (path in paths) {117
val(result, code) = attempt(packed, path)118
when (result) {119
case Some(val child) -> return child120
case None -> {}121
}122
val error = processError(code)123
val failure = PlatformException(operation, error.kind, Some(error), Some(path.toOsString()))124
if (!searchPath) throw failure125
when (error.kind) {126
case PlatformErrorKind.PermissionDenied -> denied = Some(failure)127
case PlatformErrorKind.NotFound -> missing = Some(failure)128
case PlatformErrorKind.NotDirectory -> missing = Some(failure)129
case _ -> throw failure130
}131
}132
throw denied ?: missing!!133
}135
internal fun isPipe(value: Stdio): Boolean = when (value) {136
case Stdio.Pipe -> true137
case _ -> false138
}