Implemented the Update subcommand (#7)

This PR contains the work done to implement the `Update` subcommand that update the package dependencies in a *Hummingbird* project.

Reviewed-on: #7
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
Javier Cicchelli 2025-02-19 00:14:00 +00:00 committed by Javier Cicchelli
parent 7ee071010d
commit ab5f589547
6 changed files with 67 additions and 17 deletions

View File

@ -10,7 +10,8 @@ struct Colibri: AsyncParsableCommand {
subcommands: [ subcommands: [
Build.self, Build.self,
Create.self, Create.self,
Outdated.self Outdated.self,
Update.self
], ],
defaultSubcommand: Create.self defaultSubcommand: Create.self
) )

View File

@ -20,9 +20,9 @@ extension Colibri {
mutating func run() async throws { mutating func run() async throws {
let terminalService = TerminalService() let terminalService = TerminalService()
let outdatedDependencies = OutdatedDependenciesTask(terminalService: terminalService) let updateDependencies = UpdateDependenciesTask(terminalService: terminalService)
try await outdatedDependencies(at: options.locationURL) try await updateDependencies(at: options.locationURL, checkOutdated: true)
} }
} }

View File

@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Update: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "update-dependencies",
abstract: "Update package dependencies in a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["update"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let updateDependencies = UpdateDependenciesTask(terminalService: terminalService)
try await updateDependencies(at: options.locationURL)
}
}
}

View File

@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Update {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}

View File

@ -1,6 +1,6 @@
import Foundation import Foundation
public struct OutdatedDependenciesTask { public struct UpdateDependenciesTask {
// MARK: Properties // MARK: Properties
@ -14,7 +14,7 @@ public struct OutdatedDependenciesTask {
// MARK: Functions // MARK: Functions
public func callAsFunction(at location: URL? = nil) async throws (TerminalServiceError) { public func callAsFunction(at location: URL? = nil, checkOutdated: Bool = false) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift") let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["package", "update"] var arguments: [String] = ["package", "update"]
@ -23,8 +23,10 @@ public struct OutdatedDependenciesTask {
arguments.append(contentsOf: ["--package-path", location.pathString]) arguments.append(contentsOf: ["--package-path", location.pathString])
} }
arguments.append("--dry-run") if checkOutdated {
arguments.append("--dry-run")
}
try await terminalService.run(executableURL, arguments: arguments) try await terminalService.run(executableURL, arguments: arguments)
} }

View File

@ -3,23 +3,28 @@ import Testing
@testable import ColibriLibrary @testable import ColibriLibrary
struct OutdatedDependenciesTaskTests { struct UpdateDependenciesTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder]) @Test(arguments: [nil, URL.someCurrentFolder], [false, true])
func task(at location: URL?) async throws { func task(at location: URL?, checkOutdated: Bool) async throws {
// GIVEN // GIVEN
let terminalService = TerminalServiceSpy() let terminalService = TerminalServiceSpy()
let task = OutdatedDependenciesTask(terminalService: terminalService) let task = UpdateDependenciesTask(terminalService: terminalService)
// WHEN // WHEN
try await task(at: location) try await task(at: location, checkOutdated: checkOutdated)
// THEN // THEN
let executableURL = URL(at: "/usr/bin/swift") let executableURL = URL(at: "/usr/bin/swift")
let arguments = if let location {
["package", "update", "--package-path", location.pathString, "--dry-run"] var arguments = if let location {
["package", "update", "--package-path", location.pathString]
} else { } else {
["package", "update", "--dry-run"] ["package", "update"]
}
if checkOutdated {
arguments.append("--dry-run")
} }
#expect(terminalService.actions.count == 1) #expect(terminalService.actions.count == 1)
@ -30,12 +35,12 @@ struct OutdatedDependenciesTaskTests {
func task(at location: URL?, throws error: TerminalServiceError) async throws { func task(at location: URL?, throws error: TerminalServiceError) async throws {
// GIVEN // GIVEN
let terminalService = TerminalServiceMock(action: .error(error)) let terminalService = TerminalServiceMock(action: .error(error))
let task = BuildProjectTask(terminalService: terminalService) let task = UpdateDependenciesTask(terminalService: terminalService)
// WHEN // WHEN
// THEN // THEN
await #expect(throws: error) { await #expect(throws: error) {
try await task(at: location) try await task(at: location, checkOutdated: .random())
} }
} }