Skip to content

Commit dceb7a4

Browse files
committed
Fix reading long files
1 parent 75aa71a commit dceb7a4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Tool/Sources/SystemUtils/SystemUtils.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ public class SystemUtils {
198198
}
199199

200200
public static func executeCommand(
201-
inDirectory directory: String = NSHomeDirectory(),
202-
path: String,
201+
inDirectory directory: String = NSHomeDirectory(),
202+
path: String,
203203
arguments: [String]
204204
) throws -> String? {
205205
let task = Process()
@@ -216,11 +216,21 @@ public class SystemUtils {
216216
task.arguments = arguments
217217
task.standardOutput = pipe
218218
task.currentDirectoryURL = URL(fileURLWithPath: directory)
219+
220+
var accumulatedOutput = Data()
221+
pipe.fileHandleForReading.readabilityHandler = { handle in
222+
let data = handle.availableData
223+
guard data.isEmpty else { // End of file
224+
return accumulatedOutput.append(data)
225+
}
226+
227+
handle.readabilityHandler = nil // Stop observing
228+
}
219229

220230
try task.run()
221231
task.waitUntilExit()
222232
let data = pipe.fileHandleForReading.readDataToEndOfFile()
223-
return String(data: data, encoding: .utf8)
233+
return String(data: accumulatedOutput + data, encoding: .utf8)
224234
}
225235

226236
public func appendCommonBinPaths(path: String) -> String {

0 commit comments

Comments
 (0)