@@ -15,130 +15,87 @@ final class EnvManager {
1515 domains. append ( domain)
1616 }
1717
18- func envBoolValue( rawKey: String , default defaultValue: Bool , searchInDomain: Bool ) -> Bool {
19- func _envBoolValue( _ key: String ) -> ( String , Bool ) ? {
18+ private func envValue< T> (
19+ rawKey: String ,
20+ default defaultValue: T ,
21+ searchInDomain: Bool ,
22+ parser: ( String ) -> T ?
23+ ) -> T {
24+ func parseEnvValue( _ key: String ) -> ( String , T ) ? {
2025 guard let value = Context . environment [ key] else {
2126 return nil
2227 }
23- let result : Bool
24- if value == " 1 " {
25- result = true
26- } else if value == " 0 " {
27- result = false
28- } else {
28+ guard let result = parser ( value) else {
2929 return nil
3030 }
3131 return ( value, result)
3232 }
33- if searchInDomain {
34- for domain in domains {
35- let key = " \( domain. uppercased ( ) ) _ \( rawKey) "
36- guard let ( value, result) = _envBoolValue ( key) else {
37- continue
38- }
33+ let keys : [ String ] = searchInDomain
34+ ? domains. map { " \( $0. uppercased ( ) ) _ \( rawKey) " }
35+ : [ rawKey]
36+ for key in keys {
37+ if let ( value, result) = parseEnvValue ( key) {
3938 print ( " [Env] \( key) = \( value) -> \( result) " )
4039 return result
4140 }
42- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
43- return defaultValue
44- } else {
45- guard let ( value, result) = _envBoolValue ( rawKey) else {
46- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
47- return defaultValue
48- }
49- print ( " [Env] \( rawKey) = \( value) -> \( result) " )
50- return result
5141 }
42+ let primaryKey = keys. first ?? rawKey
43+ print ( " [Env] \( primaryKey) not set -> \( defaultValue) (default) " )
44+ return defaultValue
5245 }
5346
54- func envIntValue( rawKey: String , default defaultValue: Int , searchInDomain: Bool ) -> Int {
55- func _envIntValue( _ key: String ) -> ( String , Int ) ? {
56- guard let value = Context . environment [ key] else {
57- return nil
58- }
59- let result : Int
60- guard let result = Int ( value) else {
61- return nil
62- }
63- return ( value, result)
64- }
65- if searchInDomain {
66- for domain in domains {
67- let key = " \( domain. uppercased ( ) ) _ \( rawKey) "
68- guard let ( value, result) = _envIntValue ( key) else {
69- continue
70- }
71- print ( " [Env] \( key) = \( value) -> \( result) " )
72- return result
73- }
74- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
75- return defaultValue
76- } else {
77- guard let ( value, result) = _envIntValue ( rawKey) else {
78- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
79- return defaultValue
47+ func envBoolValue( rawKey: String , default defaultValue: Bool , searchInDomain: Bool ) -> Bool {
48+ envValue ( rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { value in
49+ switch value {
50+ case " 1 " : true
51+ case " 0 " : false
52+ default : nil
8053 }
81- print ( " [Env] \( rawKey) = \( value) -> \( result) " )
82- return result
8354 }
8455 }
8556
57+ func envIntValue( rawKey: String , default defaultValue: Int , searchInDomain: Bool ) -> Int {
58+ envValue ( rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { Int ( $0) }
59+ }
60+
8661 func envStringValue( rawKey: String , default defaultValue: String , searchInDomain: Bool ) -> String {
87- func _envStringValue( _ key: String ) -> ( String , String ) ? {
88- guard let value = Context . environment [ key] else {
89- return nil
90- }
91- return ( value, value)
92- }
93- if searchInDomain {
94- for domain in domains {
95- let key = " \( domain. uppercased ( ) ) _ \( rawKey) "
96- guard let ( value, result) = _envStringValue ( key) else {
97- continue
98- }
99- print ( " [Env] \( key) = \( value) -> \( result) " )
100- return result
101- }
102- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
103- return defaultValue
104- } else {
105- guard let ( value, result) = _envStringValue ( rawKey) else {
106- print ( " [Env] \( rawKey) not set -> \( defaultValue) (default) " )
107- return defaultValue
108- }
109- print ( " [Env] \( rawKey) = \( value) -> \( result) " )
110- return result
111- }
62+ envValue ( rawKey: rawKey, default: defaultValue, searchInDomain: searchInDomain) { $0 }
11263 }
11364}
11465EnvManager . shared. register ( domain: " OpenAttributeGraph " )
11566EnvManager . shared. register ( domain: " OpenSwiftUI " )
11667
11768@MainActor
118- func envEnable ( _ key: String , default defaultValue: Bool = false , searchInDomain: Bool = true ) -> Bool {
69+ func envBoolValue ( _ key: String , default defaultValue: Bool = false , searchInDomain: Bool = true ) -> Bool {
11970 EnvManager . shared. envBoolValue ( rawKey: key, default: defaultValue, searchInDomain: searchInDomain)
12071}
12172
12273@MainActor
123- func envValue ( _ key: String , default defaultValue: Int , searchInDomain: Bool = true ) -> Int {
74+ func envIntValue ( _ key: String , default defaultValue: Int , searchInDomain: Bool = true ) -> Int {
12475 EnvManager . shared. envIntValue ( rawKey: key, default: defaultValue, searchInDomain: searchInDomain)
12576}
12677
78+ @MainActor
79+ func envStringValue( _ key: String , default defaultValue: String , searchInDomain: Bool = true ) -> String {
80+ EnvManager . shared. envStringValue ( rawKey: key, default: defaultValue, searchInDomain: searchInDomain)
81+ }
82+
83+
12784// MARK: - Env and config
12885
12986#if os(macOS)
13087// 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.
131- let buildForDarwinPlatform = envEnable ( " BUILD_FOR_DARWIN_PLATFORM " , default: true )
88+ let buildForDarwinPlatform = envBoolValue ( " BUILD_FOR_DARWIN_PLATFORM " , default: true )
13289#else
133- let buildForDarwinPlatform = envEnable ( " BUILD_FOR_DARWIN_PLATFORM " )
90+ let buildForDarwinPlatform = envBoolValue ( " BUILD_FOR_DARWIN_PLATFORM " )
13491#endif
13592// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/3061#issuecomment-2118821061
13693// By-pass https://github.com/swiftlang/swift-package-manager/issues/7580
137- let isSPIDocGenerationBuild = envEnable ( " SPI_GENERATE_DOCS " , searchInDomain: false )
138- let isSPIBuild = envEnable ( " SPI_BUILD " , searchInDomain: false )
94+ let isSPIDocGenerationBuild = envBoolValue ( " SPI_GENERATE_DOCS " , searchInDomain: false )
95+ let isSPIBuild = envBoolValue ( " SPI_BUILD " , searchInDomain: false )
13996
14097let isXcodeEnv = Context . environment [ " __CFBundleIdentifier " ] == " com.apple.dt.Xcode "
141- let development = envEnable ( " DEVELOPMENT " , default: false )
98+ let development = envBoolValue ( " DEVELOPMENT " , default: false )
14299
143100let libSwiftPath = {
144101 // From Swift toolchain being installed or from Swift SDK.
@@ -154,7 +111,7 @@ let libSwiftPath = {
154111
155112let swiftToolchainPath = Context . environment [ " OPENATTRIBUTEGRAPH_SWIFT_TOOLCHAIN_PATH " ] ?? ( development ? " /Volumes/BuildMachine/swift-project " : " " )
156113let swiftToolchainVersion = Context . environment [ " OPENATTRIBUTEGRAPH_SWIFT_TOOLCHAIN_VERSION " ] ?? ( development ? " 6.0.2 " : " " )
157- let swiftToolchainSupported = envEnable ( " SWIFT_TOOLCHAIN_SUPPORTED " , default: !swiftToolchainVersion. isEmpty)
114+ let swiftToolchainSupported = envBoolValue ( " SWIFT_TOOLCHAIN_SUPPORTED " , default: !swiftToolchainVersion. isEmpty)
158115
159116
160117
@@ -233,14 +190,14 @@ if releaseVersion >= 2021 {
233190
234191// MARK: - [env] OPENATTRIBUTEGRAPH_WERROR
235192
236- let warningsAsErrorsCondition = envEnable ( " WERROR " , default: isXcodeEnv && development)
193+ let warningsAsErrorsCondition = envBoolValue ( " WERROR " , default: isXcodeEnv && development)
237194if warningsAsErrorsCondition {
238195 sharedSwiftSettings. append ( . unsafeFlags( [ " -warnings-as-errors " ] ) )
239196}
240197
241198// MARK: - [env] OPENATTRIBUTEGRAPH_LIBRARY_EVOLUTION
242199
243- let libraryEvolutionCondition = envEnable ( " LIBRARY_EVOLUTION " , default: buildForDarwinPlatform)
200+ let libraryEvolutionCondition = envBoolValue ( " LIBRARY_EVOLUTION " , default: buildForDarwinPlatform)
244201
245202if libraryEvolutionCondition {
246203 // NOTE: -enable-library-evolution will cause module verify failure for `swift build`.
@@ -250,7 +207,7 @@ if libraryEvolutionCondition {
250207
251208// MARK: - [env] OPENATTRIBUTEGRAPH_COMPATIBILITY_TEST
252209
253- let compatibilityTestCondition = envEnable ( " COMPATIBILITY_TEST " , default: false )
210+ let compatibilityTestCondition = envBoolValue ( " COMPATIBILITY_TEST " , default: false )
254211sharedCSettings. append ( . define( " OPENATTRIBUTEGRAPH " , to: compatibilityTestCondition ? " 1 " : " 0 " ) )
255212if !compatibilityTestCondition {
256213 sharedSwiftSettings. append ( . define( " OPENATTRIBUTEGRAPH " ) )
@@ -365,9 +322,9 @@ if buildForDarwinPlatform {
365322 package . targets. append ( openAttributeGraphCompatibilityTestsTarget)
366323}
367324
368- let useLocalDeps = envEnable ( " USE_LOCAL_DEPS " )
325+ let useLocalDeps = envBoolValue ( " USE_LOCAL_DEPS " )
369326
370- let attributeGraphCondition = envEnable ( " ATTRIBUTEGRAPH " , default: buildForDarwinPlatform && !isSPIBuild)
327+ let attributeGraphCondition = envBoolValue ( " ATTRIBUTEGRAPH " , default: buildForDarwinPlatform && !isSPIBuild)
371328
372329if attributeGraphCondition {
373330 let privateFrameworkRepo : Package . Dependency
0 commit comments