Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around .swiftlint.yml failure when using as a dependency #354

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _None._

### Internal Changes

_None._
- Use Swift 5.10 toolchain. Add macOS 12.0 platform to support SwiftLint [#355]

## 2.3.1

Expand Down
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", exact: 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, exact: version)
}

func loadSwiftLintVersion() -> Version? {
let swiftLintConfigURL = URL(fileURLWithPath: #file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check will fail when 'swift build' attempts to resolve the package as a dependency.

Have you tried using #filePath here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a shot. But, I don't expect it to make much difference.

According to the docs:

Currently, #file has the same value as #filePath.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried via mokagio/swiftlint-file-path branch. Same result 😞

.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