Skip to content

Instantly share code, notes, and snippets.

@Sorix
Last active December 23, 2023 14:26
Show Gist options
  • Save Sorix/21e61347f478ae2e83ef4d8a92d933af to your computer and use it in GitHub Desktop.
Save Sorix/21e61347f478ae2e83ef4d8a92d933af to your computer and use it in GitHub Desktop.
Example of Package.swift with environment variables support
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
static let `default`: Enviroment = .development
case development, production
static func get() -> Enviroment {
if let envPointer = getenv("ENV_TYPE") {
let env = String(cString: envPointer)
return Enviroment(rawValue: env) ?? .default
} else {
return .default
}
}
}
var dependencies: [Package.Dependency] = [
.package(url: "https://github.com/vapor/vapor.git", from: "3.1.0"),
.package(url: "https://github.com/Sorix/whirr-feedback-api-model", .branch("develop"))
]
var appTargetDependencies: [Target.Dependency] = ["Vapor", "FluentSQLite", "APIModel", "GooglePlaces"]
switch Enviroment.get() {
case .production:
dependencies.append(.package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0"))
appTargetDependencies.append("FluentPostgreSQL")
case .development:
dependencies.append(.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"))
appTargetDependencies.append("FluentSQLite")
}
let package = Package(
name: "backend",
dependencies: dependencies,
targets: [
.target(name: "GooglePlaces", dependencies: ["Vapor"]),
.target(name: "App", dependencies: appTargetDependencies),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"])
]
)
@tkafka
Copy link

tkafka commented Jun 13, 2023

Xcode scheme env variables are for app run, not for build time, that's why this doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment