// REPLLauncher.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation /// Errors thrown by `REPLLauncher`. public enum CLIError: Error { /// The `swift` binary could not be found on this system. case swiftNotFound /// `swift build` exited non-zero. REPL launch was aborted. case buildFailed(exitCode: Int32) } /// Locates the Swift toolchain, builds the current package, and launches `swift repl` /// with module/library flags pointing at the package's `.build/debug` artifacts so /// `import LyrebirdSynth` works immediately. /// /// The Swift Package Manager's `swift package repl` subcommand was removed from the /// toolchain. The replacement is a two-step invocation: `swift build` to produce the /// package's `.swiftmodule` + library, then `swift repl` with `-I` / `-L` / `-l` flags /// pointing at `.build/debug`. See `docs/tooling/repl.md`. public enum REPLLauncher { /// Arguments passed to `swift` for the build step that precedes the REPL. public static let buildArguments: [String] = ["build"] /// Arguments passed to `swift` to start the REPL. Resolves modules from the package's /// `.build/debug` directory: /// - `-I .build/debug/Modules` — Swift modules (LyrebirdSynth.swiftmodule, ...) /// - `-I .build/debug/LyrebirdC.build` — LyrebirdC C-module modulemap /// - `-L .build/debug -lLyrebird` — link the Lyrebird dylib /// /// Paths are relative; `makeProcess(in:)` sets the working directory so the user can /// run from a project root whose `Package.swift` depends on Lyrebird (or from the /// Lyrebird package directory itself). public static let replArguments: [String] = [ "repl", "-I", ".build/debug/Modules", "-I", ".build/debug/LyrebirdC.build", "-L", ".build/debug", "-lLyrebird" ] /// Path to the shared Lyrebird history file used by the interactive shell /// and other tooling that needs persistent command history. public static var historyFilePath: String { FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent(".lyrebird_history").path } /// Returns the path to the `swift` binary. /// /// Checks `/usr/bin/swift` (Xcode toolchain on macOS) and `/usr/local/bin/swift` /// (standalone toolchain). Throws `CLIError.swiftNotFound` if neither exists. public static func findSwift() throws -> String { let candidates: [String] = ["/usr/bin/swift", "/usr/local/bin/swift"] for path: String in candidates where FileManager.default.isExecutableFile(atPath: path) { return path } throw CLIError.swiftNotFound } /// Builds a `Process` that runs `swift` with the given arguments in `directory`. /// stdin / stdout / stderr inherited from the calling process. public static func makeProcess( arguments: [String], in directory: String = FileManager.default.currentDirectoryPath ) throws -> Process { let swiftPath: String = try findSwift() let process: Process = Process() process.executableURL = URL(fileURLWithPath: swiftPath) process.arguments = arguments process.currentDirectoryURL = URL(fileURLWithPath: directory) return process } /// Builds a `Process` configured to run `swift repl` with module/library flags in `directory`. public static func makeProcess( in directory: String = FileManager.default.currentDirectoryPath ) throws -> Process { try makeProcess(arguments: replArguments, in: directory) } /// Runs `swift build` and waits for it to finish. Throws `CLIError.buildFailed` /// if the build exited non-zero. If the build was killed by a signal, /// `exitCode` is reported as `1` (rather than the raw signal number, which /// would mislead callers that propagate it through `exit(_:)`). public static func build( in directory: String = FileManager.default.currentDirectoryPath ) throws { let process: Process = try makeProcess(arguments: buildArguments, in: directory) try process.run() process.waitUntilExit() if process.terminationReason != .exit { throw CLIError.buildFailed(exitCode: 1) } if process.terminationStatus != 0 { throw CLIError.buildFailed(exitCode: process.terminationStatus) } } /// Builds the package, then launches `swift repl` with Lyrebird available. /// Blocks until the REPL session ends. /// /// - Parameter directory: Working directory for the SPM + REPL invocation. /// Defaults to the current directory. Must contain a `Package.swift`. public static func launch( in directory: String = FileManager.default.currentDirectoryPath ) throws { try build(in: directory) let process: Process = try makeProcess(in: directory) try process.run() process.waitUntilExit() } }