Skip to content

Commit

Permalink
Invoke all commands in Bash
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Dec 22, 2024
1 parent 1158c8b commit 5a8d35d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Sources/Shared/PeripheryError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SystemPackage

public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
case shellCommandFailed(cmd: [String], status: Int32, output: String)
case shellOutputEncodingFailed(cmd: String, args: [String], encoding: String.Encoding)
case shellOutputEncodingFailed(cmd: [String], encoding: String.Encoding)
case usageError(String)
case underlyingError(Error)
case invalidScheme(name: String, project: String)
Expand All @@ -25,9 +25,9 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
case let .shellCommandFailed(cmd, status, output):
let joinedCmd = cmd.joined(separator: " ")
return "Shell command '\(joinedCmd)' returned exit status '\(status)':\n\(output)"
case let .shellOutputEncodingFailed(cmd, args, encoding):
let joinedArgs = args.joined(separator: " ")
return "Shell command '\(cmd) \(joinedArgs)' output encoding to \(encoding) failed."
case let .shellOutputEncodingFailed(cmd, encoding):
let joinedCmd = cmd.joined(separator: " ")
return "Shell command '\(joinedCmd)' output encoding to \(encoding) failed."
case let .usageError(message):
return message
case let .underlyingError(error):
Expand Down
34 changes: 7 additions & 27 deletions Sources/Shared/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ public class ShellProcessStore {
}

open class Shell {
private let environment: [String: String]
private let logger: ContextualLogger

public convenience init(logger: Logger) {
self.init(environment: ProcessInfo.processInfo.environment, logger: logger)
}

public required init(environment: [String: String], logger: Logger) {
self.environment = environment
public required init(logger: Logger) {
self.logger = logger.contextualized(with: "shell")
}

Expand Down Expand Up @@ -62,26 +56,14 @@ open class Shell {
// MARK: - Private

private func exec(
_ args: [String],
_ cmd: [String],
captureOutput: Bool = true
) throws -> (Int32, String, String) {
let launchPath: String
let newArgs: [String]

if let cmd = args.first, cmd.hasPrefix("/") {
launchPath = cmd
newArgs = Array(args.dropFirst())
} else {
launchPath = "/usr/bin/env"
newArgs = args
}

let process = Process()
process.launchPath = launchPath
process.environment = environment
process.arguments = newArgs
process.launchPath = "/bin/bash"
process.arguments = ["-c", cmd.joined(separator: " ")]

logger.debug("\(launchPath) \(newArgs.joined(separator: " "))")
logger.debug("\(cmd.joined(separator: " "))")
ShellProcessStore.shared.add(process)

var stdoutPipe: Pipe?
Expand All @@ -104,8 +86,7 @@ open class Shell {
else {
ShellProcessStore.shared.remove(process)
throw PeripheryError.shellOutputEncodingFailed(
cmd: launchPath,
args: newArgs,
cmd: cmd,
encoding: .utf8
)
}
Expand All @@ -117,8 +98,7 @@ open class Shell {
else {
ShellProcessStore.shared.remove(process)
throw PeripheryError.shellOutputEncodingFailed(
cmd: launchPath,
args: newArgs,
cmd: cmd,
encoding: .utf8
)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/XcodeSupport/Xcodebuild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public final class Xcodebuild {
]

let quotedArguments = quote(arguments: additionalArguments)
let xcodebuild = "xcodebuild \((args + envs + quotedArguments).joined(separator: " "))"
return try shell.exec(["/bin/sh", "-c", xcodebuild])
let xcodebuild = ["xcodebuild"] + args + envs + quotedArguments
return try shell.exec(xcodebuild)
}

public func removeDerivedData(for project: XcodeProjectlike, allSchemes: [String]) throws {
Expand Down Expand Up @@ -85,8 +85,8 @@ public final class Xcodebuild {
]

let quotedArguments = quote(arguments: additionalArguments)
let xcodebuild = "xcodebuild \((args + quotedArguments).joined(separator: " "))"
let lines = try shell.exec(["/bin/sh", "-c", xcodebuild]).split(separator: "\n").map { String($0).trimmed }
let xcodebuild = ["xcodebuild"] + args + quotedArguments
let lines = try shell.exec(xcodebuild).split(separator: "\n").map { String($0).trimmed }

// xcodebuild may output unrelated warnings, we need to strip them out otherwise
// JSON parsing will fail.
Expand Down
2 changes: 1 addition & 1 deletion Tests/XcodeTests/ShellMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ShellMock: Shell {

convenience init() {
let logger = Logger(quiet: true)
self.init(environment: ProcessInfo.processInfo.environment, logger: logger)
self.init(logger: logger)
}

override func exec(_: [String]) throws -> String {
Expand Down

0 comments on commit 5a8d35d

Please sign in to comment.