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

137 lines
4.4 KiB
Swift

// SynthExporter.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
// MARK: - SynthExporter
/// Exports a named `Synthesizer` subclass template to a Swift source file.
///
/// `lyrebird export <SynthName> [--to <path>]` generates a starter `.swift` file
/// containing a concrete `SynthesizerBase` subclass with the given name. The
/// generated file is ready to drop into any Lyrebird project's `Sources/` directory.
///
/// If `--to` is omitted, the file is written to `<SynthName>.swift` in the current
/// working directory.
///
/// ## Generated file layout
///
/// ```
/// import LyrebirdSynth
///
/// final class MySynth: SynthesizerBase {
/// // MARK: - Parameters
/// ...
/// override func build() {
/// ...
/// }
/// }
/// ```
public enum SynthExporter {
// MARK: - Errors
/// Errors thrown by `SynthExporter`.
public enum Error: Swift.Error, Equatable {
/// The synthesizer name is empty or not a valid Swift identifier.
case invalidName(name: String)
/// A file could not be written.
case fileSystemError(path: String, reason: String)
/// A file already exists at the output path and would be overwritten.
case fileExists(path: String)
}
// MARK: - Public API
/// Export a Synthesizer template named `name` to `outputPath`.
///
/// - Parameters:
/// - name: The Swift class name for the generated Synthesizer.
/// Must be a valid, non-empty Swift identifier.
/// - outputPath: Destination `.swift` file path.
/// Defaults to `<name>.swift` in the current working directory.
/// - overwrite: When `false` (default), throws `Error.fileExists` if the path
/// is already occupied. Pass `true` to replace an existing file.
/// - Throws: `SynthExporter.Error` on name validation failure or file system errors.
public static func export(
name: String,
to outputPath: String? = nil,
overwrite: Bool = false
) throws {
try validateName(name)
let resolvedPath: String = outputPath ?? defaultOutputPath(for: name)
if !overwrite && FileManager.default.fileExists(atPath: resolvedPath) {
throw Error.fileExists(path: resolvedPath)
}
let source: String = synthTemplate(name: name)
do {
try source.write(toFile: resolvedPath, atomically: true, encoding: .utf8)
} catch {
throw Error.fileSystemError(path: resolvedPath,
reason: error.localizedDescription)
}
}
// MARK: - Template
/// The Swift source template for a new Synthesizer named `name`.
public static func synthTemplate(name: String) -> String {
"""
import LyrebirdSynth
// MARK: - \(name)
/// A Lyrebird synthesizer.
///
/// Add and wire generators in `build()`, then add this to a running engine:
///
/// ```swift
/// let synth = \(name)()
/// try engine.add(synth)
/// ```
final class \(name): SynthesizerBase {
// MARK: - Parameters
// Declare parameters here. Example:
// let freq: Double = 440.0
// let amp: Double = 0.5
// MARK: - Build
override func build() {
// Wire your signal graph here. Example:
// let osc = OscSin.Audio(freq: freq)
// let env = EnvelopeGen(envelope: Envelope(
// segmentCount: 2,
// levels: [0.0, 1.0, 0.0],
// durations: [0.01, 0.5]
// ))
// addOutputGenerator(osc * env)
}
}
"""
}
// MARK: - Helpers
/// Returns the default output file path: `<name>.swift` in the current directory.
static func defaultOutputPath(for name: String) -> String {
let cwd: String = FileManager.default.currentDirectoryPath
return (cwd as NSString).appendingPathComponent("\(name).swift")
}
/// Validate that `name` is a non-empty Swift-compatible identifier.
///
/// Allows letters, digits, and underscores; must not start with a digit.
static func validateName(_ name: String) throws {
try validateSwiftIdentifier(name, invalidNameError: Error.invalidName(name: name))
}
}