Skip to content

Commit 94f794b

Browse files
committed
Update Package.swift to add EnvManager support
1 parent 174d839 commit 94f794b

File tree

2 files changed

+126
-28
lines changed

2 files changed

+126
-28
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 125 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,156 @@
11
// swift-tools-version: 6.1
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
32

3+
import CompilerPluginSupport
4+
import Foundation
45
import PackageDescription
56

6-
func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool {
7-
guard let value = Context.environment[key] else {
8-
return defaultValue
7+
// MARK: - EnvManager
8+
9+
/* GENERATED BY SPMManifestTool BEGIN */
10+
/* DO NOT EDIT */
11+
12+
public protocol EnvironmentProvider {
13+
func value(forKey key: String) -> String?
14+
}
15+
16+
import PackageDescription
17+
public struct PackageContextEnvironmentProvider: EnvironmentProvider {
18+
public init() {}
19+
20+
public func value(forKey key: String) -> String? {
21+
Context.environment[key]
22+
}
23+
}
24+
25+
public final class EnvManager {
26+
nonisolated(unsafe) public static let shared = EnvManager()
27+
28+
private var domains: [String] = []
29+
private var environmentProvider: EnvironmentProvider
30+
31+
/// When true, append raw key as fallback when searching in domains
32+
public var includeFallbackToRawKey: Bool = false
33+
34+
private init() {
35+
self.environmentProvider = PackageContextEnvironmentProvider()
36+
}
37+
38+
/// Set a custom environment provider (useful for testing)
39+
public func setEnvironmentProvider(_ provider: EnvironmentProvider) {
40+
self.environmentProvider = provider
41+
}
42+
43+
/// Reset domains and environment provider (useful for testing)
44+
public func reset() {
45+
domains.removeAll()
46+
includeFallbackToRawKey = false
47+
self.environmentProvider = PackageContextEnvironmentProvider()
48+
}
49+
50+
public func register(domain: String) {
51+
domains.append(domain)
52+
}
53+
54+
public func withDomain<T>(_ domain: String, perform: () throws -> T) rethrows -> T {
55+
domains.append(domain)
56+
defer { domains.removeAll { $0 == domain } }
57+
return try perform()
958
}
10-
if value == "1" {
11-
return true
12-
} else if value == "0" {
13-
return false
14-
} else {
59+
60+
private func envValue<T>(rawKey: String, default defaultValue: T?, searchInDomain: Bool, parser: (String) -> T?) -> T? {
61+
func parseEnvValue(_ key: String) -> (String, T)? {
62+
guard let value = environmentProvider.value(forKey: key),
63+
let result = parser(value) else { return nil }
64+
return (value, result)
65+
}
66+
var keys: [String] = searchInDomain ? domains.map { "\($0.uppercased())_\(rawKey)" } : []
67+
if !searchInDomain || includeFallbackToRawKey {
68+
keys.append(rawKey)
69+
}
70+
for key in keys {
71+
if let (value, result) = parseEnvValue(key) {
72+
print("[Env] \(key)=\(value) -> \(result)")
73+
return result
74+
}
75+
}
76+
let primaryKey = keys.first ?? rawKey
77+
if let defaultValue {
78+
print("[Env] \(primaryKey) not set -> \(defaultValue)(default)")
79+
}
1580
return defaultValue
1681
}
82+
83+
public func envBoolValue(rawKey: String, default defaultValue: Bool? = nil, searchInDomain: Bool) -> Bool? {
84+
envValue(rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { value in
85+
switch value {
86+
case "1": true
87+
case "0": false
88+
default: nil
89+
}
90+
}
91+
}
92+
93+
public func envIntValue(rawKey: String, default defaultValue: Int? = nil, searchInDomain: Bool) -> Int? {
94+
envValue(rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { Int($0) }
95+
}
96+
97+
public func envStringValue(rawKey: String, default defaultValue: String? = nil, searchInDomain: Bool) -> String? {
98+
envValue(rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { $0 }
99+
}
17100
}
18101

19-
var sharedSwiftSettings: [SwiftSetting] = [
20-
.swiftLanguageMode(.v6),
21-
.enableUpcomingFeature("InternalImportsByDefault"),
22-
]
102+
public func envBoolValue(_ key: String, default defaultValue: Bool = false, searchInDomain: Bool = true) -> Bool {
103+
EnvManager.shared.envBoolValue(rawKey: key, default: defaultValue, searchInDomain: searchInDomain)!
104+
}
105+
106+
public func envIntValue(_ key: String, default defaultValue: Int = 0, searchInDomain: Bool = true) -> Int {
107+
EnvManager.shared.envIntValue(rawKey: key, default: defaultValue, searchInDomain: searchInDomain)!
108+
}
109+
110+
public func envStringValue(_ key: String, default defaultValue: String, searchInDomain: Bool = true) -> String {
111+
EnvManager.shared.envStringValue(rawKey: key, default: defaultValue, searchInDomain: searchInDomain)!
112+
}
113+
114+
public func envStringValue(_ key: String, searchInDomain: Bool = true) -> String? {
115+
EnvManager.shared.envStringValue(rawKey: key, searchInDomain: searchInDomain)
116+
}
117+
118+
/* GENERATED BY SPMManifestTool END */
119+
EnvManager.shared.register(domain: "OpenCoreGraphics")
120+
EnvManager.shared.register(domain: "OpenSwiftUI")
121+
122+
// MARK: - Env and config
23123

24124
#if os(macOS)
25125
// NOTE: #if os(macOS) check is not accurate if we are cross compiling for Linux platform. So we add an env key to specify it.
26-
let buildForDarwinPlatform = envEnable("OPENCOREGRAPHICS_BUILD_FOR_DARWIN_PLATFORM", default: true)
126+
let buildForDarwinPlatform = envBoolValue("BUILD_FOR_DARWIN_PLATFORM", default: true)
27127
#else
28-
let buildForDarwinPlatform = envEnable("OPENCOREGRAPHICS_BUILD_FOR_DARWIN_PLATFORM")
128+
let buildForDarwinPlatform = envBoolValue("BUILD_FOR_DARWIN_PLATFORM")
29129
#endif
30130

31131
let isXcodeEnv = Context.environment["__CFBundleIdentifier"] == "com.apple.dt.Xcode"
132+
let development = envBoolValue("DEVELOPMENT")
32133

33-
let development = envEnable("OPENCOREGRAPHICS_DEVELOPMENT")
134+
let coreGraphicsCondition = envBoolValue("COREGRAPHICS", default: buildForDarwinPlatform)
135+
let warningsAsErrorsCondition = envBoolValue("WERROR", default: isXcodeEnv && development)
136+
let libraryEvolutionCondition = envBoolValue("LIBRARY_EVOLUTION", default: buildForDarwinPlatform)
34137

35-
// MARK: - [env] OPENCOREGRAPHICS_COREGRAPHICS
138+
var sharedSwiftSettings: [SwiftSetting] = [
139+
.swiftLanguageMode(.v6),
140+
.enableUpcomingFeature("InternalImportsByDefault"),
141+
]
36142

37-
let coreGraphicsCondition = envEnable("OPENCOREGRAPHICS_COREGRAPHICS", default: buildForDarwinPlatform)
38143
if coreGraphicsCondition {
39-
sharedSwiftSettings.append(.define("OPENCOREGRAPHICS_COREGRAPHICS"))
144+
sharedSwiftSettings.append(.define("COREGRAPHICS"))
40145
}
41146

42-
// MARK: - [env] OPENCOREGRAPHICS_WERROR
43-
44-
let warningsAsErrorsCondition = envEnable("OPENCOREGRAPHICS_WERROR", default: isXcodeEnv && development)
45147
if warningsAsErrorsCondition {
46148
sharedSwiftSettings.append(.unsafeFlags(["-warnings-as-errors"]))
47149
}
48150

49-
// MARK: - [env] OPENCOREGRAPHICS_LIBRARY_EVOLUTION
50-
51-
let libraryEvolutionCondition = envEnable("OPENCOREGRAPHICS_LIBRARY_EVOLUTION", default: buildForDarwinPlatform)
52-
53151
if libraryEvolutionCondition {
54152
// NOTE: -enable-library-evolution will cause module verify failure for `swift build`.
55-
// Either set OPENCOREGRAPHICS_LIBRARY_EVOLUTION=0 or add `-Xswiftc -no-verify-emitted-module-interface` after `swift build`
153+
// Either set LIBRARY_EVOLUTION=0 or add `-Xswiftc -no-verify-emitted-module-interface` after `swift build`
56154
sharedSwiftSettings.append(.unsafeFlags(["-enable-library-evolution", "-no-verify-emitted-module-interface"]))
57155
}
58156

0 commit comments

Comments
 (0)