|
| 1 | +import Foundation |
| 2 | +import SwiftlyCore |
| 3 | +import SystemPackage |
| 4 | +import TSCBasic |
| 5 | +import TSCUtility |
| 6 | + |
| 7 | +public protocol ProgressReporterProtocol { |
| 8 | + /// Updates the progress animation with the current step, total steps, and an optional text message. |
| 9 | + func update(step: Int, total: Int, text: String) async throws |
| 10 | + |
| 11 | + /// Completes the progress animation, indicating success or failure. |
| 12 | + func complete(success: Bool) async throws |
| 13 | + |
| 14 | + /// Closes any resources used by the reporter, if applicable. |
| 15 | + func close() throws |
| 16 | +} |
| 17 | + |
| 18 | +/// Progress reporter that delegates to a `PercentProgressAnimation` for console output. |
| 19 | +struct ConsoleProgressReporter: ProgressReporterProtocol { |
| 20 | + private let reporter: PercentProgressAnimation |
| 21 | + |
| 22 | + init(stream: WritableByteStream, header: String) { |
| 23 | + self.reporter = PercentProgressAnimation(stream: stream, header: header) |
| 24 | + } |
| 25 | + |
| 26 | + func update(step: Int, total: Int, text: String) async throws { |
| 27 | + self.reporter.update(step: step, total: total, text: text) |
| 28 | + } |
| 29 | + |
| 30 | + func complete(success: Bool) async throws { |
| 31 | + self.reporter.complete(success: success) |
| 32 | + } |
| 33 | + |
| 34 | + func close() throws { |
| 35 | + // No resources to close for console reporter |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +enum ProgressInfo: Codable { |
| 40 | + case step(timestamp: Date, percent: Int, text: String) |
| 41 | + case complete(success: Bool) |
| 42 | +} |
| 43 | + |
| 44 | +struct JsonFileProgressReporter: ProgressReporterProtocol { |
| 45 | + let filePath: FilePath |
| 46 | + private let encoder: JSONEncoder |
| 47 | + private let ctx: SwiftlyCoreContext |
| 48 | + private let fileHandle: FileHandle |
| 49 | + |
| 50 | + init(_ ctx: SwiftlyCoreContext, filePath: FilePath, encoder: JSONEncoder = JSONEncoder()) throws |
| 51 | + { |
| 52 | + self.ctx = ctx |
| 53 | + self.filePath = filePath |
| 54 | + self.encoder = encoder |
| 55 | + self.fileHandle = try FileHandle(forWritingTo: URL(fileURLWithPath: filePath.string)) |
| 56 | + } |
| 57 | + |
| 58 | + private func writeProgress(_ progress: ProgressInfo) async throws { |
| 59 | + let jsonData = try self.encoder.encode(progress) |
| 60 | + |
| 61 | + self.fileHandle.write(jsonData) |
| 62 | + self.fileHandle.write("\n".data(using: .utf8) ?? Data()) |
| 63 | + try self.fileHandle.synchronize() |
| 64 | + } |
| 65 | + |
| 66 | + func update(step: Int, total: Int, text: String) async throws { |
| 67 | + guard total > 0 && step <= total else { |
| 68 | + return |
| 69 | + } |
| 70 | + try await self.writeProgress( |
| 71 | + ProgressInfo.step( |
| 72 | + timestamp: Date(), |
| 73 | + percent: Int(Double(step) / Double(total) * 100), |
| 74 | + text: text |
| 75 | + ) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + func complete(success: Bool) async throws { |
| 80 | + try await self.writeProgress(ProgressInfo.complete(success: success)) |
| 81 | + } |
| 82 | + |
| 83 | + func close() throws { |
| 84 | + try self.fileHandle.close() |
| 85 | + } |
| 86 | +} |
0 commit comments