Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed May 27, 2018
0 parents commit 6bab7c5
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
79 changes: 79 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"object": {
"pins": [
{
"package": "danger-swift",
"repositoryURL": "https://github.com/danger/danger-swift.git",
"state": {
"branch": null,
"revision": "dd0d6457adf72eedeb10076e1887d49431fd56a6",
"version": "0.4.0"
}
},
{
"package": "Files",
"repositoryURL": "https://github.com/JohnSundell/Files.git",
"state": {
"branch": null,
"revision": "06f95bd1bf4f8d5f50bc6bb30b808c253acd4c88",
"version": "2.2.1"
}
},
{
"package": "Marathon",
"repositoryURL": "https://github.com/JohnSundell/Marathon.git",
"state": {
"branch": null,
"revision": "959e82b044f7dbb2680c6e7cdaa80d70e9dfa4ca",
"version": "3.0.0"
}
},
{
"package": "Releases",
"repositoryURL": "https://github.com/JohnSundell/Releases.git",
"state": {
"branch": null,
"revision": "e74f0895855b56147cb96f2d45aba3ec37da64fb",
"version": "3.0.0"
}
},
{
"package": "Require",
"repositoryURL": "https://github.com/JohnSundell/Require.git",
"state": {
"branch": null,
"revision": "7cfbd0d8a2dede0e01f6f0d8ab2c7acef1df112e",
"version": "2.0.1"
}
},
{
"package": "ShellOut",
"repositoryURL": "https://github.com/JohnSundell/ShellOut.git",
"state": {
"branch": null,
"revision": "f1c253a34a40df4bfd268b09fdb101b059f6d52d",
"version": "2.1.0"
}
},
{
"package": "Unbox",
"repositoryURL": "https://github.com/JohnSundell/Unbox.git",
"state": {
"branch": null,
"revision": "17ad6aa1cc2f1efa27a0bbbdb66f79796708aa95",
"version": "2.5.0"
}
},
{
"package": "Wrap",
"repositoryURL": "https://github.com/JohnSundell/Wrap.git",
"state": {
"branch": null,
"revision": "8085c925060b84a1fc0caef444c130da2c8154c3",
"version": "3.0.1"
}
}
]
},
"version": 1
}
23 changes: 23 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "DangerIBLinter",
products: [
.library(
name: "DangerIBLinter",
targets: ["DangerIBLinter"]),
],
dependencies: [
.package(url: "https://github.com/danger/danger-swift.git", from: "0.3.0")
],
targets: [
.target(
name: "DangerIBLinter",
dependencies: ["Danger"]),
.testTarget(
name: "DangerIBLinterTests",
dependencies: ["DangerIBLinter"]),
]
)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DangerIBLinter

A description of this package.
42 changes: 42 additions & 0 deletions Sources/DangerIBLinter/DangerIBLinter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation
import Danger

public struct IBLinter {
static let danger = Danger()
}

extension IBLinter {

public static func lint(inline: Bool = true) throws {
let arguments = [
"--reporter", "json"
]
let shellExecutor = ShellExecutor()
let output = shellExecutor.execute("iblinter", arguments: arguments)
let pwd = shellExecutor.execute("pwd")
let decoder = JSONDecoder()
let files = danger.git.createdFiles + danger.git.modifiedFiles
let violations = try decoder.decode([Violation<AbsolutePath>].self, from: output.data(using: .utf8)!)
let filteredViolations = violations.map { $0.convert(gitRoot: pwd) }
.filter {
files.contains($0.relativePath)
}

if inline {
filteredViolations
.forEach {
fail(message: $0.message, file: $0.relativePath, line: 0)
}
} else {
let header = """
### IBLinter found issues
| Severity | File | Reason |
| -------- | ---- | ------ |\n
"""
let markdownMessage = filteredViolations.reduce(header) {
$0 + $1.toMarkdown() + "\n"
}
markdown(markdownMessage)
}
}
}
29 changes: 29 additions & 0 deletions Sources/DangerIBLinter/ShellExecutor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ShellExecutor.swift
// DangerIBLinter
//
// Created by SaitoYuta on 2018/05/26.
//

import Foundation

internal class ShellExecutor {
func execute(_ command: String, arguments: [String] = []) -> String {
let script = "\(command) \(arguments.joined(separator: " "))"
print("Executing \(script)")

var env = ProcessInfo.processInfo.environment
let task = Process()
task.launchPath = env["SHELL"]
task.arguments = ["-l", "-c", script]
task.currentDirectoryPath = FileManager.default.currentDirectoryPath

let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: String.Encoding.utf8)!
}
}
44 changes: 44 additions & 0 deletions Sources/DangerIBLinter/Violation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Violation.swift
// DangerIBLinter
//
// Created by SaitoYuta on 2018/05/26.
//

import Foundation

enum AbsolutePath {}
enum RelativePath {}

enum Level: String, Codable {
case warning
case error
}

struct Violation<PathType>: Codable {
let message: String
let level: Level
private let path: String
}

extension Violation where PathType == AbsolutePath {
var absolutePath: String { return path }

func convert(gitRoot: String) -> Violation<RelativePath> {
guard let range = path.range(of: gitRoot)else {
fatalError()
}
var relativePath = path
relativePath.removeSubrange(range)
return .init(message: message, level: level, path: relativePath)
}
}

extension Violation where PathType == RelativePath {
var relativePath: String { return path }

func toMarkdown() -> String {
let formattedFile = path.split(separator: "/").last! + ":0"
return "\(level.rawValue) | \(formattedFile) | \(message) |"
}
}
16 changes: 16 additions & 0 deletions Tests/DangerIBLinterTests/DangerIBLinterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest
@testable import DangerIBLinter

final class DangerIBLinterTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(DangerIBLinter().text, "Hello, World!")
}


static var allTests = [
("testExample", testExample),
]
}
9 changes: 9 additions & 0 deletions Tests/DangerIBLinterTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import XCTest

#if !os(macOS)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(DangerIBLinterTests.allTests),
]
}
#endif
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

import DangerIBLinterTests

var tests = [XCTestCaseEntry]()
tests += DangerIBLinterTests.allTests()
XCTMain(tests)

0 comments on commit 6bab7c5

Please sign in to comment.