Files
Lyrebird/Sources/LyrebirdCLI/REPLLauncher.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

116 lines
4.9 KiB
Swift

// 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, located via ``lyrebirdLocateSwift()``
/// (searches `PATH` — honouring swiftly / Homebrew / custom toolchains — then
/// the well-known absolute locations). Throws `CLIError.swiftNotFound` if none
/// is found.
public static func findSwift() throws -> String {
guard let path: String = lyrebirdLocateSwift() else { throw CLIError.swiftNotFound }
return path
}
/// 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()
}
}