|
12 | 12 |
|
13 | 13 | import Foundation
|
14 | 14 |
|
15 |
| -@TaskLocal fileprivate var processEnvironment = ProcessInfo.processInfo.environment |
| 15 | +public struct Environment { |
| 16 | + var storage: [EnvironmentKey: String] |
| 17 | +} |
| 18 | + |
| 19 | +// MARK: - Accessors |
| 20 | + |
| 21 | +extension Environment { |
| 22 | + package init() { |
| 23 | + self.storage = .init() |
| 24 | + } |
| 25 | + |
| 26 | + package subscript(_ key: EnvironmentKey) -> String? { |
| 27 | + _read { yield self.storage[key] } |
| 28 | + _modify { yield &self.storage[key] } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// MARK: - Conversions between Dictionary<String, String> |
| 33 | + |
| 34 | +extension Environment { |
| 35 | + public init(_ dictionary: [String: String]) { |
| 36 | + self.storage = .init() |
| 37 | + let sorted = dictionary.sorted { $0.key < $1.key } |
| 38 | + for (key, value) in sorted { |
| 39 | + self.storage[.init(key)] = value |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension [String: String] { |
| 45 | + public init(_ environment: Environment) { |
| 46 | + self.init() |
| 47 | + let sorted = environment.sorted { $0.key < $1.key } |
| 48 | + for (key, value) in sorted { |
| 49 | + self[key.rawValue] = value |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// MARK: - Path Modification |
| 55 | + |
| 56 | +extension Environment { |
| 57 | + package mutating func prependPath(key: EnvironmentKey, value: String) { |
| 58 | + guard !value.isEmpty else { return } |
| 59 | + if let existing = self[key] { |
| 60 | + self[key] = "\(value)\(Path.pathEnvironmentSeparator)\(existing)" |
| 61 | + } else { |
| 62 | + self[key] = value |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + package mutating func appendPath(key: EnvironmentKey, value: String) { |
| 67 | + guard !value.isEmpty else { return } |
| 68 | + if let existing = self[key] { |
| 69 | + self[key] = "\(existing)\(Path.pathEnvironmentSeparator)\(value)" |
| 70 | + } else { |
| 71 | + self[key] = value |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - Global Environment |
| 77 | + |
| 78 | +extension Environment { |
| 79 | + fileprivate static let _cachedCurrent = SWBMutex<Self?>(nil) |
16 | 80 |
|
17 |
| -/// Binds the internal defaults to the specified `environment` for the duration of the synchronous `operation`. |
18 |
| -/// - parameter clean: `true` to start with a clean environment, `false` to merge the input environment over the existing process environment. |
19 |
| -/// - note: This is implemented via task-local values. |
20 |
| -@_spi(Testing) public func withEnvironment<R>(_ environment: [String: String], clean: Bool = false, operation: () throws -> R) rethrows -> R { |
21 |
| - try $processEnvironment.withValue(clean ? environment : processEnvironment.addingContents(of: environment), operation: operation) |
| 81 | + /// Vends a copy of the current process's environment variables. |
| 82 | + /// |
| 83 | + /// Mutations to the current process's global environment are not reflected |
| 84 | + /// in the returned value. |
| 85 | + public static var current: Self { |
| 86 | + Self._cachedCurrent.withLock { cachedValue in |
| 87 | + if let cachedValue = cachedValue { |
| 88 | + return cachedValue |
| 89 | + } else { |
| 90 | + let current = Self(ProcessInfo.processInfo.environment) |
| 91 | + cachedValue = current |
| 92 | + return current |
| 93 | + } |
| 94 | + } |
| 95 | + } |
22 | 96 | }
|
23 | 97 |
|
24 |
| -/// Binds the internal defaults to the specified `environment` for the duration of the asynchronous `operation`. |
25 |
| -/// - parameter clean: `true` to start with a clean environment, `false` to merge the input environment over the existing process environment. |
26 |
| -/// - note: This is implemented via task-local values. |
27 |
| -@_spi(Testing) public func withEnvironment<R>(_ environment: [String: String], clean: Bool = false, operation: () async throws -> R) async rethrows -> R { |
28 |
| - try await $processEnvironment.withValue(clean ? environment : processEnvironment.addingContents(of: environment), operation: operation) |
| 98 | +// MARK: - Protocol Conformances |
| 99 | + |
| 100 | +extension Environment: Collection { |
| 101 | + public struct Index: Comparable { |
| 102 | + public static func < (lhs: Self, rhs: Self) -> Bool { |
| 103 | + lhs.underlying < rhs.underlying |
| 104 | + } |
| 105 | + |
| 106 | + var underlying: Dictionary<EnvironmentKey, String>.Index |
| 107 | + } |
| 108 | + |
| 109 | + public typealias Element = (key: EnvironmentKey, value: String) |
| 110 | + |
| 111 | + public var startIndex: Index { |
| 112 | + Index(underlying: self.storage.startIndex) |
| 113 | + } |
| 114 | + |
| 115 | + public var endIndex: Index { |
| 116 | + Index(underlying: self.storage.endIndex) |
| 117 | + } |
| 118 | + |
| 119 | + public subscript(index: Index) -> Element { |
| 120 | + self.storage[index.underlying] |
| 121 | + } |
| 122 | + |
| 123 | + public func index(after index: Self.Index) -> Self.Index { |
| 124 | + Index(underlying: self.storage.index(after: index.underlying)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension Environment: CustomStringConvertible { |
| 129 | + public var description: String { |
| 130 | + let body = self |
| 131 | + .sorted { $0.key < $1.key } |
| 132 | + .map { "\"\($0.rawValue)=\($1)\"" } |
| 133 | + .joined(separator: ", ") |
| 134 | + return "[\(body)]" |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +extension Environment: Encodable { |
| 139 | + public func encode(to encoder: any Swift.Encoder) throws { |
| 140 | + try self.storage.encode(to: encoder) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +extension Environment: Equatable {} |
| 145 | + |
| 146 | +extension Environment: ExpressibleByDictionaryLiteral { |
| 147 | + public typealias Key = EnvironmentKey |
| 148 | + public typealias Value = String |
| 149 | + |
| 150 | + public init(dictionaryLiteral elements: (Key, Value)...) { |
| 151 | + self.storage = .init() |
| 152 | + for (key, value) in elements { |
| 153 | + self.storage[key] = value |
| 154 | + } |
| 155 | + } |
29 | 156 | }
|
30 | 157 |
|
31 |
| -/// Gets the value of the named variable from the process' environment. |
32 |
| -/// - parameter name: The name of the environment variable. |
33 |
| -/// - returns: The value of the variable as a `String`, or `nil` if it is not defined in the environment. |
34 |
| -public func getEnvironmentVariable(_ name: String) -> String? { |
35 |
| - processEnvironment[name] |
| 158 | +extension Environment: Decodable { |
| 159 | + public init(from decoder: any Swift.Decoder) throws { |
| 160 | + self.storage = try .init(from: decoder) |
| 161 | + } |
36 | 162 | }
|
| 163 | + |
| 164 | +extension Environment: Sendable {} |
0 commit comments