Skip to content

Commit

Permalink
Merge pull request #43 from JohnSundell/process-inject
Browse files Browse the repository at this point in the history
Enable a custom Process instance to be injected
  • Loading branch information
JohnSundell committed Jan 7, 2020
2 parents 4ebf258 + f3acd98 commit e1577ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
.swiftpm
63 changes: 46 additions & 17 deletions Sources/ShellOut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Dispatch
* - parameter command: The command to run
* - parameter arguments: The arguments to pass to the command
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter process: Which process to use to perform the command (default: A new one)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
Expand All @@ -26,21 +27,29 @@ import Dispatch
* Use this function to "shell out" in a Swift script or command line tool
* For example: `shellOut(to: "mkdir", arguments: ["NewFolder"], at: "~/CurrentFolder")`
*/
@discardableResult public func shellOut(to command: String,
arguments: [String] = [],
at path: String = ".",
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
let process = Process()
@discardableResult public func shellOut(
to command: String,
arguments: [String] = [],
at path: String = ".",
process: Process = .init(),
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil
) throws -> String {
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"
return try process.launchBash(with: command, outputHandle: outputHandle, errorHandle: errorHandle)

return try process.launchBash(
with: command,
outputHandle: outputHandle,
errorHandle: errorHandle
)
}

/**
* Run a series of shell commands using Bash
*
* - parameter commands: The commands to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter process: Which process to use to perform the command (default: A new one)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
Expand All @@ -52,19 +61,30 @@ import Dispatch
* Use this function to "shell out" in a Swift script or command line tool
* For example: `shellOut(to: ["mkdir NewFolder", "cd NewFolder"], at: "~/CurrentFolder")`
*/
@discardableResult public func shellOut(to commands: [String],
at path: String = ".",
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
@discardableResult public func shellOut(
to commands: [String],
at path: String = ".",
process: Process = .init(),
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil
) throws -> String {
let command = commands.joined(separator: " && ")
return try shellOut(to: command, at: path, outputHandle: outputHandle, errorHandle: errorHandle)

return try shellOut(
to: command,
at: path,
process: process,
outputHandle: outputHandle,
errorHandle: errorHandle
)
}

/**
* Run a pre-defined shell command using Bash
*
* - parameter command: The command to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter process: Which process to use to perform the command (default: A new one)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
*
Expand All @@ -76,11 +96,20 @@ import Dispatch
*
* See `ShellOutCommand` for more info.
*/
@discardableResult public func shellOut(to command: ShellOutCommand,
at path: String = ".",
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
return try shellOut(to: command.string, at: path, outputHandle: outputHandle, errorHandle: errorHandle)
@discardableResult public func shellOut(
to command: ShellOutCommand,
at path: String = ".",
process: Process = .init(),
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil
) throws -> String {
return try shellOut(
to: command.string,
at: path,
process: process,
outputHandle: outputHandle,
errorHandle: errorHandle
)
}

/// Structure used to pre-define commands for use with ShellOut
Expand Down

0 comments on commit e1577ac

Please sign in to comment.