Files
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

95 lines
3.5 KiB
Swift

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