Files
Lyrebird/Sources/LyrebirdCLI/LyrebirdCLI.swift
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

224 lines
8.9 KiB
Swift

// LyrebirdCLI.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Entry point for the `lyrebird` command-line tool.
///
/// Uses `@main` (not `main.swift`) so the module can be `@testable import`ed by
/// `LyrebirdCLITests` without triggering top-level code execution.
@main
struct LyrebirdCLIMain {
static func main() {
let command: CLICommand = CLICommand.parse(CommandLine.arguments)
switch command {
case .repl:
runREPL()
case .interactive:
print(welcomeBanner)
print()
InteractiveShell.run()
case .version:
print("lyrebird \(lyrebirdVersion)")
case .help:
printHelp()
case .new(let name, let template, let lyrebirdPath):
runNew(name: name, template: template, lyrebirdPath: lyrebirdPath)
case .run(let release):
runProject(release: release)
case .export(let name, let toPath, let overwrite):
runExport(name: name, to: toPath, overwrite: overwrite)
case .installTemplates(let force):
runInstallTemplates(force: force)
case .uninstallTemplates:
runUninstallTemplates()
}
}
private static func runInstallTemplates(force: Bool) {
do {
let count: Int = try TemplateInstaller.install(force: force)
print("Installed \(count) Lyrebird Xcode templates →")
print(" \(TemplateInstaller.destinationURL.path)")
print("")
print("Open Xcode → File → New → File from Template…")
print("→ scroll to the 'Lyrebird' section.")
} catch TemplateInstaller.Error.destinationExists(let path) {
fputs("error: '\(path)' already exists. Pass --force to replace it.\n", stderr)
exit(1)
} catch TemplateInstaller.Error.bundleMissingTemplates(let path) {
fputs("error: bundled templates not found at \(path).\n", stderr)
fputs(" This usually means the lyrebird binary was built without\n", stderr)
fputs(" its resource bundle. Rebuild with `swift build`.\n", stderr)
exit(1)
} catch TemplateInstaller.Error.fileSystemError(let path, let reason) {
fputs("error: could not write '\(path)': \(reason)\n", stderr)
exit(1)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func runUninstallTemplates() {
do {
let removed: Bool = try TemplateInstaller.uninstall()
if removed {
print("Removed Lyrebird templates from")
print(" \(TemplateInstaller.destinationURL.path)")
} else {
print("Nothing to remove. Templates not installed at")
print(" \(TemplateInstaller.destinationURL.path)")
}
} catch TemplateInstaller.Error.fileSystemError(let path, let reason) {
fputs("error: could not remove '\(path)': \(reason)\n", stderr)
exit(1)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func runREPL() {
print("""
Lyrebird \(lyrebirdVersion) — building package, then starting Swift REPL
Run `import LyrebirdSynth` to access the framework.
Type :help for Swift REPL help. Ctrl+D to exit.
""")
do {
try REPLLauncher.launch()
} catch CLIError.swiftNotFound {
fputs("error: swift not found. Install Xcode or a Swift toolchain.\n", stderr)
exit(1)
} catch CLIError.buildFailed(let code) {
fputs("error: swift build failed (exit code \(code)). REPL not started.\n", stderr)
exit(code)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func runNew(
name: String,
template: ProjectTemplate,
lyrebirdPath: String?
) {
let resolvedPath: String?
if let p: String = lyrebirdPath {
// Resolve to absolute. `URL(fileURLWithPath:)` keeps absolute as-is
// and roots relative paths at the current working directory.
resolvedPath = URL(fileURLWithPath: p).standardizedFileURL.path
} else {
resolvedPath = nil
}
// Report which Lyrebird dependency the generated Package.swift will pin.
let effectivePath: String? = resolvedPath ?? ProjectScaffolder.defaultLyrebirdCheckoutPath
if let path: String = effectivePath {
print("Lyrebird dependency: local checkout at \(path)")
} else {
fputs("""
warning: no local Lyrebird checkout found; pinning published \
\(ProjectScaffolder.lyrebirdPackageURL)@\(ProjectScaffolder.lyrebirdMinimumVersion). \
The generated template may use API newer than that release — if it \
fails to build, re-run with --lyrebird-path <checkout>.
""", stderr)
}
do {
try ProjectScaffolder.scaffold(
name: name,
template: template,
lyrebirdPath: effectivePath
)
print("Created \(name)/")
} catch ProjectScaffolder.Error.projectExists(let n) {
fputs("error: directory '\(n)' already exists.\n", stderr)
exit(1)
} catch ProjectScaffolder.Error.invalidName(let n) {
fputs("error: '\(n)' is not a valid Swift module name.\n", stderr)
exit(1)
} catch ProjectScaffolder.Error.fileSystemError(let path, let reason) {
fputs("error: could not create '\(path)': \(reason)\n", stderr)
exit(1)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func runProject(release: Bool) {
do {
try ProjectRunner.run(release: release)
} catch ProjectRunner.Error.swiftNotFound {
fputs("error: swift not found. Install Xcode or a Swift toolchain.\n", stderr)
exit(1)
} catch ProjectRunner.Error.buildFailed(let code) {
exit(code)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func runExport(
name: String,
to path: String?,
overwrite: Bool
) {
do {
try SynthExporter.export(name: name, to: path, overwrite: overwrite)
let dest: String = path ?? SynthExporter.defaultOutputPath(for: name)
print("Exported \(dest)")
} catch SynthExporter.Error.invalidName(let n) {
fputs("error: '\(n)' is not a valid Swift identifier.\n", stderr)
exit(1)
} catch SynthExporter.Error.fileExists(let p) {
fputs("error: '\(p)' already exists. Pass --overwrite to replace it.\n", stderr)
exit(1)
} catch SynthExporter.Error.fileSystemError(let p, let reason) {
fputs("error: could not write '\(p)': \(reason)\n", stderr)
exit(1)
} catch {
fputs("error: \(error)\n", stderr)
exit(1)
}
}
private static func printHelp() {
print("""
Usage: lyrebird [command]
Commands:
new <name> [--template <t>] Scaffold a new Lyrebird project
SPM templates (open Package.swift in Xcode):
quickstart fastest path to sound (plays 10 s)
basic (default), demo, ui-demo, nrt, score
Xcode-project templates (build to .app or .appex):
macos-ui-xcode macOS @UIParam synth app
ios-ui iOS @UIParam synth app
auv3 AUv3 host + .appex
run [--release] Build and run the current project
export <name> [--to p] Export a Synthesizer template to a .swift file
install-templates [--force] Install Xcode file templates (Generator,
Synthesizer, Task, …) to the user template dir
uninstall-templates Remove the installed Xcode file templates
repl Launch Swift REPL with Lyrebird available
version Print lyrebird version
help Show this help
Flags for `new`:
--template <t> Template name (see above)
--lyrebird-path <path> Use a local Lyrebird checkout instead of the
published remote package URL
Run `lyrebird` with no arguments for an interactive shell.
""")
}
}