Skip to content

Instantly share code, notes, and snippets.

@trszdev
Last active February 19, 2022 14:52
Show Gist options
  • Save trszdev/48711dd6696cf526667acc52eea4d6c8 to your computer and use it in GitHub Desktop.
Save trszdev/48711dd6696cf526667acc52eea4d6c8 to your computer and use it in GitHub Desktop.
Package.swift boilerplate
// swift-tools-version:5.5
import PackageDescription
var package = Package(
name: "App",
defaultLocalization: "en",
platforms: [.iOS(.v14)]
)
// MARK: - Dependencies
struct ExternalDependency {
var id = Int.random(in: Int.min...Int.max)
var target: Target.Dependency
var package: Package.Dependency
static let kingfisher = Self(
target: .product(name: "Kingfisher", package: "Kingfisher"),
package: .package(url: "https://github.com/onevcat/Kingfisher.git", "7.0.0"..."8.0.0")
)
}
// MARK: - Modules
struct Module {
var name: String
var moduleDependencies = [Self]()
var externalDependencies = [ExternalDependency]()
var resources: [Resource]?
var isTestTarget = false
var exclude = [String]()
static let navigationCore = Self(
name: "NavigationCore",
moduleDependencies: [],
externalDependencies: [.kingfisher]
)
static let all: [Self] = [
.navigationCore,
.commonTestModule(for: .navigationCore)
]
private static func commonTestModule(for module: Self) -> Self {
Self(name: "\(module.name)Tests", moduleDependencies: [module], isTestTarget: true)
}
}
// MARK: - Configure package
extension ExternalDependency: Hashable, Identifiable {
static func == (lhs: ExternalDependency, rhs: ExternalDependency) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
extension Module {
var target: PackageDescription.Target {
let moduleDependencies = moduleDependencies.lazy.map(\.name).map(Target.Dependency.init(stringLiteral:))
let externalDependencies = externalDependencies.map(\.target)
let dependencies = moduleDependencies + externalDependencies
if isTestTarget {
return .testTarget(name: name, dependencies: dependencies, exclude: exclude, resources: resources)
} else {
return .target(name: name, dependencies: dependencies, exclude: exclude, resources: resources)
}
}
}
let externalDependencies = Set(Module.all.flatMap(\.externalDependencies))
package.dependencies.append(contentsOf: externalDependencies.map(\.package))
for module in Module.all {
if !module.isTestTarget {
package.products.append(.library(name: module.name, targets: [module.name]))
}
package.targets.append(module.target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment