6 Commits

Author SHA1 Message Date
javier 888d00c1e8 Set executable, package, and IDE tasks in the Makefile (#10)
This PR contains the work done to implement the necessary tasks in the `Makefile` file to manage the executable file as well as the SPM package dependencies. In addition, some tasks related to IDEs have been implemented.

Reviewed-on: #10
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-20 22:51:47 +00:00
javier fce5a23734 Implemented the Open subcommand. (#9)
This PR contains the work done to implement the `Open` subcommand to the `colibri` executable, which open a *Hummingbird* project with either Visual Studio Code or Xcode.

Reviewed-on: #9
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-19 23:27:21 +00:00
javier ced12d509e Implemented the Clean subcommand (#8)
This PR contains the work done to implement the `Update` subcommand that clean the  *Hummingbird* project.

Reviewed-on: #8
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-19 01:02:32 +00:00
javier ab5f589547 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>
2025-02-19 00:14:00 +00:00
javier 7ee071010d Implemented the Outdated subcommand (#6)
This PR contains the work done to implement the `Outdated` subcommand that check for outdated package dependencies in a *Hummingbird* project.

Reviewed-on: #6
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2025-02-18 23:50:54 +00:00
javier 6a3b9b5141 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>
2025-02-18 23:03:51 +00:00
28 changed files with 788 additions and 13 deletions
+9 -1
View File
@@ -7,7 +7,15 @@ struct Colibri: AsyncParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
abstract: "The utility to manage your Hummingbird apps", abstract: "The utility to manage your Hummingbird apps",
subcommands: [Create.self] subcommands: [
Build.self,
Clean.self,
Create.self,
Open.self,
Outdated.self,
Update.self
],
defaultSubcommand: Create.self
) )
} }
@@ -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)
}
}
}
@@ -0,0 +1,31 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Clean: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "clean-project",
abstract: "Clean a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["clean"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let cleanProject = CleanProjectTask(terminalService: terminalService)
try await cleanProject(at: options.locationURL,
shouldReset: options.reset,
purgeCache: options.purgeCache)
}
}
}
@@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Open: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "open-project",
abstract: "Open a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["open"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let terminalService = TerminalService()
let openProject = OpenProjectTask(terminalService: terminalService)
try await openProject(with: options.ide, at: options.locationURL)
}
}
}
@@ -0,0 +1,29 @@
import ArgumentParser
import ColibriLibrary
extension Colibri {
struct Outdated: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "outdated-dependencies",
abstract: "Check for outdated package dependencies in a Hummingbird app",
helpNames: .shortAndLong,
aliases: ["outdated"]
)
@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, checkOutdated: true)
}
}
}
@@ -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)
}
}
}
@@ -0,0 +1,6 @@
import ArgumentParser
import ColibriLibrary
// MARK: - ExpressibleByArgument
extension IDE: ExpressibleByArgument {}
@@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Build {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}
@@ -0,0 +1,19 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Clean {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Flag(name: .shortAndLong)
var reset: Bool = false
@Flag(name: .shortAndLong)
var purgeCache: Bool = false
@Option(name: .shortAndLong)
var location: String?
}
}
@@ -1,8 +1,8 @@
import ArgumentParser import ArgumentParser
import Foundation import ColibriLibrary
extension Colibri.Create { extension Colibri.Create {
struct Options: ParsableArguments { struct Options: ParsableArguments, Locationable {
// MARK: Properties // MARK: Properties
@@ -11,12 +11,6 @@ extension Colibri.Create {
@Option(name: .shortAndLong) @Option(name: .shortAndLong)
var location: String? var location: String?
// MARK: Computed
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
} }
} }
@@ -0,0 +1,16 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Open {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var ide: IDE = .xcode
@Option(name: .shortAndLong)
var location: String?
}
}
@@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Outdated {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}
@@ -0,0 +1,13 @@
import ArgumentParser
import ColibriLibrary
extension Colibri.Update {
struct Options: ParsableArguments, Locationable {
// MARK: Properties
@Option(name: .shortAndLong)
var location: String?
}
}
@@ -16,7 +16,7 @@ extension URL {
var pathString: String { var pathString: String {
if #available(macOS 13.0, *) { if #available(macOS 13.0, *) {
path(percentEncoded: true) path(percentEncoded: false)
} else { } else {
path path
} }
@@ -0,0 +1,20 @@
public enum IDE: String {
case vscode
case xcode
}
// MARK: - Extension
extension IDE {
// MARK: Functions
static func random() -> IDE {
.allCases.randomElement() ?? .xcode
}
}
// MARK: - CaseIterable
extension IDE: CaseIterable {}
@@ -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) }
}
}
@@ -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)
}
}
@@ -0,0 +1,49 @@
import Foundation
public struct CleanProjectTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(
at location: URL? = nil,
shouldReset: Bool = false,
purgeCache: Bool = false
) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["package"]
if let location {
arguments.append(contentsOf: ["--package-path", location.pathString])
}
arguments.insert("clean", at: 1)
try await terminalService.run(executableURL, arguments: arguments)
if shouldReset {
arguments.remove(at: 1)
arguments.insert("reset", at: 1)
try await terminalService.run(executableURL, arguments: arguments)
}
if purgeCache {
arguments.remove(at: 1)
arguments.insert("purge-cache", at: 1)
try await terminalService.run(executableURL, arguments: arguments)
}
}
}
@@ -0,0 +1,44 @@
import Foundation
public struct OpenProjectTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(with ide: IDE, at location: URL? = nil) async throws (TerminalServiceError) {
let executableURL: URL = switch ide {
case .vscode: .init(at: "/usr/local/bin/code")
case .xcode: .init(at: "/usr/bin/open")
}
let locationPath = switch ide {
case .vscode: location?.pathString ?? "."
case .xcode: location?.appendingPath(.Path.package).pathString ?? .Path.package
}
let arguments: [String] = switch ide {
case .vscode: [locationPath]
case .xcode: ["-a", "Xcode", locationPath]
}
try await terminalService.run(executableURL, arguments: arguments)
}
}
// MARK: - String+Constants
private extension String {
enum Path {
static let package = "Package.swift"
}
}
@@ -0,0 +1,33 @@
import Foundation
public struct UpdateDependenciesTask {
// MARK: Properties
private let terminalService: TerminalServicing
// MARK: Initialisers
public init(terminalService: TerminalServicing) {
self.terminalService = terminalService
}
// MARK: Functions
public func callAsFunction(at location: URL? = nil, checkOutdated: Bool = false) async throws (TerminalServiceError) {
let executableURL = URL(at: "/usr/bin/swift")
var arguments: [String] = ["package", "update"]
if let location {
arguments.append(contentsOf: ["--package-path", location.pathString])
}
if checkOutdated {
arguments.append("--dry-run")
}
try await terminalService.run(executableURL, arguments: arguments)
}
}
+59
View File
@@ -0,0 +1,59 @@
# VARIABLES
BINARY_FOLDER = $(prefix)/bin
BUILD_FOLDER = .build/release
EXECUTABLE_FILE = colibri
# INPUT ARGUMENTS
prefix ?= /usr/local
# EXECUTABLE MANAGEMENT
build: ## Build the executable from source code
@swift build -c release --disable-sandbox
install: build ## Install the built executable into the system
@install -d "$(BINARY_FOLDER)"
@install "$(BUILD_FOLDER)/$(EXECUTABLE_FILE)" "$(BINARY_FOLDER)"
uninstall: ## Uninstall the built executable from the system
@rm -rf "$(BINARY_FOLDER)/$(EXECUTABLE_FILE)"
# PACKAGE MANAGEMENT
clean: ## Delete built SPM artifacts from the package
@swift package clean
outdated: ## List the SPM package dependencies that can be updated
@swift package update --dry-run
purge: ## Purge the global SPM package repository
@swift package purge-cache
reset: ## Reset the complete SPM cache/build folder from the package
@swift package reset
update: ## Update the SPM package dependencies
@swift package update
wipe: clean reset purge ## Wipe all SPM package dependencies and purge the global SPM repository
# OPEN IDEs
vscode: ## Opens this project with Visual Studio Code
@code .
xcode: ## Opens this project with Xcode
@open -a Xcode Package.swift
# HELP
# Output the documentation for each of the defined tasks when `help` is called.
# Reference: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help
help: ## Prints the written documentation for all the defined tasks
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
@@ -24,8 +24,8 @@ struct URL_ExtensionsTests {
// MARK: Computed tests // MARK: Computed tests
@Test(arguments: zip([URL.someFile, .dotFile, .tildeFile, .someURL], @Test(arguments: zip([URL.someFile, .dotFile, .tildeFile, .someEncodedFile, .someURL],
[String.someFilePath, .dotPath, .tildePath, .empty])) [String.someFilePath, .dotPath, .tildePath, .someEncodedPath, .empty]))
func pathString( func pathString(
with url: URL, with url: URL,
expects path: String expects path: String
@@ -63,6 +63,7 @@ private extension String {
static let dotPath = "." static let dotPath = "."
static let empty = "" static let empty = ""
static let tildePath = "~" static let tildePath = "~"
static let someEncodedPath = "/sömê/páth/fîlê"
static let someFilePath = "/some/file/path" static let someFilePath = "/some/file/path"
} }
@@ -70,6 +71,7 @@ private extension String {
private extension URL { private extension URL {
static let dotFile = URL(at: .dotPath) static let dotFile = URL(at: .dotPath)
static let someEncodedFile = URL(at: "/sömê/páth/fîlê")
static let someFile = URL(at: .someFilePath) static let someFile = URL(at: .someFilePath)
static let someURL = URL(string: "https://some.url.path")! static let someURL = URL(string: "https://some.url.path")!
static let tildeFile = URL(at: .tildePath) static let tildeFile = URL(at: .tildePath)
@@ -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)
}
}
}
@@ -0,0 +1,128 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CleanProjectTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder])
func task(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = CleanProjectTask(terminalService: terminalService)
// WHEN
try await task(at: location)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
let arguments = if let location {
["package", "clean", "--package-path", location.pathString]
} else {
["package", "clean"]
}
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithReset(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = CleanProjectTask(terminalService: terminalService)
// WHEN
try await task(at: location, shouldReset: true)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
var arguments = if let location {
["package", "clean", "--package-path", location.pathString]
} else {
["package", "clean"]
}
#expect(terminalService.actions.count == 2)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
arguments.remove(at: 1)
arguments.insert("reset", at: 1)
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithPurgeCache(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = CleanProjectTask(terminalService: terminalService)
// WHEN
try await task(at: location, purgeCache: true)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
var arguments = if let location {
["package", "clean", "--package-path", location.pathString]
} else {
["package", "clean"]
}
#expect(terminalService.actions.count == 2)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
arguments.remove(at: 1)
arguments.insert("purge-cache", at: 1)
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithResetAndPurgeCache(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = CleanProjectTask(terminalService: terminalService)
// WHEN
try await task(at: location, shouldReset: true, purgeCache: true)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
var arguments = if let location {
["package", "clean", "--package-path", location.pathString]
} else {
["package", "clean"]
}
#expect(terminalService.actions.count == 3)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
arguments.remove(at: 1)
arguments.insert("reset", at: 1)
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
arguments.remove(at: 1)
arguments.insert("purge-cache", at: 1)
#expect(terminalService.actions[2] == .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 = CleanProjectTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(at: location, shouldReset: .random(), purgeCache: .random())
}
}
}
@@ -0,0 +1,56 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct OpenProjectTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithVSCodeIDE(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = OpenProjectTask(terminalService: terminalService)
// WHEN
try await task(with: .vscode, at: location)
// THEN
let executableURL = URL(at: "/usr/local/bin/code")
let arguments = [location?.pathString ?? "."]
#expect(terminalService.actions.count == 1)
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
}
@Test(arguments: [nil, URL.someCurrentFolder])
func taskWithXcodeIDE(at location: URL?) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = OpenProjectTask(terminalService: terminalService)
// WHEN
try await task(with: .xcode, at: location)
// THEN
let locationPath = location?.appendingPath("Package.swift").pathString ?? "Package.swift"
let executableURL = URL(at: "/usr/bin/open")
let arguments = ["-a", "Xcode", locationPath]
#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 = OpenProjectTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(with: .random(), at: location)
}
}
}
@@ -0,0 +1,47 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct UpdateDependenciesTaskTests {
@Test(arguments: [nil, URL.someCurrentFolder], [false, true])
func task(at location: URL?, checkOutdated: Bool) async throws {
// GIVEN
let terminalService = TerminalServiceSpy()
let task = UpdateDependenciesTask(terminalService: terminalService)
// WHEN
try await task(at: location, checkOutdated: checkOutdated)
// THEN
let executableURL = URL(at: "/usr/bin/swift")
var arguments = if let location {
["package", "update", "--package-path", location.pathString]
} else {
["package", "update"]
}
if checkOutdated {
arguments.append("--dry-run")
}
#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 = UpdateDependenciesTask(terminalService: terminalService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(at: location, checkOutdated: .random())
}
}
}
@@ -19,6 +19,14 @@ final class TemplateServiceMock {
self.spy = spy self.spy = spy
} }
init(
actions: [Action],
spy: TemplateServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
} }
// MARK: - TemplateServicing // MARK: - TemplateServicing
@@ -19,6 +19,14 @@ final class TerminalServiceMock {
self.spy = spy self.spy = spy
} }
init(
actions: [Action],
spy: TerminalServiceSpy? = nil
) {
self.actions = actions
self.spy = spy
}
} }
// MARK: - TerminalServicing // MARK: - TerminalServicing