Added helper method to retrieve Environment variables (#1468)

* Adding a helper method to retrieve Environment variables as their raw value

* Removing chance of a String being optional

* Added default values for both strings and booleans. Updated documentation

* Adding documentation for helper methods

Co-authored-by: Michael Miscampbell <mmiscampbell@dynamicsignal.com>
This commit is contained in:
Michael Miscampbell 2020-06-27 08:39:36 +01:00 committed by GitHub
parent 8d4c46f707
commit 662303cfbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -5,6 +5,24 @@ public struct Environment {
public enum Value {
case boolean(Bool)
case string(String)
/// Retrieve the Environment value as a string or return the specified default string value
/// - Parameters:
/// - default: default String value to be returned
/// - Returns: String
public func getString(default defaultString: String) -> String {
if case let .string(value) = self { return value }
return defaultString
}
/// Retrieve the Environment value as a boolean or return the specified default boolean value
/// - Parameters:
/// - default: default Boolean value to be returned
/// - Returns: Bool
public func getBoolean(default defaultBoolean: Bool) -> Bool {
if case let .boolean(value) = self { return value }
return defaultBoolean
}
}
public static subscript(dynamicMember member: String) -> Value? {

View File

@ -152,6 +152,16 @@ Accessing variables returns an instance of type `Environment.Value?` which can t
]}
/>
You can also retrieve the string or boolean Environment variable using either of the helper methods defined below, these methods require a default value to be passed to ensure the user gets consistent results each time. This avoids the need to define the function appName() defined above.
```swift
Environment.appName?.getString(default: "TuistApp")
```
```swift
Environment.isCI?.getBoolean(default: false)
```
## Package
You can add Swift Packages very similarly to how you add dependencies in a `Package.swift`: