The BuildEvironment project lets you store your keys and secrets away from your repository. Secrets can be stored in the .env
file or in environment variables. Simple lightweight and functional solution, platform agnostic.
- Add the package dependency:
.package(url: "https://github.com/DimaRU/BuildEnvironment.git", from: "1.0.0"),
- Add plugin to the target:
plugins: [ .plugin(name: "BuildEnvPlugin", package: "BuildEnvironment") ]
- You may have add platform requirements:
platforms: [.macOS(.v11), .iOS(.v14), .watchOS(.v7), .tvOS(.v14)],
One of the files must be added.
- enum name:
BuildEnvironment
- enum access level:
public
- encode: no
- variables will be taken from the
.env
file.
Caution
The .env
file shouldn't be committed to git as it contains your secrets.
- you can customize code generation options.
- environment variables can be added.
buildenv.config
contains no secrets and can be added to the repository.
# Sample api key
apiKey = "123456"
Generated code:
// Code generated from .env file
// Don't edit! All changes will be lost.
public enum BuildEnvironment {
public static let apiKey: String = "123456"
}
# Sample BuildEnvPlugin config file
# Generated enum name. Default: BuildEnvironment
name: BuildEnvironment
# Generated enum access level.
# Must be one of: public, package, internal. Default: public
access: internal
# Obfuscate data by encode: yes/no. Default: no
encode: yes
# Environment variable list.
# Format: swift_variable_name=$environment_variable_name
userName= $USER
homeDir= $HOME
Generated code:
// Code generated from .env file
// Don't edit! All changes will be lost.
enum BuildEnvironment {
static let userName: String = {
let encrypted: [UInt8] = [191, 121, 218, 23, 127, 221, 219, 20, 179, 99, 13, 164]
let count = encrypted.count / 2
return String(unsafeUninitializedCapacity: count) { ptr in
(0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
return count
}
}()
static let homeDir: String = {
let encrypted: [UInt8] = [245, 33, 241, 173, 4, 190, 7, 163, 71, 155, 203, 189, 211, 218, 116, 130, 200, 118, 205, 40, 199, 42, 242, 191, 207, 170]
let count = encrypted.count / 2
return String(unsafeUninitializedCapacity: count) { ptr in
(0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
return count
}
}()
static let apiKey: String = {
let encrypted: [UInt8] = [41, 83, 240, 172, 236, 228, 24, 97, 195, 152, 217, 210]
let count = encrypted.count / 2
return String(unsafeUninitializedCapacity: count) { ptr in
(0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
return count
}
}()
}
- Clone the current repository
- Enter the
BuildEnvironment
directory - Execute:
swift run BuildEnvExample
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details