diff --git a/Sources/ProjectDrivers/BazelProjectDriver.swift b/Sources/ProjectDrivers/BazelProjectDriver.swift index 1c15d1587..8727554a8 100644 --- a/Sources/ProjectDrivers/BazelProjectDriver.swift +++ b/Sources/ProjectDrivers/BazelProjectDriver.swift @@ -123,21 +123,14 @@ public class BazelProjectDriver: ProjectDriver { private func queryTargets() throws -> [String] { try shell - .exec(["bazel", "query", query]) + .exec(["bazel", "query", "\"\(query)\""]) .split(separator: "\n") .map { "\"@@\($0)\"" } } private var query: String { - let query = """ - filter( - '^//.*', - kind( - '(\(Self.topLevelKinds.joined(separator: "|"))) rule', - deps(//...) - ) - ) - """ + let kinds = Self.topLevelKinds.joined(separator: "|") + let query = "filter('^//.*', kind('(\(kinds)) rule', deps(//...)))" if let pattern = configuration.bazelFilter { return "filter('\(pattern)', \(query))" diff --git a/Sources/Shared/PeripheryError.swift b/Sources/Shared/PeripheryError.swift index 56d9bc3ae..79e655e7b 100644 --- a/Sources/Shared/PeripheryError.swift +++ b/Sources/Shared/PeripheryError.swift @@ -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) @@ -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): diff --git a/Sources/Shared/Shell.swift b/Sources/Shared/Shell.swift index d9e834732..d7a302e0a 100644 --- a/Sources/Shared/Shell.swift +++ b/Sources/Shared/Shell.swift @@ -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") } @@ -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? @@ -104,8 +86,7 @@ open class Shell { else { ShellProcessStore.shared.remove(process) throw PeripheryError.shellOutputEncodingFailed( - cmd: launchPath, - args: newArgs, + cmd: cmd, encoding: .utf8 ) } @@ -117,8 +98,7 @@ open class Shell { else { ShellProcessStore.shared.remove(process) throw PeripheryError.shellOutputEncodingFailed( - cmd: launchPath, - args: newArgs, + cmd: cmd, encoding: .utf8 ) } diff --git a/Sources/XcodeSupport/Xcodebuild.swift b/Sources/XcodeSupport/Xcodebuild.swift index fb59a86a8..8d098ad09 100644 --- a/Sources/XcodeSupport/Xcodebuild.swift +++ b/Sources/XcodeSupport/Xcodebuild.swift @@ -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 { @@ -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. diff --git a/Tests/XcodeTests/ShellMock.swift b/Tests/XcodeTests/ShellMock.swift index 8a99a60b7..c3cacb8ba 100644 --- a/Tests/XcodeTests/ShellMock.swift +++ b/Tests/XcodeTests/ShellMock.swift @@ -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 {