Skip to content

Commit

Permalink
Relax SwiftLint version logic to fallback to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio committed Apr 8, 2024
1 parent 065876e commit 422950b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
// See https://github.com/erikdoe/ocmock/issues/500#issuecomment-1002700625
.package(url: "https://github.com/erikdoe/ocmock", revision: "afd2c6924e8a36cb872bc475248b978f743c6050"),
.package(url: "https://github.com/Quick/Quick", from: "6.0.0"),
.package(url: "https://github.com/realm/SwiftLint", .exactItem(loadSwiftLintVersion()))
swiftLintPlugin()
],
targets: [
.target(
Expand Down Expand Up @@ -62,25 +62,42 @@ let package = Package(
]
)

func loadSwiftLintVersion() -> Version {
func swiftLintPlugin() -> PackageDescription.Package.Dependency {
let url = "https://github.com/realm/SwiftLint"

// Swift can access and read the config file with the current logic only within the package repo.
// This is, the check will fail when 'swift build' attempts to resolve the package as a dependency.
// To workaround this limitation, we fall back to the latest version of the plugin when the config file read fails.
// This should be safe to do, given the SwiftLint plugin is only used in development.
guard let version = loadSwiftLintVersion() else {
return .package(url: url, from: "0.54.0")
}

return .package(url: url, .exactItem(version))
}

func loadSwiftLintVersion() -> Version? {
let swiftLintConfigURL = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent(".swiftlint.yml")

guard let yamlString = try? String(contentsOf: swiftLintConfigURL) else {
fatalError("Failed to read SwiftLint config file at \(swiftLintConfigURL).")
print("[!] Failed to read SwiftLint config file at \(swiftLintConfigURL).")
return .none
}

guard let versionLine = yamlString.components(separatedBy: .newlines)
.first(where: { $0.contains("swiftlint_version") }) else {
fatalError("SwiftLint version not found in YAML file.")
print("[!] SwiftLint version not found in YAML file.")
return .none
}

// Assumes the format `swiftlint_version: <version>`
guard let version = Version(versionLine.components(separatedBy: ":")
.last?
.trimmingCharacters(in: .whitespaces) ?? "") else {
fatalError("Failed to extract SwiftLint version.")
print("[!] Failed to extract SwiftLint version.")
return .none
}

return version
Expand Down

0 comments on commit 422950b

Please sign in to comment.