Skip to content

Commit e1577ac

Browse files
authored
Merge pull request #43 from JohnSundell/process-inject
Enable a custom Process instance to be injected
2 parents 4ebf258 + f3acd98 commit e1577ac

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.build
33
/Packages
44
/*.xcodeproj
5+
.swiftpm

Sources/ShellOut.swift

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Dispatch
1515
* - parameter command: The command to run
1616
* - parameter arguments: The arguments to pass to the command
1717
* - parameter path: The path to execute the commands at (defaults to current folder)
18+
* - parameter process: Which process to use to perform the command (default: A new one)
1819
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
1920
* (at the moment this is only supported on macOS)
2021
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
@@ -26,21 +27,29 @@ import Dispatch
2627
* Use this function to "shell out" in a Swift script or command line tool
2728
* For example: `shellOut(to: "mkdir", arguments: ["NewFolder"], at: "~/CurrentFolder")`
2829
*/
29-
@discardableResult public func shellOut(to command: String,
30-
arguments: [String] = [],
31-
at path: String = ".",
32-
outputHandle: FileHandle? = nil,
33-
errorHandle: FileHandle? = nil) throws -> String {
34-
let process = Process()
30+
@discardableResult public func shellOut(
31+
to command: String,
32+
arguments: [String] = [],
33+
at path: String = ".",
34+
process: Process = .init(),
35+
outputHandle: FileHandle? = nil,
36+
errorHandle: FileHandle? = nil
37+
) throws -> String {
3538
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"
36-
return try process.launchBash(with: command, outputHandle: outputHandle, errorHandle: errorHandle)
39+
40+
return try process.launchBash(
41+
with: command,
42+
outputHandle: outputHandle,
43+
errorHandle: errorHandle
44+
)
3745
}
3846

3947
/**
4048
* Run a series of shell commands using Bash
4149
*
4250
* - parameter commands: The commands to run
4351
* - parameter path: The path to execute the commands at (defaults to current folder)
52+
* - parameter process: Which process to use to perform the command (default: A new one)
4453
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
4554
* (at the moment this is only supported on macOS)
4655
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
@@ -52,19 +61,30 @@ import Dispatch
5261
* Use this function to "shell out" in a Swift script or command line tool
5362
* For example: `shellOut(to: ["mkdir NewFolder", "cd NewFolder"], at: "~/CurrentFolder")`
5463
*/
55-
@discardableResult public func shellOut(to commands: [String],
56-
at path: String = ".",
57-
outputHandle: FileHandle? = nil,
58-
errorHandle: FileHandle? = nil) throws -> String {
64+
@discardableResult public func shellOut(
65+
to commands: [String],
66+
at path: String = ".",
67+
process: Process = .init(),
68+
outputHandle: FileHandle? = nil,
69+
errorHandle: FileHandle? = nil
70+
) throws -> String {
5971
let command = commands.joined(separator: " && ")
60-
return try shellOut(to: command, at: path, outputHandle: outputHandle, errorHandle: errorHandle)
72+
73+
return try shellOut(
74+
to: command,
75+
at: path,
76+
process: process,
77+
outputHandle: outputHandle,
78+
errorHandle: errorHandle
79+
)
6180
}
6281

6382
/**
6483
* Run a pre-defined shell command using Bash
6584
*
6685
* - parameter command: The command to run
6786
* - parameter path: The path to execute the commands at (defaults to current folder)
87+
* - parameter process: Which process to use to perform the command (default: A new one)
6888
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
6989
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
7090
*
@@ -76,11 +96,20 @@ import Dispatch
7696
*
7797
* See `ShellOutCommand` for more info.
7898
*/
79-
@discardableResult public func shellOut(to command: ShellOutCommand,
80-
at path: String = ".",
81-
outputHandle: FileHandle? = nil,
82-
errorHandle: FileHandle? = nil) throws -> String {
83-
return try shellOut(to: command.string, at: path, outputHandle: outputHandle, errorHandle: errorHandle)
99+
@discardableResult public func shellOut(
100+
to command: ShellOutCommand,
101+
at path: String = ".",
102+
process: Process = .init(),
103+
outputHandle: FileHandle? = nil,
104+
errorHandle: FileHandle? = nil
105+
) throws -> String {
106+
return try shellOut(
107+
to: command.string,
108+
at: path,
109+
process: process,
110+
outputHandle: outputHandle,
111+
errorHandle: errorHandle
112+
)
84113
}
85114

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

0 commit comments

Comments
 (0)