-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathURL+Helpers.swift
More file actions
37 lines (31 loc) · 1.05 KB
/
URL+Helpers.swift
File metadata and controls
37 lines (31 loc) · 1.05 KB
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
35
36
37
import Foundation
import PackagePlugin
extension URL {
var filepath: String {
withUnsafeFileSystemRepresentation { String(cString: $0!) }
}
var depth: Int {
pathComponents.count
}
func isDescendant(of other: URL) -> Bool {
path().hasPrefix(other.path())
}
func resolvedWorkingDirectory(in directory: URL) throws -> URL {
guard isDescendant(of: directory) else {
throw SwiftLintBuildToolPluginError.pathNotInDirectory(path: self, directory: directory)
}
let path: URL? = sequence(first: self) { path in
let path = path.deletingLastPathComponent()
guard path.isDescendant(of: directory) else {
return nil
}
return path
}
.reversed()
.first {
let file = $0.appending(path: ".swiftlint.yml", directoryHint: .notDirectory).filepath
return FileManager.default.fileExists(atPath: file)
}
return path ?? directory
}
}