2 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
8 changed files with 231 additions and 0 deletions
+1
View File
@@ -11,6 +11,7 @@ struct Colibri: AsyncParsableCommand {
Build.self,
Clean.self,
Create.self,
Open.self,
Outdated.self,
Update.self
],
@@ -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,6 @@
import ArgumentParser
import ColibriLibrary
// MARK: - ExpressibleByArgument
extension IDE: ExpressibleByArgument {}
@@ -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,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,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"
}
}
+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
@@ -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)
}
}
}