Skip to content

Commit d1df62b

Browse files
authored
Refactor progress report on Clean Command (#151)
* Remove unused method * Fix typo in folder * Add foldMTap to IO * Create protocol to report progress * Create description for events in Clean command * Create implementation for protocol to report progress * Remove unnecessary extension * Update clean dependencies * Update API to receive new dependencies * Include new reporting of events in clean command * Adapt command to report progress * Add new cases to progress report * Report finalization at the end of command * Finish command * Re-indent * Move decoration to console implementation
1 parent 085d7aa commit d1df62b

File tree

14 files changed

+217
-65
lines changed

14 files changed

+217
-65
lines changed

project/Component/NefClean/Clean.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ public struct Clean {
1111

1212
public func nefPlayground(_ nefPlayground: NefPlaygroundURL) -> EnvIO<CleanEnvironment, CleanError, Void> {
1313
EnvIO { (env: CleanEnvironment) in
14-
binding(
15-
|<-env.console.print(information: "\t• Clean playground '\(nefPlayground.name)'"),
14+
let step = CleanEvent.cleaningPlayground(nefPlayground.name)
15+
return binding(
16+
|<-env.progressReport.inProgress(step),
1617
|<-env.shell.clean(playground: nefPlayground).provide(env.fileSystem).mapError { e in .clean(info: e) },
17-
yield: ())^.reportStatus(console: env.console)
18+
yield: ())^
19+
.foldMTap(
20+
{ e in env.progressReport.failed(step, e) },
21+
{ env.progressReport.succeeded(step) })
22+
1823
}
1924
}
2025
}

project/Component/NefClean/Models/CleanEnvironment.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import NefCommon
55
import NefModels
66

77
public struct CleanEnvironment {
8-
public let console: Console
8+
public let progressReport: ProgressReport
99
public let fileSystem: FileSystem
1010
public let shell: NefPlaygroundSystem
1111

12-
public init(console: Console, fileSystem: FileSystem, shell: NefPlaygroundSystem) {
13-
self.console = console
12+
public init(progressReport: ProgressReport, fileSystem: FileSystem, shell: NefPlaygroundSystem) {
13+
self.progressReport = progressReport
1414
self.fileSystem = fileSystem
1515
self.shell = shell
1616
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import NefModels
2+
3+
public enum CleanEvent {
4+
case cleaningPlayground(String)
5+
}
6+
7+
extension CleanEvent: CustomProgressDescription {
8+
public var progressDescription: String {
9+
switch self {
10+
case let .cleaningPlayground(name):
11+
return "\t• Cleaning playground '\(name)'..."
12+
}
13+
}
14+
}

project/Component/NefClean/Utils/IO+Console.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import BowEffects
2+
3+
public extension IO {
4+
func foldMTap<B>(_ f: @escaping (E) -> IO<E, B>,
5+
_ g: @escaping (A) -> IO<E, B>) -> IO<E, A> {
6+
handleErrorWith { e in
7+
f(e).followedBy(.raiseError(e))
8+
}.flatTap(g)^
9+
}
10+
}

project/Component/NefModels/Console.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ public struct Step {
3030
extension Step {
3131
/// Defined dummy `Step`
3232
public static var empty: Step { .init(total: 0, partial: 0, duration: .never) }
33-
34-
/// Advance the step a number of increments.
35-
/// - Parameter partial: Number of increments for the step.
36-
/// - Returns: A `Step` with the current operation advanced.
37-
public func increment(_ partial: UInt) -> Step {
38-
.init(total: total, partial: self.partial + partial, duration: estimatedDuration)
39-
}
4033
}
4134

4235
/// Describes a `Console` to represent progress information.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import BowEffects
2+
3+
public protocol ProgressReport {
4+
func notify<E: Error, A: CustomProgressDescription>(_ event: ProgressEvent<A>) -> IO<E, Void>
5+
}
6+
7+
public extension ProgressReport {
8+
func oneShot<E: Error, A: CustomProgressDescription>(_ step: A) -> IO<E, Void> {
9+
self.notify(
10+
ProgressEvent(step: step,
11+
status: .oneShot))
12+
}
13+
14+
func inProgress<E: Error, A: CustomProgressDescription>(_ step: A) -> IO<E, Void> {
15+
self.notify(
16+
ProgressEvent(step: step,
17+
status: .inProgress))
18+
}
19+
20+
func succeeded<E: Error, A: CustomProgressDescription>(_ step: A, info: String = "") -> IO<E, Void> {
21+
self.notify(
22+
ProgressEvent(step: step,
23+
status: .successful(info: info)))
24+
}
25+
26+
func failed<E: Error, A: CustomProgressDescription>(_ step: A, _ error: E, info: String = "") -> IO<E, Void> {
27+
self.notify(
28+
ProgressEvent(step: step,
29+
status: .failed(error, info: info)))
30+
}
31+
32+
func finished<E: Error, A: CustomProgressDescription>(successfully step: A) -> IO<E, Void> {
33+
self.notify(
34+
ProgressEvent(step: step,
35+
status: .finishedSuccessfully))
36+
}
37+
38+
func finished<E: Error, A: CustomProgressDescription>(withError step: A) -> IO<E, Void> {
39+
self.notify(
40+
ProgressEvent(step: step,
41+
status: .finishedWithError))
42+
}
43+
}
44+
45+
public enum ProgressEventStatus {
46+
case oneShot
47+
case inProgress
48+
case successful(info: String)
49+
case failed(Error, info: String)
50+
case finishedSuccessfully
51+
case finishedWithError
52+
}
53+
54+
public protocol CustomProgressDescription {
55+
var progressDescription: String { get }
56+
}
57+
58+
public struct ProgressEvent<A: CustomProgressDescription> {
59+
public let step: A
60+
public let status: ProgressEventStatus
61+
62+
public init(step: A, status: ProgressEventStatus) {
63+
self.step = step
64+
self.status = status
65+
}
66+
}

project/Component/nef/API.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public protocol CleanAPI {
6666
///
6767
/// - Parameters:
6868
/// - nefPlayground: Folder where to search for Xcode Playgrounds - it must be a nef Playground structure.
69-
/// - Returns: An `EnvIO` to perform IO operations that produce errors of type `nef.Error`, having access to an immutable environment of type `Console`.
70-
static func clean(nefPlayground: URL) -> EnvIO<Console, nef.Error, Void>
69+
/// - Returns: An `EnvIO` to perform IO operations that produce errors of type `nef.Error`, having access to an immutable environment of type `ProgressReport`.
70+
static func clean(nefPlayground: URL) -> EnvIO<ProgressReport, nef.Error, Void>
7171
}
7272

7373
/// Describes the API for `Markdown`

0 commit comments

Comments
 (0)