Skip to content

Commit

Permalink
Refactor Outdated version logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iv-mexx committed Dec 19, 2017
1 parent 2348da8 commit 37b88fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
29 changes: 17 additions & 12 deletions Source/carthage/Formatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,40 @@ public struct ColorOptions: OptionsProtocol {
}
}

extension OutdatedCommand.UpdateAvailabilityAndApplicability {
fileprivate static let dict: [OutdatedCommand.UpdateAvailabilityAndApplicability: Color.Named.Color] = [
.updatesAvailableAllApplicable: .green,
.updatesAvailableSomeApplicable: .yellow,
.updatesAvailableNoneApplicable: .red,
]
extension OutdatedCommand.UpdateType {
var color: Color.Named.Color {
switch self {
case .newest:
return .green
case .newer:
return .yellow
case .ineligible:
return .red
}
}
}

extension ColorOptions.Formatting {
subscript(_ index: OutdatedCommand.UpdateAvailabilityAndApplicability) -> Wrap {
guard self.isColorful, let color = OutdatedCommand.UpdateAvailabilityAndApplicability.dict[index] else {
subscript(_ index: OutdatedCommand.UpdateType?) -> Wrap {
guard self.isColorful, let update = index else {
return { $0 }
}
return Color.Wrap(foreground: color).wrap
return Color.Wrap(foreground: update.color).wrap
}

public var legendForOutdatedCommand: String? {
guard self.isColorful else { return nil }

let header = "Legend — <color> • «what happens when you run `carthage update`»:\n"
return OutdatedCommand.UpdateAvailabilityAndApplicability.dict.reduce(into: header) {
let (situation, color) = $1
return [OutdatedCommand.UpdateType.newest, .newer, .ineligible].reduce(into: header) {
let (color, explanation) = ($1.color, $1.explanation)
let tabs = String(
repeating: "\t",
count: color == .yellow || color == .magenta ? 1 : 2
)
let colorDescription = Color.Wrap(foreground: color)
.wrap("<" + String(describing: color) + ">")
$0.append(colorDescription + tabs + "" + situation.rawValue + "\n")
$0.append(colorDescription + tabs + "" + explanation + "\n")
}
}
}
46 changes: 25 additions & 21 deletions Source/carthage/Outdated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ import Curry

/// Type that encapsulates the configuration and evaluation of the `outdated` subcommand.
public struct OutdatedCommand: CommandProtocol {
enum UpdateAvailabilityAndApplicability: String {
case updatesAvailableAllApplicable =
"Will be updated to the newest version."
case updatesAvailableSomeApplicable =
"Will be updated, but not to the newest version because of the specified version in Cartfile."
case updatesAvailableNoneApplicable =
"Will not be updated because of the specified version in Cartfile."
case noUpdatesAvailable =
"Will not be updated: no updates available."
enum UpdateType {
case newest
case newer
case ineligible

init(currentVersion current: PinnedVersion, applicableVersion applicable: PinnedVersion, latestVersion latest: PinnedVersion) {
switch (current, applicable, latest) {
case (current, applicable, latest) where current != latest && applicable == latest:
self = .updatesAvailableAllApplicable
case (current, applicable, latest) where current != applicable && applicable != latest:
self = .updatesAvailableSomeApplicable
case (current, applicable, latest) where current != latest && current == applicable:
self = .updatesAvailableNoneApplicable
default:
self = .noUpdatesAvailable
init?(currentVersion current: PinnedVersion, applicableVersion applicable: PinnedVersion, latestVersion latest: PinnedVersion) {
guard current != latest else { return nil }
if applicable == latest {
self = .newest
} else if current != applicable {
self = .newer
} else {
self = .ineligible
}
}

var explanation: String {
switch self {
case .newest:
return "Will be updated to the newest version."
case .newer:
return "Will be updated, but not to the newest version because of the specified version in Cartfile."
case .ineligible:
return "Will not be updated because of the specified version in Cartfile."
}
}
}
Expand Down Expand Up @@ -83,8 +87,8 @@ public struct OutdatedCommand: CommandProtocol {
if options.outputXcodeWarnings {
carthage.println("warning: \(formatting.projectName(project.name)) is out of date (\(current) -> \(applicable)) (Latest: \(latest))")
} else {
let availability = UpdateAvailabilityAndApplicability(currentVersion: current, applicableVersion: applicable, latestVersion: latest)
let style = formatting[availability]
let update = UpdateType(currentVersion: current, applicableVersion: applicable, latestVersion: latest)
let style = formatting[update]
let versionSummary = "\(style(current.description)) -> \(style(applicable.description)) (Latest: \(latest))"
carthage.println(formatting.projectName(project.name) + " " + versionSummary)
}
Expand Down

0 comments on commit 37b88fc

Please sign in to comment.