Skip to content
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

Added outdated + update commands #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/MintCLI/Commands/OutdatedCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import MintKit

class OutdatedCommand: MintCommand {

init(mint: Mint) {
super.init(mint: mint,
name: "outdated",
description: "List all the currently installed and linked packages that are outdated.")
}

override func execute() throws {
try super.execute()
try mint.outdated()
}
}
16 changes: 16 additions & 0 deletions Sources/MintCLI/Commands/UpdateCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import MintKit

class UpdateCommand: MintCommand {

init(mint: Mint) {
super.init(mint: mint,
name: "update",
description: "Updates all currently installed and linked packages that are outdated.")
}

override func execute() throws {
try super.execute()
try mint.update()
}
}
2 changes: 2 additions & 0 deletions Sources/MintCLI/MintCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class MintCLI {
UninstallCommand(mint: mint),
ListCommand(mint: mint),
BootstrapCommand(mint: mint),
OutdatedCommand(mint: mint),
UpdateCommand(mint: mint)
])
}

Expand Down
58 changes: 55 additions & 3 deletions Sources/MintKit/Mint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class Mint {
return versionsByPackage
}

func resolvePackage(_ package: PackageReference) throws {
func resolvePackage(_ package: PackageReference, silent: Bool = false) throws {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super happy with that solution to silent the method.. Maybe it would be cleaner to inject output. as. an optional parameter into the functions?


// resolve version from MintFile
if package.version.isEmpty,
Expand All @@ -140,7 +140,9 @@ public class Mint {
if let mintFilePackage = mintfile.package(for: package.repo), !mintFilePackage.version.isEmpty {
package.version = mintFilePackage.version
package.repo = mintFilePackage.repo
output("Using \(package.repo) \(package.version) from Mintfile.")
if !silent {
output("Using \(package.repo) \(package.version) from Mintfile.")
}
}
}

Expand All @@ -157,7 +159,9 @@ public class Mint {
// resove latest version from git repo
if package.version.isEmpty {
// we don't have a specific version, let's get the latest tag
output("Finding latest version of \(package.name)")
if !silent {
output("Finding latest version of \(package.name)")
}
do {
let tagOutput = try SwiftCLI.capture(bash: "git ls-remote --tags --refs \(package.gitPath)")

Expand Down Expand Up @@ -450,4 +454,52 @@ public class Mint {
}
try? installPath.delete()
}

@discardableResult
public func outdated(silent: Bool = false) throws -> [String : (oldVersion: String, newReference: PackageReference) ] {
guard packagesPath.exists else {
if !silent {
output("No mint packages installed")
}
return [:]
}

var outdatedPackageReferences: [String: (oldVersion: String, newReference: PackageReference)] = [:]
let outdatedPackages: [String] = try getLinkedPackages().compactMap { (name, version) in
guard let gitRepo = try getPackageGit(name: name) else {
return nil
}
let reference = PackageReference(repo: gitRepo)
try resolvePackage(reference, silent: true)
if reference.version > version {
outdatedPackageReferences[name] = (oldVersion: version, newReference: reference)
return "\(name): \(version) < \(reference.version)"
} else {
return nil
}
}.sorted { $0.localizedStandardCompare($1) == .orderedAscending }

if outdatedPackages.isEmpty {
if !silent {
output("All packages are up to date")
}
} else {
if !silent {
output("Outdated mint packages:\n\(outdatedPackages.joined(separator: "\n"))")
}
}
return outdatedPackageReferences
}

public func update() throws {
let outdatedPackages = try outdated(silent: true)
guard !outdatedPackages.isEmpty else {
output("All packages are already up to date")
return
}
for (name, value) in outdatedPackages {
try install(package: value.newReference, executable: name, force: true, link: true)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if I need to provide the executable name here

output("Updated \(value.newReference.name) from version \(value.oldVersion) to \(value.newReference.version)")
}
}
}
27 changes: 27 additions & 0 deletions Tests/MintTests/MintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MintTests: XCTestCase {
standardOut: WriteStream.null,
standardError: WriteStream.null)
let testRepo = "yonaskolb/SimplePackage"
let gitTestRepo = "https://github.com/yonaskolb/SimplePackage.git"
let sshTestRepo = "[email protected]:yonaskolb/SimplePackage.git"
let testVersion = "4.0.0"
let latestVersion = "5.0.0"
Expand Down Expand Up @@ -189,4 +190,30 @@ class MintTests: XCTestCase {
try mint.install(package: PackageReference(repo: "yonaskolb/simplepackage", version: "compile_error"))
}
}

func testOutdated() throws {
let specificPackage = PackageReference(repo: testRepo, version: testVersion)
let updatedPackage = PackageReference(repo: gitTestRepo, version: latestVersion)
try mint.install(package: specificPackage, link: true)
let outdatedResult = try mint.outdated()
XCTAssertEqual(outdatedResult.count, 1)
let result = outdatedResult["simplepackage"]!
XCTAssertEqual(result.oldVersion, testVersion)
XCTAssertEqual(result.newReference, updatedPackage)

try mint.install(package: updatedPackage, force: true, link: true)
let outdatedResultAfterLatestInstallation = try mint.outdated()
XCTAssertEqual(outdatedResultAfterLatestInstallation.count, 0)
}

func testUpdate() throws {
let specificPackage = PackageReference(repo: testRepo, version: testVersion)
try mint.install(package: specificPackage, link: true)
let outdatedResult = try mint.outdated()
XCTAssertEqual(outdatedResult.count, 1)

try mint.update()
let outdatedResultAfterLatestInstallation = try mint.outdated()
XCTAssertEqual(outdatedResultAfterLatestInstallation.count, 0)
}
}