Skip to content

Commit

Permalink
Merge pull request #16 from marcelofabri/custom-binary-path
Browse files Browse the repository at this point in the history
Allow setting a custom SwiftLint binary path
  • Loading branch information
ashfurrow authored Nov 21, 2018
2 parents 897a546 + 1950370 commit 9ade161
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Next

- Nothing yet!
- Added `swiftlintPath` option to `SwiftLint.lint()`. This allows providing a custom path if you want to use a specific binary instead of the global one (e.g. when using CocoaPods).

## 0.4.0

- Addded `lintAllFiles` option to `SwiftLint.lint()`. This will lint all existing files (instead of just the added/modified ones). However, nested configurations works when this option is enabled.
- Added `lintAllFiles` option to `SwiftLint.lint()`. This will lint all existing files (instead of just the added/modified ones). However, nested configurations works when this option is enabled.

## 0.3.0

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ However, you can use the `lintAllFiles` option to lint all the files. In that ca
SwiftLint.lint(lintAllFiles: true)
```

### Custom SwiftLint binary path

By default, Danger SwiftLint runs `swiftlint` assuming it's installed globally. However, there're cases where it makes sense to use a different path. One example would be if you've installed SwiftLint using [CocoaPods](https://github.com/CocoaPods/CocoaPods).

To use another binary, you can use the `swiftlintPath` option:

```swift
SwiftLint.lint(swiftlintPath: "Pods/SwiftLint/swiftlint")
```

# Contributing

If you find a bug, please [open an issue](https://github.com/ashfurrow/danger-swiftlint/issues/new)! Or a pull request :wink:
Expand Down
12 changes: 8 additions & 4 deletions Sources/DangerSwiftLint/DangerSwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ public struct SwiftLint {
/// This is the main entry point for linting Swift in PRs using Danger-Swift.
/// Call this function anywhere from within your Dangerfile.swift.
@discardableResult
public static func lint(inline: Bool = false, directory: String? = nil, configFile: String? = nil, lintAllFiles: Bool = false) -> [Violation] {
public static func lint(inline: Bool = false, directory: String? = nil,
configFile: String? = nil, lintAllFiles: Bool = false,
swiftlintPath: String = "swiftlint") -> [Violation] {
// First, for debugging purposes, print the working directory.
print("Working directory: \(shellExecutor.execute("pwd"))")
return self.lint(danger: danger, shellExecutor: shellExecutor, inline: inline, directory: directory, configFile: configFile, lintAllFiles: lintAllFiles)
return self.lint(danger: danger, shellExecutor: shellExecutor, inline: inline, directory: directory,
configFile: configFile, lintAllFiles: lintAllFiles, swiftlintPath: swiftlintPath)
}
}

Expand All @@ -24,6 +27,7 @@ internal extension SwiftLint {
directory: String? = nil,
configFile: String? = nil,
lintAllFiles: Bool = false,
swiftlintPath: String = "swiftlint",
currentPathProvider: CurrentPathProvider = DefaultCurrentPathProvider(),
markdownAction: (String) -> Void = markdown,
failAction: (String) -> Void = fail,
Expand All @@ -40,7 +44,7 @@ internal extension SwiftLint {
if let configFile = configFile {
arguments.append("--config \"\(configFile)\"")
}
let outputJSON = shellExecutor.execute("swiftlint", arguments: arguments)
let outputJSON = shellExecutor.execute(swiftlintPath, arguments: arguments)
violations = makeViolations(from: outputJSON, failAction: failAction)
} else {
var files = danger.git.createdFiles + danger.git.modifiedFiles
Expand All @@ -53,7 +57,7 @@ internal extension SwiftLint {
if let configFile = configFile {
arguments.append("--config \"\(configFile)\"")
}
let outputJSON = shellExecutor.execute("swiftlint", arguments: arguments)
let outputJSON = shellExecutor.execute(swiftlintPath, arguments: arguments)
return makeViolations(from: outputJSON, failAction: failAction)
}
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/DangerSwiftLintTests/DangerSwiftLintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class DangerSwiftLintTests: XCTestCase {
func testExecutesTheShell() {
_ = SwiftLint.lint(danger: danger, shellExecutor: executor, currentPathProvider: fakePathProvider)
XCTAssertNotEqual(executor.invocations.dropFirst().count, 0)
XCTAssertEqual(executor.invocations.first?.command, "swiftlint")
}

func testExecutesTheShellWithCustomSwiftLintPath() {
_ = SwiftLint.lint(danger: danger, shellExecutor: executor, swiftlintPath: "Pods/SwiftLint/swiftlint", currentPathProvider: fakePathProvider)
XCTAssertNotEqual(executor.invocations.dropFirst().count, 0)
XCTAssertEqual(executor.invocations.first?.command, "Pods/SwiftLint/swiftlint")
}

func testExecuteSwiftLintInInlineMode() {
Expand Down

0 comments on commit 9ade161

Please sign in to comment.