-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathPath+Helpers.swift
More file actions
34 lines (28 loc) · 969 Bytes
/
Path+Helpers.swift
File metadata and controls
34 lines (28 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
}
}