// 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) { printError("error: '\(path)' already exists. Pass --force to replace it.\n") exit(1) } catch TemplateInstaller.Error.bundleMissingTemplates(let path) { printError("error: bundled templates not found at \(path).\n") printError(" This usually means the lyrebird binary was built without\n") printError(" its resource bundle. Rebuild with `swift build`.\n") exit(1) } catch TemplateInstaller.Error.fileSystemError(let path, let reason) { printError("error: could not write '\(path)': \(reason)\n") exit(1) } catch { printError("error: \(error)\n") 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) { printError("error: could not remove '\(path)': \(reason)\n") exit(1) } catch { printError("error: \(error)\n") 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 { printError("error: swift not found. Install Xcode or a Swift toolchain.\n") exit(1) } catch CLIError.buildFailed(let code) { printError("error: swift build failed (exit code \(code)). REPL not started.\n") exit(code) } catch { printError("error: \(error)\n") 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 { printError(""" 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 . """) } do { try ProjectScaffolder.scaffold( name: name, template: template, lyrebirdPath: effectivePath ) print("Created \(name)/") } catch ProjectScaffolder.Error.projectExists(let n) { printError("error: directory '\(n)' already exists.\n") exit(1) } catch ProjectScaffolder.Error.invalidName(let n) { printError("error: '\(n)' is not a valid Swift module name.\n") exit(1) } catch ProjectScaffolder.Error.fileSystemError(let path, let reason) { printError("error: could not create '\(path)': \(reason)\n") exit(1) } catch { printError("error: \(error)\n") exit(1) } } private static func runProject(release: Bool) { do { try ProjectRunner.run(release: release) } catch ProjectRunner.Error.swiftNotFound { printError("error: swift not found. Install Xcode or a Swift toolchain.\n") exit(1) } catch ProjectRunner.Error.buildFailed(let code) { exit(code) } catch { printError("error: \(error)\n") 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) { printError("error: '\(n)' is not a valid Swift identifier.\n") exit(1) } catch SynthExporter.Error.fileExists(let p) { printError("error: '\(p)' already exists. Pass --overwrite to replace it.\n") exit(1) } catch SynthExporter.Error.fileSystemError(let p, let reason) { printError("error: could not write '\(p)': \(reason)\n") exit(1) } catch { printError("error: \(error)\n") exit(1) } } private static func printHelp() { print(""" Usage: lyrebird [command] Commands: new [--template ] 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 [--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 Template name (see above) --lyrebird-path Use a local Lyrebird checkout instead of the published remote package URL Run `lyrebird` with no arguments for an interactive shell. """) } }