// ProjectRunner.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation // MARK: - ProjectRunner /// Wraps `swift run` to build and launch the current Lyrebird project. /// /// `lyrebird run` and `lyrebird run --release` are thin wrappers: they locate the /// `swift` binary and exec `swift run` (optionally with `-c release`) in the current /// working directory. stdin / stdout / stderr are inherited so compiler output and /// runtime output appear directly in the user's terminal. /// /// The runner does **not** verify that a `Package.swift` is present before launching — /// SPM provides a clear error message in that case. public enum ProjectRunner { // MARK: - Errors /// Errors thrown by `ProjectRunner`. public enum Error: Swift.Error, Equatable { /// The `swift` binary could not be found on this system. case swiftNotFound /// The process exited with a non-zero status. case buildFailed(exitCode: Int32) } // MARK: - Public API /// Build and run the Swift package in `directory`. /// /// - Parameters: /// - release: When `true`, passes `-c release` to `swift run` for an optimised build. /// - directory: The directory containing `Package.swift`. /// Defaults to the current working directory. /// - Throws: `ProjectRunner.Error.swiftNotFound` if the toolchain is absent; /// `ProjectRunner.Error.buildFailed` if `swift run` exits non-zero. public static func run( release: Bool = false, in directory: String = FileManager.default.currentDirectoryPath ) throws { let process: Process = try makeProcess(release: release, in: directory) try process.run() process.waitUntilExit() let exitCode: Int32 = process.terminationStatus guard exitCode == 0 else { throw Error.buildFailed(exitCode: exitCode) } } // MARK: - Internal /// Builds a `Process` configured to run `swift run` (or `swift run -c release`). /// /// Exposed for testing so callers can inspect the process configuration without /// actually launching a build. /// /// - Parameters: /// - release: When `true`, appends `-c release` to the swift arguments. /// - directory: Working directory for the SPM invocation. /// - Throws: `ProjectRunner.Error.swiftNotFound` if the toolchain is absent. public static func makeProcess( release: Bool = false, in directory: String = FileManager.default.currentDirectoryPath ) throws -> Process { let swiftPath: String = try findSwift() var arguments: [String] = ["run"] if release { arguments += ["-c", "release"] } let process: Process = Process() process.executableURL = URL(fileURLWithPath: swiftPath) process.arguments = arguments process.currentDirectoryURL = URL(fileURLWithPath: directory) // stdin / stdout / stderr intentionally not set — inherited for interactive output. return process } // MARK: - Helpers /// Returns the path to the `swift` binary. /// /// Checks, in order: /// - `/usr/bin/swift` — Xcode Command Line Tools / Xcode toolchain /// - `/usr/local/bin/swift` — standalone toolchain (Intel Mac default) /// - `/opt/homebrew/bin/swift` — Homebrew toolchain on Apple Silicon /// /// Throws `ProjectRunner.Error.swiftNotFound` if none of the candidates exist. static func findSwift() throws -> String { let candidates: [String] = ["/usr/bin/swift", "/usr/local/bin/swift", "/opt/homebrew/bin/swift"] for path: String in candidates where FileManager.default.isExecutableFile(atPath: path) { return path } throw Error.swiftNotFound } }