mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
35 lines
969 B
Swift
35 lines
969 B
Swift
import Foundation
|
|
import PackagePlugin
|
|
|
|
extension Path {
|
|
var directoryContainsConfigFile: Bool {
|
|
FileManager.default.fileExists(atPath: "\(self)/.swiftlint.yml")
|
|
}
|
|
|
|
var depth: Int {
|
|
URL(fileURLWithPath: "\(self)").pathComponents.count
|
|
}
|
|
|
|
func isDescendant(of path: Path) -> Bool {
|
|
"\(self)".hasPrefix("\(path)")
|
|
}
|
|
|
|
func resolveWorkingDirectory(in directory: Path) throws -> Path {
|
|
guard "\(self)".hasPrefix("\(directory)") else {
|
|
throw SwiftLintBuildToolPluginError.pathNotInDirectory(path: self, directory: directory)
|
|
}
|
|
|
|
let path: Path? = sequence(first: self) { path in
|
|
let path: Path = path.removingLastComponent()
|
|
guard "\(path)".hasPrefix("\(directory)") else {
|
|
return nil
|
|
}
|
|
return path
|
|
}
|
|
.reversed()
|
|
.first(where: \.directoryContainsConfigFile)
|
|
|
|
return path ?? directory
|
|
}
|
|
}
|