Implemented the Create subcommand (#5)

This PR contains the work done to add the *Build* subcommand that build a *Hummingbird* project from the command line to the `Colibri` command.

Reviewed-on: #5
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-18 23:03:51 +00:00 committed by Javier Cicchelli
parent 212ca52279
commit 6a3b9b5141
9 changed files with 158 additions and 10 deletions

View File

@ -7,7 +7,11 @@ struct Colibri: AsyncParsableCommand {
static let configuration = CommandConfiguration(
abstract: "The utility to manage your Hummingbird apps",
subcommands: [Create.self]
subcommands: [
Build.self,
Create.self
],
defaultSubcommand: Create.self
)
}

View File

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

View File

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

View File

@ -1,8 +1,8 @@
import ArgumentParser
import Foundation
import ColibriLibrary
extension Colibri.Create {
struct Options: ParsableArguments {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@ -12,11 +12,5 @@ extension Colibri.Create {
@Option(name: .shortAndLong)
var location: String?
// MARK: Computed
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
}
}

View File

@ -0,0 +1,21 @@
import Foundation
public protocol Locationable {
// MARK: Properties
var location: String? { get set }
}
// MARK: - Locationable+Properties
public extension Locationable {
// MARK: Properties
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
}

View File

@ -0,0 +1,29 @@
import Foundation
public struct BuildProjectTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["build"]
if let location {
arguments.append(contentsOf: ["--package-path", location.pathString])
}
try await terminalService.run(executableURL, arguments: arguments)
}
}

View File

@ -0,0 +1,42 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct BuildProjectTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder])
func task(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = BuildProjectTask(terminalService: terminalService)
// WHEN
try await task(at: location)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
let arguments = if let location {
["build", "--package-path", location.pathString]
} else {
["build"]
}
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder], [TerminalServiceError.unexpected, .output(""), .captured("")])
func task(at location: URL?, throws error: TerminalServiceError) async throws {
// GIVEN
let terminalService = TerminalServiceMock(action: .error(error))
let task = BuildProjectTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(at: location)
}
}
}

View File

@ -19,6 +19,14 @@ final class TemplateServiceMock {
self.spy = spy
}
init(
actions: [Action],
spy: TemplateServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
}
// MARK: - TemplateServicing

View File

@ -19,6 +19,14 @@ final class TerminalServiceMock {
self.spy = spy
}
init(
actions: [Action],
spy: TerminalServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
}
// MARK: - TerminalServicing