// ProjectScaffolder+ShoeGazeIOS.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation // MARK: - shoegaze-ios template extension ProjectScaffolder { /// Write the full `shoegaze-ios` template tree at `projectRoot`. /// /// Single iOS application target. SwiftUI `@main` entry point wired to /// `iOSAudioDriver` via `AVAudioSession`. Lyrebird + LyrebirdUI + /// LyrebirdShoeGaze + LyrebirdShoeGazeUI pulled in via SPM dependencies. /// A default board (Overdrive → Delay → Reverb → Modulation) fed by the /// built-in test loop is constructed at launch inside an `@Observable` /// `AppModel`. static func writeShoeGazeIOSFiles( name: String, projectRoot: String, lyrebirdPath: String?, shoegazePath: String?, fm: FileManager ) throws { let appGroupDir: String = (projectRoot as NSString).appendingPathComponent(name) let xcodeprojDir: String = (projectRoot as NSString).appendingPathComponent("\(name).xcodeproj") let schemeDir: String = (xcodeprojDir as NSString) .appendingPathComponent("xcshareddata/xcschemes") for dir: String in [appGroupDir, xcodeprojDir, schemeDir] { do { try fm.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil) } catch { try? fm.removeItem(atPath: projectRoot) throw Error.fileSystemError(path: dir, reason: error.localizedDescription) } } let files: [(String, String)] = [ ((appGroupDir as NSString).appendingPathComponent("Info.plist"), iOSAppInfoPlist(name: name)), ((appGroupDir as NSString).appendingPathComponent("\(name)App.swift"), shoegazeIOSAppSwift(name: name)), ((appGroupDir as NSString).appendingPathComponent("\(name)AppModel.swift"), shoegazeIOSAppModelSwift(name: name)), ((xcodeprojDir as NSString).appendingPathComponent("project.pbxproj"), shoegazeIOSPBXProj(name: name, lyrebirdPath: lyrebirdPath, shoegazePath: shoegazePath)), ((schemeDir as NSString).appendingPathComponent("\(name).xcscheme"), appScheme(name: name, productExtension: "app")), ] try writeFiles(files, fm: fm, projectRoot: projectRoot) } // MARK: - Source files /// `App.swift` — SwiftUI `@main` entry point for the shoegaze-ios template. /// /// Emitted source must not contain the Lyrebird MIT copyright header. static func shoegazeIOSAppSwift(name: String) -> String { """ // \(name) — entry point import SwiftUI import LyrebirdShoeGazeUI @main struct \(name)App: App { @State private var model: \(name)AppModel = \(name)AppModel() var body: some Scene { WindowGroup { NavigationStack { ScrollView { ShoeGazeView(shoeGaze: model.shoeGaze) .padding(.horizontal, 12) } .navigationTitle("\(name)") } .onAppear { model.start() } .onDisappear { model.stop() } } } } """ } /// `AppModel.swift` — `@Observable` model owning the engine and `ShoeGaze`. /// /// Emitted source must not contain the Lyrebird MIT copyright header. static func shoegazeIOSAppModelSwift(name: String) -> String { let blockSize: Int = scaffoldDefaultBlockSize let outputChannels: Int = scaffoldDefaultOutputChannels return """ // \(name)AppModel.swift import AVFoundation import Observation import LyrebirdSynth import LyrebirdShoeGaze /// Owns the `SynthesisEngine` and `ShoeGaze` manager for \(name). /// /// Call `start()` when the root view appears and `stop()` when it disappears. /// The engine uses the hardware sample rate from `AVAudioSession` so pitch /// and timing are accurate on all iOS devices. @Observable @MainActor final class \(name)AppModel { private(set) var shoeGaze: ShoeGaze private let engine: SynthesisEngine init() { // Activate the session before querying `sampleRate` so the // hardware rate is known (iOS locks to 48 000 Hz on most devices // regardless of the preferred rate). let session: AVAudioSession = AVAudioSession.sharedInstance() try? session.setCategory(.playback, mode: .default) try? session.setActive(true) let sampleRate: Double = session.sampleRate let eng: SynthesisEngine = SynthesisEngine( blockSize: \(blockSize), sampleRate: sampleRate, numOutputChannels: \(outputChannels), numInputChannels: 0, driver: iOSAudioDriver() ) self.engine = eng self.shoeGaze = ShoeGaze(engine: eng) do { let board: PedalBoard = try shoeGaze.addBoard() try board.add(OverdrivePedal()) try board.add(DelayPedal()) try board.add(ReverbPedal()) try board.add(ModulationPedal()) try shoeGaze.finalizeBoard(board) } catch { print("[\(name)] board build failed: \\(error)") } } /// Start the audio engine. Call from `.onAppear`. func start() { do { try engine.start() } catch { print("[\(name)] engine.start failed: \\(error)") } } /// Stop the audio engine. Call from `.onDisappear`. func stop() { engine.stop() } } """ } // MARK: - pbxproj /// `project.pbxproj` for the `shoegaze-ios` template. /// /// Identical in structure to the `ios-ui` pbxproj but: /// - Declares two source files (`App.swift` + `AppModel.swift`) /// using the `sg`-prefixed UUID constants so they don't collide with the /// `ios-ui` constants. /// - References three SPM products: `LyrebirdUI`, `LyrebirdShoeGaze`, and /// `LyrebirdShoeGazeUI` — the last two from a second package reference /// pointing at the ShoeGaze package. static func shoegazeIOSPBXProj( name: String, lyrebirdPath: String? = nil, shoegazePath: String? = nil ) -> String { let bundleID: String = "\(defaultBundleIdentifierPrefix).\(name)" let lyrebirdRefLabel: String = pbxPackageReferenceLabel(lyrebirdPath: lyrebirdPath) let lyrebirdRefSection: String = pbxPackageReferenceSection(lyrebirdPath: lyrebirdPath) let shoegazeRefLabel: String = shoegazePackageReferenceLabel(shoegazePath: shoegazePath) let shoegazeRefSection: String = shoegazePackageReferenceSection(shoegazePath: shoegazePath) return """ // !$*UTF8*$! { \tarchiveVersion = 1; \tclasses = { \t}; \tobjectVersion = 60; \tobjects = { /* Begin PBXBuildFile section */ \t\t\(pbxSGAppFileBuild) /* \(name)App.swift in Sources */ = {isa = PBXBuildFile; fileRef = \(pbxSGAppFileRef) /* \(name)App.swift */; }; \t\t\(pbxSGAppModelBuild) /* \(name)AppModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = \(pbxSGAppModelRef) /* \(name)AppModel.swift */; }; \t\t\(pbxLyrebirdUIFrameworkBuild) /* LyrebirdUI in Frameworks */ = {isa = PBXBuildFile; productRef = \(pbxLyrebirdUIProductDep) /* LyrebirdUI */; }; \t\t\(pbxSGShoeGazeBuild) /* LyrebirdShoeGaze in Frameworks */ = {isa = PBXBuildFile; productRef = \(pbxSGShoeGazeDep) /* LyrebirdShoeGaze */; }; \t\t\(pbxSGShoeGazeUIBuild) /* LyrebirdShoeGazeUI in Frameworks */ = {isa = PBXBuildFile; productRef = \(pbxSGShoeGazeUIDep) /* LyrebirdShoeGazeUI */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ \t\t\(pbxAppProductRef) /* \(name).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = \(name).app; sourceTree = BUILT_PRODUCTS_DIR; }; \t\t\(pbxInfoPlistRef) /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; \t\t\(pbxSGAppFileRef) /* \(name)App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = \(name)App.swift; sourceTree = ""; }; \t\t\(pbxSGAppModelRef) /* \(name)AppModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = \(name)AppModel.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ \t\t\(pbxFrameworksPhase) /* Frameworks */ = { \t\t\tisa = PBXFrameworksBuildPhase; \t\t\tbuildActionMask = \(pbxBuildActionMask); \t\t\tfiles = ( \t\t\t\t\(pbxLyrebirdUIFrameworkBuild) /* LyrebirdUI in Frameworks */, \t\t\t\t\(pbxSGShoeGazeBuild) /* LyrebirdShoeGaze in Frameworks */, \t\t\t\t\(pbxSGShoeGazeUIBuild) /* LyrebirdShoeGazeUI in Frameworks */, \t\t\t); \t\t\trunOnlyForDeploymentPostprocessing = 0; \t\t}; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ \t\t\(pbxMainGroup) = { \t\t\tisa = PBXGroup; \t\t\tchildren = ( \t\t\t\t\(pbxAppGroup) /* \(name) */, \t\t\t\t\(pbxProductsGroup) /* Products */, \t\t\t); \t\t\tsourceTree = ""; \t\t}; \t\t\(pbxProductsGroup) /* Products */ = { \t\t\tisa = PBXGroup; \t\t\tchildren = ( \t\t\t\t\(pbxAppProductRef) /* \(name).app */, \t\t\t); \t\t\tname = Products; \t\t\tsourceTree = ""; \t\t}; \t\t\(pbxAppGroup) /* \(name) */ = { \t\t\tisa = PBXGroup; \t\t\tchildren = ( \t\t\t\t\(pbxSGAppFileRef) /* \(name)App.swift */, \t\t\t\t\(pbxSGAppModelRef) /* \(name)AppModel.swift */, \t\t\t\t\(pbxInfoPlistRef) /* Info.plist */, \t\t\t); \t\t\tpath = \(name); \t\t\tsourceTree = ""; \t\t}; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ \t\t\(pbxBlueprintIDApp) /* \(name) */ = { \t\t\tisa = PBXNativeTarget; \t\t\tbuildConfigurationList = \(pbxAppBuildConfigList) /* Build configuration list for PBXNativeTarget "\(name)" */; \t\t\tbuildPhases = ( \t\t\t\t\(pbxSourcesPhase) /* Sources */, \t\t\t\t\(pbxFrameworksPhase) /* Frameworks */, \t\t\t\t\(pbxResourcesPhase) /* Resources */, \t\t\t); \t\t\tbuildRules = ( \t\t\t); \t\t\tdependencies = ( \t\t\t); \t\t\tname = \(name); \t\t\tpackageProductDependencies = ( \t\t\t\t\(pbxLyrebirdUIProductDep) /* LyrebirdUI */, \t\t\t\t\(pbxSGShoeGazeDep) /* LyrebirdShoeGaze */, \t\t\t\t\(pbxSGShoeGazeUIDep) /* LyrebirdShoeGazeUI */, \t\t\t); \t\t\tproductName = \(name); \t\t\tproductReference = \(pbxAppProductRef) /* \(name).app */; \t\t\tproductType = "com.apple.product-type.application"; \t\t}; /* End PBXNativeTarget section */ /* Begin PBXProject section */ \t\t\(pbxRootObject) /* Project object */ = { \t\t\tisa = PBXProject; \t\t\tattributes = { \t\t\t\tBuildIndependentTargetsInParallel = 1; \t\t\t\tLastSwiftUpdateCheck = 1600; \t\t\t\tLastUpgradeCheck = 1600; \t\t\t\tTargetAttributes = { \t\t\t\t\t\(pbxBlueprintIDApp) = { \t\t\t\t\t\tCreatedOnToolsVersion = 16.0; \t\t\t\t\t}; \t\t\t\t}; \t\t\t}; \t\t\tbuildConfigurationList = \(pbxProjectBuildConfigList) /* Build configuration list for PBXProject "\(name)" */; \t\t\tcompatibilityVersion = "Xcode 14.0"; \t\t\tdevelopmentRegion = en; \t\t\thasScannedForEncodings = 0; \t\t\tknownRegions = ( \t\t\t\ten, \t\t\t\tBase, \t\t\t); \t\t\tmainGroup = \(pbxMainGroup); \t\t\tpackageReferences = ( \t\t\t\t\(pbxRemoteSPMRef) /* \(lyrebirdRefLabel) */, \t\t\t\t\(pbxSGShoeGazePackageRef) /* \(shoegazeRefLabel) */, \t\t\t); \t\t\tproductRefGroup = \(pbxProductsGroup) /* Products */; \t\t\tprojectDirPath = ""; \t\t\tprojectRoot = ""; \t\t\ttargets = ( \t\t\t\t\(pbxBlueprintIDApp) /* \(name) */, \t\t\t); \t\t}; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ \t\t\(pbxResourcesPhase) /* Resources */ = { \t\t\tisa = PBXResourcesBuildPhase; \t\t\tbuildActionMask = \(pbxBuildActionMask); \t\t\tfiles = ( \t\t\t); \t\t\trunOnlyForDeploymentPostprocessing = 0; \t\t}; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ \t\t\(pbxSourcesPhase) /* Sources */ = { \t\t\tisa = PBXSourcesBuildPhase; \t\t\tbuildActionMask = \(pbxBuildActionMask); \t\t\tfiles = ( \t\t\t\t\(pbxSGAppFileBuild) /* \(name)App.swift in Sources */, \t\t\t\t\(pbxSGAppModelBuild) /* \(name)AppModel.swift in Sources */, \t\t\t); \t\t\trunOnlyForDeploymentPostprocessing = 0; \t\t}; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ \t\t\(pbxAppDebugConfig) /* Debug */ = { \t\t\tisa = XCBuildConfiguration; \t\t\tbuildSettings = { \t\t\t\tCODE_SIGN_STYLE = Automatic; \t\t\t\tDEBUG_INFORMATION_FORMAT = dwarf; \t\t\t\tGENERATE_INFOPLIST_FILE = NO; \t\t\t\tINFOPLIST_FILE = \(name)/Info.plist; \t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = 18.0; \t\t\t\tLD_RUNPATH_SEARCH_PATHS = ( \t\t\t\t\t"$(inherited)", \t\t\t\t\t"@executable_path/Frameworks", \t\t\t\t); \t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = \(bundleID); \t\t\t\tPRODUCT_NAME = "$(TARGET_NAME)"; \t\t\t\tSWIFT_VERSION = 6.0; \t\t\t\tTARGETED_DEVICE_FAMILY = "1,2"; \t\t\t}; \t\t\tname = Debug; \t\t}; \t\t\(pbxAppReleaseConfig) /* Release */ = { \t\t\tisa = XCBuildConfiguration; \t\t\tbuildSettings = { \t\t\t\tCODE_SIGN_STYLE = Automatic; \t\t\t\tDEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; \t\t\t\tGENERATE_INFOPLIST_FILE = NO; \t\t\t\tINFOPLIST_FILE = \(name)/Info.plist; \t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = 18.0; \t\t\t\tLD_RUNPATH_SEARCH_PATHS = ( \t\t\t\t\t"$(inherited)", \t\t\t\t\t"@executable_path/Frameworks", \t\t\t\t); \t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = \(bundleID); \t\t\t\tPRODUCT_NAME = "$(TARGET_NAME)"; \t\t\t\tSWIFT_VERSION = 6.0; \t\t\t\tTARGETED_DEVICE_FAMILY = "1,2"; \t\t\t}; \t\t\tname = Release; \t\t}; \t\t\(pbxProjectDebugConfig) /* Debug */ = { \t\t\tisa = XCBuildConfiguration; \t\t\tbuildSettings = { \t\t\t\tALWAYS_SEARCH_USER_PATHS = NO; \t\t\t\tCLANG_ENABLE_OBJC_ARC = YES; \t\t\t\tCOPY_PHASE_STRIP = NO; \t\t\t\tDEBUG_INFORMATION_FORMAT = dwarf; \t\t\t\tENABLE_STRICT_OBJC_MSGSEND = YES; \t\t\t\tENABLE_TESTABILITY = YES; \t\t\t\tENABLE_USER_SCRIPT_SANDBOXING = YES; \t\t\t\tGCC_C_LANGUAGE_STANDARD = gnu17; \t\t\t\tGCC_NO_COMMON_BLOCKS = YES; \t\t\t\tGCC_OPTIMIZATION_LEVEL = 0; \t\t\t\tONLY_ACTIVE_ARCH = YES; \t\t\t\tSDKROOT = iphoneos; \t\t\t\tSWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; \t\t\t\tSWIFT_OPTIMIZATION_LEVEL = "-Onone"; \t\t\t}; \t\t\tname = Debug; \t\t}; \t\t\(pbxProjectReleaseConfig) /* Release */ = { \t\t\tisa = XCBuildConfiguration; \t\t\tbuildSettings = { \t\t\t\tALWAYS_SEARCH_USER_PATHS = NO; \t\t\t\tCLANG_ENABLE_OBJC_ARC = YES; \t\t\t\tCOPY_PHASE_STRIP = NO; \t\t\t\tDEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; \t\t\t\tENABLE_NS_ASSERTIONS = NO; \t\t\t\tENABLE_STRICT_OBJC_MSGSEND = YES; \t\t\t\tENABLE_USER_SCRIPT_SANDBOXING = YES; \t\t\t\tGCC_C_LANGUAGE_STANDARD = gnu17; \t\t\t\tGCC_NO_COMMON_BLOCKS = YES; \t\t\t\tSDKROOT = iphoneos; \t\t\t\tSWIFT_COMPILATION_MODE = wholemodule; \t\t\t\tSWIFT_OPTIMIZATION_LEVEL = "-O"; \t\t\t}; \t\t\tname = Release; \t\t}; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ \t\t\(pbxAppBuildConfigList) /* Build configuration list for PBXNativeTarget "\(name)" */ = { \t\t\tisa = XCConfigurationList; \t\t\tbuildConfigurations = ( \t\t\t\t\(pbxAppDebugConfig) /* Debug */, \t\t\t\t\(pbxAppReleaseConfig) /* Release */, \t\t\t); \t\t\tdefaultConfigurationIsVisible = 0; \t\t\tdefaultConfigurationName = Release; \t\t}; \t\t\(pbxProjectBuildConfigList) /* Build configuration list for PBXProject "\(name)" */ = { \t\t\tisa = XCConfigurationList; \t\t\tbuildConfigurations = ( \t\t\t\t\(pbxProjectDebugConfig) /* Debug */, \t\t\t\t\(pbxProjectReleaseConfig) /* Release */, \t\t\t); \t\t\tdefaultConfigurationIsVisible = 0; \t\t\tdefaultConfigurationName = Release; \t\t}; /* End XCConfigurationList section */ \(lyrebirdRefSection) \(shoegazeRefSection) /* Begin XCSwiftPackageProductDependency section */ \t\t\(pbxLyrebirdUIProductDep) /* LyrebirdUI */ = { \t\t\tisa = XCSwiftPackageProductDependency; \t\t\tpackage = \(pbxRemoteSPMRef) /* \(lyrebirdRefLabel) */; \t\t\tproductName = LyrebirdUI; \t\t}; \t\t\(pbxSGShoeGazeDep) /* LyrebirdShoeGaze */ = { \t\t\tisa = XCSwiftPackageProductDependency; \t\t\tpackage = \(pbxSGShoeGazePackageRef) /* \(shoegazeRefLabel) */; \t\t\tproductName = LyrebirdShoeGaze; \t\t}; \t\t\(pbxSGShoeGazeUIDep) /* LyrebirdShoeGazeUI */ = { \t\t\tisa = XCSwiftPackageProductDependency; \t\t\tpackage = \(pbxSGShoeGazePackageRef) /* \(shoegazeRefLabel) */; \t\t\tproductName = LyrebirdShoeGazeUI; \t\t}; /* End XCSwiftPackageProductDependency section */ \t}; \trootObject = \(pbxRootObject) /* Project object */; } """ } // MARK: - ShoeGaze package reference helpers /// pbxproj snippet for the ShoeGaze SPM package reference. Emits either an /// `XCRemoteSwiftPackageReference` (when `shoegazePath == nil`) or an /// `XCLocalSwiftPackageReference` pointing at the absolute path. static func shoegazePackageReferenceSection(shoegazePath: String?) -> String { if let path: String = shoegazePath { let escaped: String = pbxQuoteEscape(path) return """ /* Begin XCLocalSwiftPackageReference section */ \t\t\(pbxSGShoeGazePackageRef) /* XCLocalSwiftPackageReference "shoegaze" */ = { \t\t\tisa = XCLocalSwiftPackageReference; \t\t\trelativePath = "\(escaped)"; \t\t}; /* End XCLocalSwiftPackageReference section */ """ } else { return """ /* Begin XCRemoteSwiftPackageReference section */ \t\t\(pbxSGShoeGazePackageRef) /* XCRemoteSwiftPackageReference "shoegaze" */ = { \t\t\tisa = XCRemoteSwiftPackageReference; \t\t\trepositoryURL = "\(shoegazePackageURL)"; \t\t\trequirement = { \t\t\t\tkind = upToNextMajorVersion; \t\t\t\tminimumVersion = \(shoegazeMinimumVersion); \t\t\t}; \t\t}; /* End XCRemoteSwiftPackageReference section */ """ } } /// Comment label used in pbxproj for the ShoeGaze package reference. static func shoegazePackageReferenceLabel(shoegazePath: String?) -> String { shoegazePath == nil ? #"XCRemoteSwiftPackageReference "shoegaze""# : #"XCLocalSwiftPackageReference "shoegaze""# } // MARK: - shoegaze-ios PBX UUID constants // // All UUIDs in the `0x03xx` range — no collision with ios-ui (`0x02xx`), // macos-ui-xcode (`0x01xx`), or auv3 (`0x03Axx` / `0x03xx` note: auv3 // uses `0x03A0`–`0x03A8`, so shoegaze-ios uses `0x03B0`–`0x03B8`). /// `App.swift` file reference UUID. static let pbxSGAppFileRef: String = "AABB000000000000000003B0" /// `App.swift` build file UUID. static let pbxSGAppFileBuild: String = "AABB000000000000000003B1" /// `AppModel.swift` file reference UUID. static let pbxSGAppModelRef: String = "AABB000000000000000003B2" /// `AppModel.swift` build file UUID. static let pbxSGAppModelBuild: String = "AABB000000000000000003B3" /// Package reference UUID for the ShoeGaze SPM package. static let pbxSGShoeGazePackageRef: String = "AABB000000000000000003B4" /// Product dependency UUID for `LyrebirdShoeGaze`. static let pbxSGShoeGazeDep: String = "AABB000000000000000003B5" /// Build file UUID for `LyrebirdShoeGaze` in Frameworks. static let pbxSGShoeGazeBuild: String = "AABB000000000000000003B6" /// Product dependency UUID for `LyrebirdShoeGazeUI`. static let pbxSGShoeGazeUIDep: String = "AABB000000000000000003B7" /// Build file UUID for `LyrebirdShoeGazeUI` in Frameworks. static let pbxSGShoeGazeUIBuild: String = "AABB000000000000000003B8" }