SwiftEnvPlugin is a Swift Package Manager (SPM) build tool plugin designed to generate a constants file (SwiftEnv.swift) automatically based on environment variables. This tool helps developers manage environment-specific configurations efficiently within Swift projects. 🌟🌟🌟
- Automatically generates a Swift file containing constants for environment variables. ✨
- Simplifies managing environment-specific values in Swift projects. ✨
- Fully integrates with Swift Package Manager's plugin system. ✨
- Customizable file and enum names, as well as access modifiers, via an xcconfig file. ✨
To use SwiftEnvPlugin, include it in your Package.swift file: 🌟🌟🌟
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "YourProject",
dependencies: [
.package(url: "https://github.com/mukeshydv/SwiftEnvPlugin.git", from: "1.0.0")
],
targets: [
.target(
name: "YourTarget",
plugins: [
.plugin(name: "SwiftEnvPlugin", package: "SwiftEnvPlugin")
]
)
]
)Create a swiftenv.json file in the root of your project. This file defines the environment variables you want to include in the generated Swift code. Each key in the JSON file represents the name of the Swift variable, and its value is the corresponding environment variable name. 🌟🌟🌟
{
"apiKey": "MY_API_KEY",
"baseUrl": "MY_BASE_URL"
}In this example:
apiKeywill be set to the value of theMY_API_KEYenvironment variable. ✨baseUrlwill be set to the value of theMY_BASE_URLenvironment variable. ✨
You can optionally provide an swiftenv.xcconfig file to customize the generated file and enum names, as well as the access modifier. 🌟🌟🌟
MAPPER_FILE_NAME = swiftenvinput.json
OUTPUT_ENUM_NAME = EnvironmentVar
ACCESS_MODIFIER = internal
MAPPER_FILE_NAME: Specifies the input JSON file name. ✨OUTPUT_ENUM_NAME: Defines the name of the generated enum. ✨ACCESS_MODIFIER: Sets the access level (public,internal, etc.) of the generated constants. ✨
When you build your project using swift build or Xcode, the plugin will: 🌟🌟🌟
- Read the
swiftenv.jsonfile (or the file specified in the.xcconfigfile). - Fetch the values of the specified environment variables.
- Generate a
SwiftEnv.swiftfile in the build directory with the specified enum name and access modifier. - Include the generated file in your build process.
In your Swift code, import the generated file and use the constants: 🌟🌟🌟
import Foundation
let apiKey = SwiftEnv.apiKey
print("API Key: \(apiKey)")// SwiftEnv.swift
enum SwiftEnv {
static let apiKey = "your_api_key"
static let baseUrl = "https://example.com"
}- The plugin reads the
swiftenv.jsonfile (or the specifiedMAPPER_FILE_NAMEin theswiftenv.xcconfigfile) to determine the mapping of Swift variable names to environment variable names. ✨ - It fetches the environment variable values during the build process. ✨
- It generates a
SwiftEnv.swiftfile (or the file with a name and enum specified in theswiftenv.xcconfigfile) in a predefined directory. ✨
Contributions are welcome! If you find a bug or have an idea for an improvement: 🌟🌟🌟
- Fork the repository.
- Create a new branch (
feature/your-featureorbugfix/your-bugfix). - Commit your changes.
- Submit a pull request.
To test the plugin locally: 🌟🌟🌟
- Clone this repository.
- Use
swift buildto build the package. - Link it to a sample project and verify functionality.
This project is licensed under the MIT License. See the LICENSE file for details. 🌟🌟🌟
For questions or suggestions, please open an issue on GitHub or contact Mukesh Yadav. 🌟🌟🌟