-
Notifications
You must be signed in to change notification settings - Fork 130
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
lennet
wants to merge
1
commit into
yonaskolb:master
Choose a base branch
from
lennet:outdated-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ public class Mint { | |
return versionsByPackage | ||
} | ||
|
||
func resolvePackage(_ package: PackageReference) throws { | ||
func resolvePackage(_ package: PackageReference, silent: Bool = false) throws { | ||
|
||
// resolve version from MintFile | ||
if package.version.isEmpty, | ||
|
@@ -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.") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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)") | ||
|
||
|
@@ -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) | ||
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'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)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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?