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

403 lines
16 KiB
Swift

// ProjectScaffolder+IOSUI.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
// MARK: - ios-ui template
extension ProjectScaffolder {
/// Write the full `ios-ui` template tree at `projectRoot`.
///
/// Single iOS application target. SwiftUI entry point (`@main`) wired
/// to `iOSAudioDriver` via `AVAudioSession`. Lyrebird + LyrebirdUI
/// pulled in via a remote SPM dependency.
static func writeIOSUIFiles(
name: String,
projectRoot: String,
lyrebirdPath: 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"),
iOSAppSwift(name: name)),
((appGroupDir as NSString).appendingPathComponent("\(name)Synth.swift"),
iOSAppSynthSwift(name: name)),
((xcodeprojDir as NSString).appendingPathComponent("project.pbxproj"),
iOSAppPBXProj(name: name, lyrebirdPath: lyrebirdPath)),
((schemeDir as NSString).appendingPathComponent("\(name).xcscheme"),
appScheme(name: name, productExtension: "app")),
]
try writeFiles(files, fm: fm, projectRoot: projectRoot)
}
// MARK: Source files
static func iOSAppInfoPlist(name: String) -> String {
appInfoPlist(name: name, platform: .iOS)
}
static func iOSAppSwift(name: String) -> String {
"""
// \(name) — entry point
import SwiftUI
import AVFoundation
import LyrebirdSynth
import LyrebirdUI
@main
struct \(name)App: App {
@State private var engine: SynthesisEngine?
@State private var synth: \(name)Synth = \(name)Synth()
var body: some Scene {
WindowGroup {
VStack(spacing: 16) {
Text("\(name)").font(.title)
ParameterSlider("Frequency", node: synth.$freq, range: 110...1760)
ParameterKnob("Amplitude", node: synth.$amp, range: 0...1)
ParameterKnob("Pan", node: synth.$pan, range: -1...1)
}
.padding(24)
.onAppear { startEngine() }
}
}
private func startEngine() {
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("AVAudioSession activation failed: \\(error)")
return
}
let hwRate = AVAudioSession.sharedInstance().sampleRate
let e = SynthesisEngine(
blockSize: \(scaffoldDefaultBlockSize),
sampleRate: hwRate,
numOutputChannels: \(scaffoldDefaultOutputChannels),
numInputChannels: 0,
driver: iOSAudioDriver()
)
do {
try e.start()
try e.add(synth)
} catch {
print("Engine failed to start: \\(error)")
return
}
engine = e
}
}
"""
}
static func iOSAppSynthSwift(name: String) -> String {
"""
import LyrebirdSynth
import LyrebirdUI
final class \(name)Synth: SynthesizerBase {
@UIParam var freq: Double = 440.0
@UIParam var amp: Double = 0.25
@UIParam var pan: Double = 0.0
override func build() {
let osc = OscSin.Audio(freq: $freq)
output(bus: 0, source: Pan2(input: osc * $amp, pan: $pan))
}
}
"""
}
// MARK: - iOS pbxproj
/// `project.pbxproj` for the `ios-ui` template.
///
/// Single iOS application target. Differs from the macOS pbxproj in:
/// `SDKROOT = iphoneos`, `IPHONEOS_DEPLOYMENT_TARGET = 18.0`,
/// `TARGETED_DEVICE_FAMILY = "1,2"` (iPhone + iPad), and a SwiftUI
/// `@main` entry-file instead of `main.swift` + `AppDelegate.swift`.
static func iOSAppPBXProj(name: String, lyrebirdPath: String? = nil) -> String {
let bundleID: String = "\(defaultBundleIdentifierPrefix).\(name)"
let packageRefLabel: String = pbxPackageReferenceLabel(lyrebirdPath: lyrebirdPath)
let packageRefSection: String = pbxPackageReferenceSection(lyrebirdPath: lyrebirdPath)
return """
// !$*UTF8*$!
{
\tarchiveVersion = 1;
\tclasses = {
\t};
\tobjectVersion = 60;
\tobjects = {
/* Begin PBXBuildFile section */
\t\t\(pbxIOSAppFileBuild) /* \(name)App.swift in Sources */ = {isa = PBXBuildFile; fileRef = \(pbxIOSAppFileRef) /* \(name)App.swift */; };
\t\t\(pbxIOSSynthBuild) /* \(name)Synth.swift in Sources */ = {isa = PBXBuildFile; fileRef = \(pbxIOSSynthRef) /* \(name)Synth.swift */; };
\t\t\(pbxLyrebirdUIFrameworkBuild) /* LyrebirdUI in Frameworks */ = {isa = PBXBuildFile; productRef = \(pbxLyrebirdUIProductDep) /* LyrebirdUI */; };
/* 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 = "<group>"; };
\t\t\(pbxIOSAppFileRef) /* \(name)App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = \(name)App.swift; sourceTree = "<group>"; };
\t\t\(pbxIOSSynthRef) /* \(name)Synth.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = \(name)Synth.swift; sourceTree = "<group>"; };
/* 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\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 = "<group>";
\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 = "<group>";
\t\t};
\t\t\(pbxAppGroup) /* \(name) */ = {
\t\t\tisa = PBXGroup;
\t\t\tchildren = (
\t\t\t\t\(pbxIOSAppFileRef) /* \(name)App.swift */,
\t\t\t\t\(pbxIOSSynthRef) /* \(name)Synth.swift */,
\t\t\t\t\(pbxInfoPlistRef) /* Info.plist */,
\t\t\t);
\t\t\tpath = \(name);
\t\t\tsourceTree = "<group>";
\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\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) /* \(packageRefLabel) */,
\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\(pbxIOSAppFileBuild) /* \(name)App.swift in Sources */,
\t\t\t\t\(pbxIOSSynthBuild) /* \(name)Synth.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 */
\(packageRefSection)
/* Begin XCSwiftPackageProductDependency section */
\t\t\(pbxLyrebirdUIProductDep) /* LyrebirdUI */ = {
\t\t\tisa = XCSwiftPackageProductDependency;
\t\t\tpackage = \(pbxRemoteSPMRef) /* \(packageRefLabel) */;
\t\t\tproductName = LyrebirdUI;
\t\t};
/* End XCSwiftPackageProductDependency section */
\t};
\trootObject = \(pbxRootObject) /* Project object */;
}
"""
}
}