-
Notifications
You must be signed in to change notification settings - Fork 35
Update ProcessExecutor to support environment variables #232
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,8 +11,19 @@ private actor ProcessOutputBuffer { | |||||||||
|
|
||||||||||
| @_spi(Internals) | ||||||||||
| public protocol Executor { | ||||||||||
| /// Executes the command with the given arguments and environment variables. | ||||||||||
| /// | ||||||||||
| /// - Parameters: | ||||||||||
| /// - arguments: Command-line arguments for the process. | ||||||||||
| /// - environment: Complete set of environment variables for the process. | ||||||||||
| /// If `nil`, the current environment is used. | ||||||||||
| /// If non-nil, it **replaces** the entire environment. | ||||||||||
| /// Use `ProcessInfo.processInfo.environment` to preserve existing values if needed. | ||||||||||
| /// | ||||||||||
| /// - Note: | ||||||||||
| /// This does not merge with the existing environment. | ||||||||||
| @discardableResult | ||||||||||
| func execute(_ arguments: [String]) async throws -> ExecutorResult | ||||||||||
| func execute(_ arguments: [String], environment: [String: String]?) async throws -> ExecutorResult | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @_spi(Internals) | ||||||||||
|
|
@@ -43,6 +54,11 @@ public protocol ExecutorResult: Sendable { | |||||||||
| } | ||||||||||
|
|
||||||||||
| public extension Executor { | ||||||||||
| @discardableResult | ||||||||||
| func execute(_ arguments: [String]) async throws -> ExecutorResult { | ||||||||||
| try await execute(arguments, environment: nil) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @discardableResult | ||||||||||
| func execute(_ arguments: String...) async throws -> ExecutorResult { | ||||||||||
| try await execute(arguments) | ||||||||||
|
|
@@ -98,7 +114,7 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable { | |||||||||
|
|
||||||||||
| public var streamOutput: (@Sendable ([UInt8]) async -> Void)? | ||||||||||
|
|
||||||||||
| public func execute(_ arguments: [String]) async throws -> ExecutorResult { | ||||||||||
| public func execute(_ arguments: [String], environment: [String: String]?) async throws -> ExecutorResult { | ||||||||||
| guard let executable = arguments.first, !executable.isEmpty else { | ||||||||||
| throw ProcessExecutorError.executableNotFound | ||||||||||
| } | ||||||||||
|
|
@@ -108,7 +124,7 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable { | |||||||||
| throw ProcessExecutorError.executableNotFound | ||||||||||
| } | ||||||||||
|
|
||||||||||
| let process = Foundation.Process() | ||||||||||
| let process = Process() | ||||||||||
| let stdoutPipe = Pipe() | ||||||||||
| let stderrPipe = Pipe() | ||||||||||
|
|
||||||||||
|
|
@@ -117,6 +133,12 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable { | |||||||||
| process.standardOutput = stdoutPipe | ||||||||||
| process.standardError = stderrPipe | ||||||||||
|
|
||||||||||
| // Completely replace the process environment if provided. | ||||||||||
| // If nil, the process inherits the current environment. | ||||||||||
| if let environment { | ||||||||||
| process.environment = environment | ||||||||||
| } | ||||||||||
|
Comment on lines
+138
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nits] It's enough to be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. Setting the property explicitly to nil will not mean ineheriting the current process environment, rather it may clear out the environment. We should be careful about this behavior.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the implementation in swift-corelibs-foundation. If the property is nil However, I still prefer not to set the property if it's not needed. |
||||||||||
|
|
||||||||||
| let outputHandle = stdoutPipe.fileHandleForReading | ||||||||||
| let errorHandle = stderrPipe.fileHandleForReading | ||||||||||
|
|
||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.