Skip to content

Commit 3177d8d

Browse files
authored
Prevent xcodebuild from outputting to console. (#2983)
1 parent fc6074e commit 3177d8d

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

ZipBuilder/Sources/ZipBuilder/FrameworkBuilder.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,53 @@ struct FrameworkBuilder {
150150
/// This runs a command and immediately returns a Shell result.
151151
/// NOTE: This exists in conjunction with the `Shell.execute...` due to issues with different
152152
/// `.bash_profile` environment variables. This should be consolidated in the future.
153-
private func syncExec(command: String, args: [String] = []) -> Shell.Result {
153+
private func syncExec(command: String, args: [String] = [], captureOutput: Bool = false) -> Shell.Result {
154154
let task = Process()
155155
task.launchPath = command
156156
task.arguments = args
157+
158+
// If we want to output to the console, create a readabilityHandler and save each line along the
159+
// way. Otherwise, we can just read the pipe at the end. By disabling outputToConsole, some
160+
// commands (such as any xcodebuild) can run much, much faster.
161+
var output: [String] = []
162+
if captureOutput {
163+
let pipe = Pipe()
164+
task.standardOutput = pipe
165+
let outHandle = pipe.fileHandleForReading
166+
167+
outHandle.readabilityHandler = { pipe in
168+
// This will be run any time data is sent to the pipe. We want to print it and store it for
169+
// later. Ignore any non-valid Strings.
170+
guard let line = String(data: pipe.availableData, encoding: .utf8) else {
171+
print("Could not get data from pipe for command \(command): \(pipe.availableData)")
172+
return
173+
}
174+
output.append(line)
175+
}
176+
// Also set the termination handler on the task in order to stop the readabilityHandler from
177+
// parsing any more data from the task.
178+
task.terminationHandler = { t in
179+
guard let stdOut = t.standardOutput as? Pipe else { return }
180+
181+
stdOut.fileHandleForReading.readabilityHandler = nil
182+
}
183+
} else {
184+
// No capturing output, just mark it as complete.
185+
output = ["The task completed"]
186+
}
187+
157188
task.launch()
158189
task.waitUntilExit()
159-
//
160-
// var pipe = Pipe()
161-
// task.standardOutput = pipe
162-
// let handle = pipe.fileHandleForReading
190+
191+
let fullOutput = output.joined(separator: "\n")
163192

164193
// Normally we'd use a pipe to retrieve the output, but for whatever reason it slows things down
165194
// tremendously for xcodebuild.
166-
let output = "The task completed."
167195
guard task.terminationStatus == 0 else {
168-
return .error(code: task.terminationStatus, output: output)
196+
return .error(code: task.terminationStatus, output: fullOutput)
169197
}
170198

171-
return .success(output: output)
199+
return .success(output: fullOutput)
172200
}
173201

174202
/// Uses `xcodebuild` to build a framework for a specific architecture slice.
@@ -204,7 +232,7 @@ struct FrameworkBuilder {
204232
let logFileName = "\(framework)-\(arch.rawValue)-\(platform.rawValue).txt"
205233
let logFile = logRoot.appendingPathComponent(logFileName)
206234

207-
let result = syncExec(command: "/usr/bin/xcodebuild", args: args)
235+
let result = syncExec(command: "/usr/bin/xcodebuild", args: args, captureOutput: true)
208236
switch result {
209237
case let .error(code, output):
210238
// Write output to disk and print the location of it. Force unwrapping here since it's going
@@ -218,6 +246,9 @@ struct FrameworkBuilder {
218246
// Try to write the output to the log file but if it fails it's not a huge deal since it was
219247
// a successful build.
220248
try? output.write(to: logFile, atomically: true, encoding: .utf8)
249+
print("""
250+
Successfully built \(framework) for \(arch.rawValue). Build log can be found at \(logFile)
251+
""")
221252

222253
// Use the Xcode-generated path to return the path to the compiled library.
223254
let libPath = buildDir.appendingPathComponents(["Release-\(platform.rawValue)",
@@ -454,7 +485,6 @@ struct FrameworkBuilder {
454485
try fileManager.createDirectory(at: parentDir, withIntermediateDirectories: true)
455486
}
456487

457-
print("Attempting to copy \(location) to \(finalPath)")
458488
try fileManager.copyItem(at: location, to: finalPath)
459489
}
460490
}

0 commit comments

Comments
 (0)