@@ -150,25 +150,53 @@ struct FrameworkBuilder {
150
150
/// This runs a command and immediately returns a Shell result.
151
151
/// NOTE: This exists in conjunction with the `Shell.execute...` due to issues with different
152
152
/// `.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 {
154
154
let task = Process ( )
155
155
task. launchPath = command
156
156
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
+
157
188
task. launch ( )
158
189
task. waitUntilExit ( )
159
- //
160
- // var pipe = Pipe()
161
- // task.standardOutput = pipe
162
- // let handle = pipe.fileHandleForReading
190
+
191
+ let fullOutput = output. joined ( separator: " \n " )
163
192
164
193
// Normally we'd use a pipe to retrieve the output, but for whatever reason it slows things down
165
194
// tremendously for xcodebuild.
166
- let output = " The task completed. "
167
195
guard task. terminationStatus == 0 else {
168
- return . error( code: task. terminationStatus, output: output )
196
+ return . error( code: task. terminationStatus, output: fullOutput )
169
197
}
170
198
171
- return . success( output: output )
199
+ return . success( output: fullOutput )
172
200
}
173
201
174
202
/// Uses `xcodebuild` to build a framework for a specific architecture slice.
@@ -204,7 +232,7 @@ struct FrameworkBuilder {
204
232
let logFileName = " \( framework) - \( arch. rawValue) - \( platform. rawValue) .txt "
205
233
let logFile = logRoot. appendingPathComponent ( logFileName)
206
234
207
- let result = syncExec ( command: " /usr/bin/xcodebuild " , args: args)
235
+ let result = syncExec ( command: " /usr/bin/xcodebuild " , args: args, captureOutput : true )
208
236
switch result {
209
237
case let . error( code, output) :
210
238
// Write output to disk and print the location of it. Force unwrapping here since it's going
@@ -218,6 +246,9 @@ struct FrameworkBuilder {
218
246
// Try to write the output to the log file but if it fails it's not a huge deal since it was
219
247
// a successful build.
220
248
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
+ """ )
221
252
222
253
// Use the Xcode-generated path to return the path to the compiled library.
223
254
let libPath = buildDir. appendingPathComponents ( [ " Release- \( platform. rawValue) " ,
@@ -454,7 +485,6 @@ struct FrameworkBuilder {
454
485
try fileManager. createDirectory ( at: parentDir, withIntermediateDirectories: true )
455
486
}
456
487
457
- print ( " Attempting to copy \( location) to \( finalPath) " )
458
488
try fileManager. copyItem ( at: location, to: finalPath)
459
489
}
460
490
}
0 commit comments