-
Notifications
You must be signed in to change notification settings - Fork 55
Add progress reporter protocol #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b1655f4
4afe65d
090a75d
879eb58
af7b2d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import Foundation | |
import SwiftlyCore | ||
import SystemPackage | ||
@preconcurrency import TSCBasic | ||
import TSCUtility | ||
|
||
struct Install: SwiftlyCommand { | ||
public static let configuration = CommandConfiguration( | ||
|
@@ -313,16 +312,16 @@ struct Install: SwiftlyCommand { | |
} | ||
} | ||
|
||
let animation: ProgressAnimationProtocol = | ||
let animation: ProgressReporterProtocol = | ||
if let progressFile | ||
{ | ||
try JsonFileProgressReporter(ctx, filePath: progressFile) | ||
} else { | ||
PercentProgressAnimation(stream: stdoutStream, header: "Downloading \(version)") | ||
ConsoleProgressReporter(stream: stdoutStream, header: "Downloading \(version)") | ||
} | ||
|
||
defer { | ||
try? (animation as? JsonFileProgressReporter)?.close() | ||
try? animation.close() | ||
} | ||
|
||
var lastUpdate = Date() | ||
|
@@ -351,22 +350,28 @@ struct Install: SwiftlyCommand { | |
|
||
lastUpdate = Date() | ||
|
||
animation.update( | ||
step: progress.receivedBytes, | ||
total: progress.totalBytes!, | ||
text: | ||
"Downloaded \(String(format: "%.1f", downloadedMiB)) MiB of \(String(format: "%.1f", totalMiB)) MiB" | ||
) | ||
do { | ||
try await animation.update( | ||
step: progress.receivedBytes, | ||
total: progress.totalBytes!, | ||
text: | ||
"Downloaded \(String(format: "%.1f", downloadedMiB)) MiB of \(String(format: "%.1f", totalMiB)) MiB" | ||
) | ||
} catch { | ||
await ctx.message( | ||
"Failed to update progress: \(error.localizedDescription)" | ||
) | ||
} | ||
} | ||
) | ||
} catch let notFound as DownloadNotFoundError { | ||
throw SwiftlyError( | ||
message: "\(version) does not exist at URL \(notFound.url), exiting") | ||
} catch { | ||
animation.complete(success: false) | ||
try? await animation.complete(success: false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: It's good to do a best effort complete here so that the original error gets reported since that's probably the more relevant one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I didn't get this @cmcgee1024. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roulpriya in this catch block, there's an error that was thrown. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roulpriya in this catch block, there's an error that was thrown. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, got it. |
||
throw error | ||
} | ||
animation.complete(success: true) | ||
try await animation.complete(success: true) | ||
|
||
if verifySignature { | ||
try await Swiftly.currentPlatform.verifyToolchainSignature( | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Foundation | ||
import SwiftlyCore | ||
import SystemPackage | ||
import TSCBasic | ||
import TSCUtility | ||
|
||
public protocol ProgressReporterProtocol { | ||
/// Updates the progress animation with the current step, total steps, and an optional text message. | ||
func update(step: Int, total: Int, text: String) async throws | ||
|
||
/// Completes the progress animation, indicating success or failure. | ||
func complete(success: Bool) async throws | ||
|
||
/// Closes any resources used by the reporter, if applicable. | ||
func close() throws | ||
} | ||
|
||
/// Progress reporter that delegates to a `PercentProgressAnimation` for console output. | ||
struct ConsoleProgressReporter: ProgressReporterProtocol { | ||
private let reporter: PercentProgressAnimation | ||
|
||
init(stream: WritableByteStream, header: String) { | ||
self.reporter = PercentProgressAnimation(stream: stream, header: header) | ||
} | ||
|
||
func update(step: Int, total: Int, text: String) async throws { | ||
self.reporter.update(step: step, total: total, text: text) | ||
} | ||
|
||
func complete(success: Bool) async throws { | ||
self.reporter.complete(success: success) | ||
} | ||
|
||
func close() throws { | ||
// No resources to close for console reporter | ||
} | ||
} | ||
|
||
enum ProgressInfo: Codable { | ||
case step(timestamp: Date, percent: Int, text: String) | ||
case complete(success: Bool) | ||
} | ||
|
||
struct JsonFileProgressReporter: ProgressReporterProtocol { | ||
let filePath: FilePath | ||
private let encoder: JSONEncoder | ||
private let ctx: SwiftlyCoreContext | ||
private let fileHandle: FileHandle | ||
|
||
init(_ ctx: SwiftlyCoreContext, filePath: FilePath, encoder: JSONEncoder = JSONEncoder()) throws | ||
{ | ||
self.ctx = ctx | ||
self.filePath = filePath | ||
self.encoder = encoder | ||
self.fileHandle = try FileHandle(forWritingTo: URL(fileURLWithPath: filePath.string)) | ||
} | ||
|
||
private func writeProgress(_ progress: ProgressInfo) async throws { | ||
let jsonData = try self.encoder.encode(progress) | ||
|
||
self.fileHandle.write(jsonData) | ||
self.fileHandle.write("\n".data(using: .utf8) ?? Data()) | ||
try self.fileHandle.synchronize() | ||
} | ||
|
||
func update(step: Int, total: Int, text: String) async throws { | ||
guard total > 0 && step <= total else { | ||
return | ||
} | ||
try await self.writeProgress( | ||
ProgressInfo.step( | ||
timestamp: Date(), | ||
percent: Int(Double(step) / Double(total) * 100), | ||
text: text | ||
) | ||
) | ||
} | ||
|
||
func complete(success: Bool) async throws { | ||
try await self.writeProgress(ProgressInfo.complete(success: success)) | ||
} | ||
|
||
func close() throws { | ||
try self.fileHandle.close() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: It's great to see this TSC dependency being further isolated. The hope is to remove the dependency entirely someday, and use something that isn't deprecated, and adds to the size of the swiftly binary.