Files
Lyrebird/Sources/LyrebirdCLI/LyrebirdCLI.swift
T
2026-06-01 18:27:40 -07:00

199 lines
7.5 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
}
do {
try ProjectScaffolder.scaffold(
name: name,
template: template,
lyrebirdPath: resolvedPath
)
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 Swift Package
Templates: basic (default), demo, ui-demo, nrt
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
Run `lyrebird` with no arguments for an interactive shell.
""")
}
}