From 422950b28f01d7cc11218e7d70a6cd65004d23ae Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 8 Apr 2024 13:36:17 +1000 Subject: [PATCH] Relax SwiftLint version logic to fallback to latest version --- Package.swift | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 78fe677..db209b2 100644 --- a/Package.swift +++ b/Package.swift @@ -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( @@ -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: ` 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