Skip to content

Commit

Permalink
Merge pull request #351 from wordpress-mobile/iangmaia/prototype-load…
Browse files Browse the repository at this point in the history
…-swiftlint-version-on-spm

RFC: Load SwiftLint version in the Package Description
  • Loading branch information
iangmaia authored Feb 26, 2024
2 parents a4179d3 + 8b77cd8 commit 2298da4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// swift-tools-version:5.5

import Foundation
import PackageDescription

let package = Package(
Expand All @@ -14,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", from: "0.54.0")
.package(url: "https://github.com/realm/SwiftLint", .exactItem(loadSwiftLintVersion()))
],
targets: [
.target(
Expand Down Expand Up @@ -60,3 +61,25 @@ let package = Package(
),
]
)

func loadSwiftLintVersion() -> Version {
guard let yamlString = try? String(contentsOf: URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent(".swiftlint.yml")) else {
fatalError("Failed to read YAML file.")
}

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

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

return version
}

0 comments on commit 2298da4

Please sign in to comment.