-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 60a45bf
Showing
19 changed files
with
876 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: "Test" | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: macos-latest | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }} | ||
steps: | ||
- name: Chackout | ||
uses: actions/checkout@v2 | ||
- name: Cache dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
.build/artifacts | ||
.build/checkouts | ||
.build/repositories | ||
key: ${{ runner.os }}-dependencies-${{ matrix.xcode }}-${{ hashFiles('**/Package.resolved') }} | ||
restore-keys: | | ||
${{ runner.os }}-dependencies-${{ matrix.xcode }}-${{ hashFiles('**/Package.resolved') }} | ||
${{ runner.os }}-dependencies-${{ matrix.xcode }}- | ||
- name: Test | ||
run: swift test | ||
|
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,7 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata |
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,19 @@ | ||
Copyright (c) 2022 Yutaka Tajika | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | ||
OR OTHER DEALINGS IN THE SOFTWARE. |
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,52 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "Logger", | ||
"repositoryURL": "https://github.com/shibapm/Logger", | ||
"state": { | ||
"branch": null, | ||
"revision": "53c3ecca5abe8cf46697e33901ee774236d94cce", | ||
"version": "0.2.3" | ||
} | ||
}, | ||
{ | ||
"package": "OctoKit", | ||
"repositoryURL": "https://github.com/nerdishbynature/octokit.swift", | ||
"state": { | ||
"branch": null, | ||
"revision": "9521cdff919053868ab13cd08a228f7bc1bde2a9", | ||
"version": "0.11.0" | ||
} | ||
}, | ||
{ | ||
"package": "RequestKit", | ||
"repositoryURL": "https://github.com/nerdishbynature/RequestKit.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "fd5e9e99aada7432170366c9e95967011ce13bad", | ||
"version": "2.4.0" | ||
} | ||
}, | ||
{ | ||
"package": "danger-swift", | ||
"repositoryURL": "https://github.com/danger/swift.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "c1bba33f705ca0fd4a0022997c8b696210083755", | ||
"version": "3.12.3" | ||
} | ||
}, | ||
{ | ||
"package": "Version", | ||
"repositoryURL": "https://github.com/mxcl/Version", | ||
"state": { | ||
"branch": null, | ||
"revision": "200046c93f6d5d78a6d72bfd9c0b27a95e9c0a2b", | ||
"version": "1.2.0" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
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,26 @@ | ||
// swift-tools-version:5.5 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "DangerSwiftPeriphery", | ||
products: [ | ||
.library( | ||
name: "DangerSwiftPeriphery", | ||
targets: ["DangerSwiftPeriphery"]), | ||
], | ||
dependencies: [ | ||
.package(name: "danger-swift", url: "https://github.com/danger/swift.git", from: "3.0.0") | ||
], | ||
targets: [ | ||
.target( | ||
name: "DangerSwiftPeriphery", | ||
dependencies: [ | ||
.product(name: "Danger", package: "danger-swift") | ||
]), | ||
.testTarget( | ||
name: "DangerSwiftPeripheryTests", | ||
dependencies: ["DangerSwiftPeriphery"]), | ||
] | ||
) |
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,82 @@ | ||
# DangerSwiftPeriphery | ||
|
||
[Danger Swift](https://github.com/danger/swift) plugin to run [Periphery](https://github.com/peripheryapp/periphery) on CI. | ||
|
||
|
||
![Test](https://github.com/taji-taji/DangerSwiftPeriphery/actions/workflows/test.yml/badge.svg) | ||
[![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](https://github.com/tterb/atomic-design-ui/blob/master/LICENSEs) | ||
|
||
## Features | ||
|
||
This plugin will comment unreferenced code detected by periphery via Danger Swift. | ||
|
||
![Image](Resources/Images/screenshot.png) | ||
|
||
|
||
## Usage | ||
|
||
### Preparation | ||
|
||
- Install [Periphery](https://github.com/peripheryapp/periphery) on the machine you want to run Danger Swift on beforehand. | ||
- [Danger Swift](https://github.com/danger/swift) Setup. | ||
|
||
### Package.swift | ||
|
||
```swift | ||
let package = Package( | ||
// ... | ||
dependencies: [ | ||
// Danger | ||
.package(name: "danger-swift", url: "https://github.com/danger/swift.git", from: "3.0.0"), // dev | ||
// Danger Plugins | ||
// Add the line below. | ||
.package(name: "DangerSwiftPeriphery", url: "https://github.com/taji-taji/DangerSwiftPeriphery.git", from: "1.0.0"), // dev | ||
], | ||
targets: [ | ||
// ... | ||
// Add DangerSwiftPeriphery to dependencies in DangerDependencies. | ||
.target(name: "DangerDependencies", | ||
dependencies: [ | ||
.product(name: "Danger", package: "danger-swift"), | ||
"DangerSwiftPeriphery", | ||
]), | ||
// ... | ||
] | ||
) | ||
``` | ||
|
||
|
||
### Dangerfile.swift | ||
|
||
If you have a `.periphery.yml` file, simply include the following in `Dangerfile.swift` | ||
|
||
```swift | ||
import Danger | ||
import DangerSwiftPeriphery | ||
|
||
DangerPeriphery.scan() | ||
``` | ||
|
||
Alternatively, periphery options can be passed as arguments. | ||
|
||
```swift | ||
import Danger | ||
import DangerSwiftPeriphery | ||
|
||
DangerPeriphery.scan(arguments: [ | ||
"--workspace MaApp.xcworkspace", | ||
"--schemes MyApp", | ||
"--index-store-path /path/to/index/store", | ||
"--skip-build" | ||
]) | ||
``` | ||
|
||
You may also specify the location of periphery binaries. | ||
|
||
```swift | ||
import DangerSwiftPeriphery | ||
|
||
DangerPeriphery.scan(peripheryPath: "/path/to/periphery") | ||
``` | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,47 @@ | ||
// | ||
// CheckstyleOutputParser.swift | ||
// | ||
// | ||
// Created by 多鹿豊 on 2022/04/03. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol CheckstyleOutputParsable { | ||
func parse(xml: String, projectRootPath: String) throws -> [Violation] | ||
} | ||
|
||
struct CheckstyleOutputParser: CheckstyleOutputParsable { | ||
func parse(xml: String, projectRootPath: String) throws -> [Violation] { | ||
let xmlDocument = try XMLDocument(xmlString: xml, options: []) | ||
guard let rootElement = xmlDocument.rootElement(), | ||
rootElement.name == "checkstyle" else { | ||
throw Error.invalidCheckstyleXML | ||
} | ||
let fileElements = (try? rootElement.nodes(forXPath: "file") as? [XMLElement]) ?? [] | ||
return fileElements.reduce([], { warnings, fileElement -> [Violation] in | ||
guard let location = fileElement.attribute(forName: "name")?.stringValue else { return warnings } | ||
let filePath = location.deletingPrefix(projectRootPath).deletingPrefix("/") | ||
let errorElements = (try? fileElement.nodes(forXPath: "error") as? [XMLElement]) ?? [] | ||
return warnings + errorElements.compactMap({ errorElement -> Violation? in | ||
guard let lineString = errorElement.attribute(forName: "line")?.stringValue, | ||
let line = Int(lineString), | ||
let message = errorElement.attribute(forName: "message")?.stringValue else { return nil } | ||
return Violation(filePath: filePath, line: line, message: message) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
extension CheckstyleOutputParser { | ||
enum Error: Swift.Error { | ||
case invalidCheckstyleXML | ||
} | ||
} | ||
|
||
private extension String { | ||
func deletingPrefix(_ prefix: String) -> String { | ||
guard hasPrefix(prefix) else { return self } | ||
return String(dropFirst(prefix.count)) | ||
} | ||
} |
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,28 @@ | ||
// | ||
// CurrentPathProvider.swift | ||
// | ||
// Created by 多鹿豊 on 2022/04/05. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol CurrentPathProvider { | ||
var currentPath: String { get } | ||
} | ||
|
||
struct DefaultCurrentPathProvider<SE: ShellExecutable>: CurrentPathProvider { | ||
private let shellExecutor: SE | ||
var currentPath: String { | ||
return try! shellExecutor.execute("pwd").get().trimmingCharacters(in: .newlines) | ||
} | ||
|
||
init(shellExecutor: SE) { | ||
self.shellExecutor = shellExecutor | ||
} | ||
} | ||
|
||
extension DefaultCurrentPathProvider where SE == ShellExecutor { | ||
init() { | ||
self.shellExecutor = .init() | ||
} | ||
} |
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,78 @@ | ||
// | ||
// DangerPeriphery.swift | ||
// | ||
// | ||
// Created by 多鹿豊 on 2022/03/31. | ||
// | ||
|
||
import Danger | ||
|
||
public struct DangerPeriphery { | ||
public static func scan(peripheryPath: String = "periphery", | ||
arguments: [String] = []) { | ||
// make dependencies | ||
let commandBuilder = PeripheryScanCommandBuilder(peripheryPath: peripheryPath, | ||
additionalArguments: arguments) | ||
let scanExecutor = PeripheryScanExecutor(commandBuilder: commandBuilder) | ||
let diffProvider = PullRequestDiffProvider(dangerDSL: Danger()) | ||
|
||
// execute scan | ||
let result = self.scan(scanExecutor: scanExecutor, | ||
currentPathProvider: DefaultCurrentPathProvider(), | ||
outputParser: CheckstyleOutputParser(), | ||
diffProvider: diffProvider) | ||
|
||
// handle scan result | ||
switch result { | ||
case .success(let violations): | ||
for violation in violations { | ||
warn(message: violation.message, | ||
file: violation.filePath, | ||
line: violation.line) | ||
} | ||
case .failure(let error): | ||
fail(error.localizedDescription) | ||
} | ||
} | ||
|
||
static func scan<PSE: PeripheryScanExecutable, | ||
CPP: CurrentPathProvider, | ||
OP: CheckstyleOutputParsable, | ||
DP: PullRequestDiffProvidable>( | ||
scanExecutor: PSE, | ||
currentPathProvider: CPP, | ||
outputParser: OP, | ||
diffProvider: DP) -> Result<[Violation], Error> { | ||
do { | ||
let output = try scanExecutor.execute() | ||
let allViolations = try outputParser.parse(xml: output, | ||
projectRootPath: currentPathProvider.currentPath) | ||
let violationsForComment = allViolations.filter({ violation -> Bool in | ||
let result = diffProvider.diff(forFile: violation.filePath) | ||
guard let changes = try? result.get() else { | ||
return false | ||
} | ||
// comment only `Created files` and `Files that have been modified and are contained within hunk` | ||
switch changes { | ||
case .created: | ||
return true | ||
case .deleted: | ||
return false | ||
case let .modified(hunks): | ||
return hunks.contains(where: { | ||
let lineRange = ($0.newLineStart ..< $0.newLineSpan - $0.newLineStart) | ||
if lineRange.contains(violation.line) { | ||
return true | ||
} | ||
return false | ||
}) | ||
case .renamed: | ||
return false | ||
} | ||
}) | ||
return .success(violationsForComment) | ||
} catch { | ||
return .failure(error) | ||
} | ||
} | ||
} |
Oops, something went wrong.