|
2 | 2 | // CommandsENV.swift |
3 | 3 | // |
4 | 4 | // |
5 | | -// Created by zhifei qiu on 2021/8/2. |
| 5 | +// Created by zhifei qiu on 2021/8/25. |
6 | 6 | // |
7 | 7 |
|
8 | 8 | import Foundation |
9 | 9 |
|
10 | | -public protocol CommandsENV { |
11 | | - var executableURL: String { get } |
12 | | - var dashc: String? { get } |
13 | | - |
14 | | - func run(_ command: String, environment: [String: String]?) -> Commands.Result |
15 | | - func system(_ command: String, environment: [String: String]?) |
16 | | - func system(_ command: String, environment: [String: String]?, output: ((String) -> Void)?, errorOutput: ((String) -> Void)?) |
| 10 | +extension Commands { |
| 11 | + public struct ENV { |
| 12 | + public private(set) var data: [String: String] = [:] |
| 13 | + |
| 14 | + public init(_ data: [String: String]? = ProcessInfo.processInfo.environment) { |
| 15 | + self.data = data ?? [:] |
| 16 | + } |
| 17 | + } |
17 | 18 | } |
18 | 19 |
|
19 | | -public extension CommandsENV { |
20 | | - @discardableResult |
21 | | - func run(_ command: String, environment: [String: String]? = nil) -> Commands.Result { |
22 | | - let request = prepare(command, environment: environment) |
23 | | - return Commands.Task.run(request) |
| 20 | +public extension Commands.ENV { |
| 21 | + mutating func add(PATH: String) { |
| 22 | + data["PATH"] = data["PATH"] == nil ? PATH : "\(PATH):\(data["PATH"]!)" |
24 | 23 | } |
25 | | - |
26 | | - func system(_ command: String, environment: [String: String]? = nil) { |
27 | | - let request = prepare(command, environment: environment) |
28 | | - Commands.Task.system(request) |
| 24 | +} |
| 25 | + |
| 26 | +public extension Commands.ENV { |
| 27 | + subscript(_ key: String) -> String? { |
| 28 | + set(newValue) { |
| 29 | + data[key] = newValue |
| 30 | + } |
| 31 | + get { |
| 32 | + return data[key] |
| 33 | + } |
29 | 34 | } |
30 | | - |
31 | | - func system(_ command: String, environment: [String: String]? = nil, output: ((String) -> Void)?, errorOutput: ((String) -> Void)?) { |
32 | | - let request = prepare(command, environment: environment) |
33 | | - Commands.Task.system(request, output: output, errorOutput: errorOutput) |
| 35 | +} |
| 36 | + |
| 37 | +extension Commands.ENV: Swift.ExpressibleByDictionaryLiteral { |
| 38 | + public init(dictionaryLiteral elements: (String, String)...) { |
| 39 | + let dictionary = elements.reduce(into: [String: String](), { $0[$1.0] = $1.1}) |
| 40 | + self.init(dictionary) |
34 | 41 | } |
35 | 42 | } |
36 | 43 |
|
37 | | -private extension CommandsENV { |
38 | | - func prepare(_ command: String, environment: [String: String]? = nil) -> Commands.Request { |
39 | | - return Commands.Request(executableURL, dashc: dashc, command: command, environment: environment) |
| 44 | +extension Commands.ENV: Equatable { |
| 45 | + public static func == (lhs: Self, rhs: Self) -> Bool { |
| 46 | + lhs.data == rhs.data |
40 | 47 | } |
41 | 48 | } |
0 commit comments