forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
BUILD_WORKSPACE_DIRECTORY
in SwiftLintPlugin (realm#4758)
- Loading branch information
Showing
5 changed files
with
181 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,34 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
#if os(Linux) | ||
import Glibc | ||
#else | ||
import Darwin | ||
#endif | ||
|
||
extension Path { | ||
/// Scans the receiver, then all of its parents looking for a configuration file with the name ".swiftlint.yml". | ||
/// | ||
/// - returns: Path to the configuration file, or nil if one cannot be found. | ||
func firstConfigurationFileInParentDirectories() -> Path? { | ||
let defaultConfigurationFileName = ".swiftlint.yml" | ||
let proposedDirectory = sequence( | ||
first: self, | ||
next: { path in | ||
guard path.stem.count > 1 else { | ||
// Check we're not at the root of this filesystem, as `removingLastComponent()` | ||
// will continually return the root from itself. | ||
return nil | ||
} | ||
var directoryContainsConfigFile: Bool { | ||
FileManager.default.fileExists(atPath: "\(self)/.swiftlint.yml") | ||
} | ||
|
||
return path.removingLastComponent() | ||
} | ||
).first { path in | ||
let potentialConfigurationFile = path.appending(subpath: defaultConfigurationFileName) | ||
return potentialConfigurationFile.isAccessible() | ||
} | ||
return proposedDirectory?.appending(subpath: defaultConfigurationFileName) | ||
var depth: Int { | ||
URL(fileURLWithPath: "\(self)").pathComponents.count | ||
} | ||
|
||
/// Safe way to check if the file is accessible from within the current process sandbox. | ||
private func isAccessible() -> Bool { | ||
let result = string.withCString { pointer in | ||
access(pointer, R_OK) | ||
func isDescendant(of path: Path) -> Bool { | ||
"\(self)".hasPrefix("\(path)") | ||
} | ||
|
||
func resolveWorkingDirectory(in directory: Path) throws -> Path { | ||
guard "\(self)".hasPrefix("\(directory)") else { | ||
throw SwiftLintPluginError.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 result == 0 | ||
return path ?? directory | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import PackagePlugin | ||
|
||
enum SwiftLintPluginError: Error, CustomStringConvertible { | ||
case pathNotInDirectory(path: Path, directory: Path) | ||
case swiftFilesNotInProjectDirectory(Path) | ||
case swiftFilesNotInWorkingDirectory(Path) | ||
|
||
var description: String { | ||
switch self { | ||
case let .pathNotInDirectory(path, directory): | ||
"Path '\(path)' is not in directory '\(directory)'." | ||
case let .swiftFilesNotInProjectDirectory(directory): | ||
"Swift files are not in project directory '\(directory)'." | ||
case let .swiftFilesNotInWorkingDirectory(directory): | ||
"Swift files are not in working directory '\(directory)'." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters