Skip to content

Commit

Permalink
use taji-taji/swift-shell
Browse files Browse the repository at this point in the history
  • Loading branch information
taji-taji committed Sep 3, 2022
1 parent d1b639e commit 5b34841
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 56 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
"version": "0.1.5"
}
},
{
"package": "SwiftShell",
"repositoryURL": "https://github.com/taji-taji/swift-shell.git",
"state": {
"branch": null,
"revision": "027bf36be072c802e392570602bd1442e6dd77d0",
"version": "1.0.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/peripheryapp/swift-syntax",
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ let package = Package(
] + developProducts,
dependencies: [
.package(url: "https://github.com/danger/swift.git", from: "3.0.0"),
.package(url: "https://github.com/peripheryapp/periphery", from: "2.0.0")
.package(url: "https://github.com/peripheryapp/periphery", from: "2.0.0"),
.package(url: "https://github.com/taji-taji/swift-shell.git", from: "1.0.0"),
],
targets: [
.target(
name: "DangerSwiftPeriphery",
dependencies: [
.product(name: "Danger", package: "swift"),
.product(name: "periphery", package: "periphery"),
.product(name: "SwiftShell", package: "swift-shell"),
]),
] + developTargets
)
10 changes: 2 additions & 8 deletions Sources/DangerSwiftPeriphery/PeripheryScanExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//
// PeripheryScanExecutor.swift
//
//
// Created by 多鹿豊 on 2022/04/09.
//

import Foundation
import SwiftShell

protocol PeripheryScanExecutable {
func execute() throws -> String
Expand Down Expand Up @@ -35,7 +29,7 @@ struct PeripheryScanExecutor<SE: ShellExecutable>: PeripheryScanExecutable {
}
}

extension PeripheryScanExecutor where SE == ShellExecutor {
extension PeripheryScanExecutor where SE == Shell {
init(commandBuilder: PeripheryScanCommandBuilder) {
self.commandBuilder = commandBuilder
shellExecutor = .init()
Expand Down
53 changes: 13 additions & 40 deletions Sources/DangerSwiftPeriphery/ShellExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//
// ShellExecutor.swift
//
//
// Created by 多鹿豊 on 2022/03/31.
//

import Foundation
import SwiftShell

protocol ShellExecutable {
func execute(_ command: String) -> Result<String, CommandError>
Expand All @@ -18,44 +12,23 @@ extension ShellExecutable {
}
}

struct CommandError: Error, CustomStringConvertible {
let status: Int32
let description: String
enum CommandError: Error {
case unknown(error: Error)
case standard(exitCode: Int32, description: String)
}

struct ShellExecutor: ShellExecutable {
extension Shell: ShellExecutable {
func execute(_ command: String, arguments: [String] = []) -> Result<String, CommandError> {
let script = "\(command) \(arguments.joined(separator: " "))"
Logger.shared.debug("command started: \(script)")

let env = ProcessInfo.processInfo.environment
let task = Process()

#if os(macOS)
task.launchPath = env["SHELL"]
#else
task.launchPath = "/bin/sh"
#endif

task.arguments = ["-l", "-c", script]
task.currentDirectoryPath = FileManager.default.currentDirectoryPath

let outputPipe = Pipe()
let errorPipe = Pipe()
task.standardOutput = outputPipe
task.standardError = errorPipe
task.launch()
task.waitUntilExit()

let outputMessage = String(data: outputPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
let errorMessage = String(data: errorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)

let status = task.terminationStatus
if status == 0 {
Logger.shared.debug("command output: " + outputMessage!)
return .success(outputMessage!)
} else {
return .failure(.init(status: status, description: errorMessage!))
do {
let output = try callAsFunction(command, arguments: arguments)
Logger.shared.debug("command output: " + output)
return .success(output)
} catch let error as ShellError {
return .failure(.standard(exitCode: error.exitStatus, description: error.description))
} catch {
return .failure(.unknown(error: error))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import XCTest
import SwiftShell
@testable import DangerSwiftPeriphery

final class ShellExecutorTests: XCTestCase {
private var shellExecutor: ShellExecutor!
final class PeripheryExecutionTests: XCTestCase {
private var shellExecutor: Shell!

override func setUpWithError() throws {
try super.setUpWithError()

shellExecutor = ShellExecutor()
shellExecutor = .init()
}

func testExecutePeriphery() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class PeripheryScanExecutorTests: XCTestCase {
func testExecuteThrowCommandError() throws {
let shellExecutor = ShellExecutableMock()
shellExecutor.executeHandler = { _, _ in
.failure(.init(status: 9999, description: "test error"))
.failure(.standard(exitCode: 9999, description: "test error"))
}
executor = PeripheryScanExecutor(commandBuilder: commandBuilder,
shellExecutor: shellExecutor)
Expand All @@ -32,9 +32,9 @@ final class PeripheryScanExecutorTests: XCTestCase {
_ = try executor.execute()
XCTFail("Must throw error.")
} catch {
if let error = error as? CommandError {
XCTAssertEqual(error.status, 9999)
XCTAssertEqual(error.description, "test error")
if case let .standard(exitCode, description) = error as? CommandError {
XCTAssertEqual(exitCode, 9999)
XCTAssertEqual(description, "test error")
} else {
XCTFail("Unexpected error.")
}
Expand Down

0 comments on commit 5b34841

Please sign in to comment.