From 8c227b8faef24ddb2a1d8a83830d6963dfe10af8 Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Sun, 20 Sep 2015 20:13:26 +0200 Subject: [PATCH] Let run* commands take shell arguments of any type. --- SwiftShell/Command.swift | 90 +++-------------------- SwiftShell/General/Array.swift | 5 +- SwiftShellTests/Command_Tests.swift | 3 +- SwiftShellTests/General/Array_Tests.swift | 15 +++- 4 files changed, 31 insertions(+), 82 deletions(-) diff --git a/SwiftShell/Command.swift b/SwiftShell/Command.swift index 29bf748..6a979ac 100644 --- a/SwiftShell/Command.swift +++ b/SwiftShell/Command.swift @@ -67,19 +67,9 @@ extension ShellContextType { - parameter args: the arguments, one string for each. - returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line. */ - public func run (executable: String, _ args: String ...) -> String { - return run(executable, args) - } - - /** - Shortcut for shell command, returns output and errors as a String. - - - parameter executable: path to an executable file. - - parameter args: array of the arguments, one string for each. - - returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line. - */ - public func run (executable: String, _ args: [String]) -> String { - return outputFromRun(setupTask(executable, args: args)) + public func run (executable: String, _ args: Any ...) -> String { + let stringargs = args.flatten().map { String($0) } + return outputFromRun(setupTask(executable, args: stringargs)) } } @@ -153,19 +143,9 @@ extension ShellContextType { - parameter args: arguments to the executable. - returns: an AsyncShellTask with standard output, standard error and a 'finish' function. */ - public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask { - return runAsync(executable, args) - } - - /** - Run executable and return before it is finished. - - - parameter executable: path to an executable file. - - parameter args: array of arguments to the executable. - - returns: an AsyncShellTask with standard output, standard error and a 'finish' function. - */ - public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask { - return AsyncShellTask(task: setupTask(executable, args: args)) + public func runAsync (executable: String, _ args: Any ...) -> AsyncShellTask { + let stringargs = args.flatten().map { String($0) } + return AsyncShellTask(task: setupTask(executable, args: stringargs)) } } @@ -181,25 +161,14 @@ extension ShellContextType { - parameter args: arguments to the executable. - throws: a ShellError if the return code is anything but 0. */ - public func runAndPrint (executable: String, _ args: String ...) throws { - return try runAndPrint(executable, args) - } - - /** - Run executable and print output and errors. - - - parameter executable: path to an executable file. - - parameter args: array of arguments to the executable. - - throws: a ShellError if the return code is anything but 0. - */ - public func runAndPrint (executable: String, _ args: [String]) throws { - let task = setupTask(executable, args: args) + public func runAndPrint (executable: String, _ args: Any ...) throws { + let stringargs = args.flatten().map { String($0) } + let task = setupTask(executable, args: stringargs) task.launch() try task.finish() } } - // MARK: Global functions /** @@ -209,22 +178,10 @@ Shortcut for shell command, returns output and errors as a String. - parameter args: the arguments, one string for each. - returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line. */ -public func run (executable: String, _ args: String ...) -> String { +public func run (executable: String, _ args: Any ...) -> String { return main.run(executable, args) } -/** -Shortcut for shell command, returns output and errors as a String. - -- parameter executable: path to an executable file. -- parameter args: array of the arguments, one string for each. -- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line. -*/ -public func run (executable: String, _ args: [String]) -> String { - return main.run(executable, args) -} - - /** Run executable and return before it is finished. @@ -232,22 +189,10 @@ Run executable and return before it is finished. - parameter args: arguments to the executable. - returns: an AsyncShellTask with standard output, standard error and a 'finish' function. */ -public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask { +public func runAsync (executable: String, _ args: Any ...) -> AsyncShellTask { return main.runAsync(executable, args) } -/** -Run executable and return before it is finished. - -- parameter executable: path to an executable file. -- parameter args: array of arguments to the executable. -- returns: an AsyncShellTask with standard output, standard error and a 'finish' function. -*/ -public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask { - return main.runAsync(executable, args) -} - - /** Run executable and print output and errors. @@ -255,17 +200,6 @@ Run executable and print output and errors. - parameter args: arguments to the executable. - throws: a ShellError if the return code is anything but 0. */ -public func runAndPrint (executable: String, _ args: String ...) throws { - return try main.runAndPrint(executable, args) -} - -/** -Run executable and print output and errors. - -- parameter executable: path to an executable file. -- parameter args: array of arguments to the executable. -- throws: a ShellError if the return code is anything but 0. -*/ -public func runAndPrint (executable: String, _ args: [String]) throws { +public func runAndPrint (executable: String, _ args: Any ...) throws { return try main.runAndPrint(executable, args) } diff --git a/SwiftShell/General/Array.swift b/SwiftShell/General/Array.swift index ada7f0e..945e423 100644 --- a/SwiftShell/General/Array.swift +++ b/SwiftShell/General/Array.swift @@ -10,17 +10,18 @@ private protocol AnyArrayType { } extension Array: AnyArrayType { - var anyValues: [Any] { + private var anyValues: [Any] { return self.map { $0 as Any } } } public extension Array where Element: Any { + func flatten () -> [Any] { let result: [Any] = self.flatMap { x -> [Any] in switch x { case let anyarray as AnyArrayType: - return anyarray.anyValues + return anyarray.anyValues.flatten() default: return [x] } diff --git a/SwiftShellTests/Command_Tests.swift b/SwiftShellTests/Command_Tests.swift index e5ff2ef..9d61a45 100644 --- a/SwiftShellTests/Command_Tests.swift +++ b/SwiftShellTests/Command_Tests.swift @@ -16,7 +16,8 @@ class Run_Tests: XCTestCase { } func testArgumentsFromArray () { - XCTAssertEqual( SwiftShell.run("/bin/echo", ["one", "two"]), "one two" ) + let stringarray = ["one", "two"] + XCTAssertEqual( SwiftShell.run("/bin/echo", stringarray), "one two" ) } func testSinglelineOutput () { diff --git a/SwiftShellTests/General/Array_Tests.swift b/SwiftShellTests/General/Array_Tests.swift index 2aa54e1..5086b5d 100644 --- a/SwiftShellTests/General/Array_Tests.swift +++ b/SwiftShellTests/General/Array_Tests.swift @@ -7,7 +7,6 @@ // import XCTest - @testable import SwiftShell class Array_Tests: XCTestCase { @@ -16,9 +15,23 @@ class Array_Tests: XCTestCase { func testAnyArrayFlattenAFlatOne () { XCTAssertEqual( ([1,"2"] as [Any]).flatten().description, "[1, \"2\"]") + XCTAssertEqual( (["1","2"] as [Any]).flatten().description, ["1", "2"].description) + XCTAssertEqual( ([1,2] as [Any]).flatten().description, [1,2].description) } func testAnyArrayFlattenABumpyOne () { + let intarray = [1,2] + XCTAssertEqual( ([intarray] as [Any]).flatten().description, [1,2].description) XCTAssertEqual( (["1",[2,3]] as [Any]).flatten().description, "[\"1\", 2, 3]") + + let stringarray = ["one", "two"] + XCTAssertEqual( ([stringarray, "three"] as [Any]).flatten().description, ["one","two","three"].description) + XCTAssertEqual( ([stringarray, 3] as [Any]).flatten().description, (["one","two",3] as [Any]).description) + } + + func testAnyArrayFlattenAVeryBumpyOne () { + let intarray = [1,2] + XCTAssertEqual( ([[intarray]] as [Any]).flatten().description, intarray.description) + XCTAssertEqual( (["1",[2,[3] as Any]] as [Any]).flatten().description, "[\"1\", 2, 3]") } }