Skip to content

Commit

Permalink
fix linting local packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Dec 7, 2023
1 parent 5bb6ec9 commit aa8865a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Plugins/SwiftLintPlugin/PathExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ extension Path {
static let cat = Path("/bin/cat")
static let sh = Path("/bin/sh")

private static let swiftlintConfig = ".swiftlint.yml"

/// 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 firstParentContainingConfigFile() -> Path? {
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
}

return path.removingLastComponent()
}
).first { path in
let potentialConfigurationFile = path.appending(subpath: Self.swiftlintConfig)
return potentialConfigurationFile.isAccessible()
}
return proposedDirectory
}

/// 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)
}

return result == 0
}

/// Get file modification date
var modified: Date {
get throws {
Expand Down
2 changes: 1 addition & 1 deletion Plugins/SwiftLintPlugin/SwiftLintPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct SwiftLintPlugin: BuildToolPlugin {
return try createBuildCommands(
target: target.name,
inputFiles: inputFiles,
packageDirectory: context.package.directory,
packageDirectory: context.package.directory.firstParentContainingConfigFile() ?? context.package.directory,
workingDirectory: context.pluginWorkDirectory,
tool: context.tool(named:)
)
Expand Down

0 comments on commit aa8865a

Please sign in to comment.