-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
EnvironmentVariables.swift
60 lines (52 loc) · 1.79 KB
/
EnvironmentVariables.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
public typealias EnvironmentVariables = [String: String]
extension EnvironmentVariables {
public static func empty() -> EnvironmentVariables {
[:]
}
public static func process() -> EnvironmentVariables {
ProcessInfo.processInfo.environment
}
public mutating func prependPath(_ key: String, value: String) {
var values = value.isEmpty ? [] : [value]
if let existing = self[key], !existing.isEmpty {
values.append(existing)
}
self.setPath(key, values)
}
public mutating func appendPath(_ key: String, value: String) {
var values = value.isEmpty ? [] : [value]
if let existing = self[key], !existing.isEmpty {
values.insert(existing, at: 0)
}
self.setPath(key, values)
}
private mutating func setPath(_ key: String, _ values: [String]) {
#if os(Windows)
let delimiter = ";"
#else
let delimiter = ":"
#endif
self[key] = values.joined(separator: delimiter)
}
/// `PATH` variable in the process's environment (`Path` under Windows).
public var path: String? {
#if os(Windows)
let pathArg = "Path"
#else
let pathArg = "PATH"
#endif
return self[pathArg]
}
}