v1.6.1 release: Live Scratch Swift-toolchain detection + app icon

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-22 19:19:44 -07:00
parent 8eb64f883f
commit 90dc00f323
19 changed files with 411 additions and 6 deletions
@@ -0,0 +1,26 @@
// SwiftToolchainStatus.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
/// Result of probing for a usable Swift toolchain (the Swift scratchpad compiles
/// snippets by shelling out to `swiftc`, which requires Xcode or the Command Line
/// Tools). The Graph DSL mode does not need a toolchain, so a `.missing*` status is
/// a soft condition — it gates only the Swift-compile path. Produced by ``ToolchainProbe``.
public enum SwiftToolchainStatus: Equatable, Sendable {
/// `swiftc` and the macOS SDK both resolve — the Swift scratchpad can compile.
case available
/// No developer tools at all (`xcode-select -p` fails and `swiftc` is unresolved).
/// Fix: install the Command Line Tools (`xcode-select --install`) or Xcode.
case missingDeveloperTools
/// A developer directory is selected but `swiftc` could not be resolved.
/// Fix: install/repair Xcode or the Command Line Tools.
case missingSwiftc
/// `swiftc` resolves but the macOS SDK does not — usually an incomplete Xcode.
/// Fix: install a full Xcode.
case missingSDK
}
#endif
@@ -0,0 +1,85 @@
// ToolchainProbe.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import Foundation
/// Detects whether a usable Swift toolchain is present, so the app can warn the user
/// (with install guidance) instead of only failing into the console on the first Run.
///
/// The resolvers are injectable so the `.missing*` branches can be unit-tested without
/// actually uninstalling the toolchain; the defaults wrap the real `LiveCompiler`
/// resolution (`xcrun -f swiftc`, `xcrun --show-sdk-path`) plus `xcode-select -p`.
///
/// **Not RT-related.** Runs `xcrun` / `xcode-select` subprocesses — call off the main
/// thread (a couple of fast execs).
public struct ToolchainProbe: Sendable {
private let swiftcResolver: @Sendable () -> String?
private let sdkResolver: @Sendable () -> String?
private let developerDirResolver: @Sendable () -> String?
/// - Parameters:
/// - swiftcResolver: Returns the `swiftc` path, or `nil` when unresolved.
/// - sdkResolver: Returns the macOS SDK path, or `nil` when unresolved.
/// - developerDirResolver: Returns the active developer dir (`xcode-select -p`),
/// or `nil` when no developer directory is configured.
public init(
swiftcResolver: @escaping @Sendable () -> String? = ToolchainProbe.defaultSwiftcResolver,
sdkResolver: @escaping @Sendable () -> String? = ToolchainProbe.defaultSDKResolver,
developerDirResolver: @escaping @Sendable () -> String? = ToolchainProbe.developerDirViaXcodeSelect
) {
self.swiftcResolver = swiftcResolver
self.sdkResolver = sdkResolver
self.developerDirResolver = developerDirResolver
}
/// Default `swiftc` resolver — wraps the internal `LiveCompiler.findSwiftc()` so the
/// public `init` default argument does not reference internal API.
public static func defaultSwiftcResolver() -> String? { try? LiveCompiler.findSwiftc() }
/// Default macOS SDK resolver — wraps the internal `LiveCompiler.sdkPathViaXcrun()`.
public static func defaultSDKResolver() -> String? { LiveCompiler.sdkPathViaXcrun() }
/// Classify the current toolchain state.
public func status() -> SwiftToolchainStatus {
let swiftc: String? = swiftcResolver()
let sdk: String? = sdkResolver()
if swiftc != nil && sdk != nil { return .available }
if swiftc == nil {
// No swiftc: distinguish "no developer tools at all" from "tools present
// but swiftc unresolved" by whether a developer dir is configured.
return developerDirResolver() == nil ? .missingDeveloperTools : .missingSwiftc
}
// swiftc resolves but the SDK does not.
return .missingSDK
}
/// Active developer directory via `xcode-select -p`, or `nil` when unset / xcode-select
/// is missing / returns non-zero.
public static func developerDirViaXcodeSelect() -> String? {
let xcodeSelectPath: String = "/usr/bin/xcode-select"
guard FileManager.default.isExecutableFile(atPath: xcodeSelectPath) else { return nil }
let process: Process = Process()
process.executableURL = URL(fileURLWithPath: xcodeSelectPath)
process.arguments = ["-p"]
let stdout: Pipe = Pipe()
process.standardOutput = stdout
process.standardError = FileHandle.nullDevice
do {
try process.run()
process.waitUntilExit()
} catch {
return nil
}
guard process.terminationStatus == 0 else { return nil }
let data: Data = stdout.fileHandleForReading.readDataToEndOfFile()
guard let raw: String = String(data: data, encoding: .utf8) else { return nil }
let trimmed: String = raw.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
}
#endif
@@ -19,6 +19,13 @@ The view is macOS-only — `LyrebirdLiveCompile` shells out to `swiftc`, which i
unavailable on iOS. The nested `LiveCompile/` sub-package declares
`platforms: [.macOS(.v15)]` and cannot be added to an iOS target.
> Note: **Swift mode needs a Swift toolchain** (Xcode or the Command Line Tools) —
> it compiles snippets with `swiftc`. At launch the app probes for one
> (`ToolchainProbe`) and, if none is found, shows an alert (once per launch) with
> install guidance (Install Command Line Tools / Get Xcode). **Graph DSL mode needs no
> toolchain** — it interprets generator-graph expressions in-process — so it's the
> zero-install fallback.
## Hosting
```swift
@@ -39,6 +39,11 @@ public final class LyrebirdScratchMenuController: NSObject, NSMenuItemValidation
mainMenu.addItem(windowMenuItem())
NSApplication.shared.mainMenu = mainMenu
// Launch check: if the Swift toolchain is missing, warn with install guidance
// (the Swift scratchpad needs swiftc). Fires once per launch — the probe runs
// off-main and the alert (if any) is dispatched back to the main thread.
warnIfSwiftToolchainMissing()
}
// MARK: App menu
@@ -0,0 +1,141 @@
// ToolchainAlert.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import AppKit
import LyrebirdLiveCompile
/// User-facing text for a missing-toolchain warning. Pure/value type so the
/// status → message mapping is unit-testable without presenting a modal.
public struct ToolchainAlertContent: Equatable {
/// Alert title (the `NSAlert.messageText`).
public let title: String
/// Alert body explaining the problem and the fix (the `NSAlert.informativeText`).
public let message: String
/// Whether "Install Command Line Tools" is an appropriate remedy (it is for a
/// missing toolchain, but not for a present-but-incomplete SDK).
public let offersCommandLineToolsInstall: Bool
}
/// Map a toolchain status to alert content, or `nil` when nothing needs saying
/// (`.available`). Pure — no side effects.
public func toolchainAlertContent(for status: SwiftToolchainStatus) -> ToolchainAlertContent? {
let dslNote: String = " The Graph DSL mode works without a toolchain if you'd rather not install one."
switch status {
case .available:
return nil
case .missingDeveloperTools:
return ToolchainAlertContent(
title: "Swift toolchain not found",
message: "The Swift scratchpad compiles snippets with swiftc, which needs "
+ "Xcode or the Command Line Tools." + dslNote,
offersCommandLineToolsInstall: true
)
case .missingSwiftc:
return ToolchainAlertContent(
title: "Swift compiler not found",
message: "A developer directory is selected, but swiftc could not be resolved. "
+ "Install or repair Xcode or the Command Line Tools." + dslNote,
offersCommandLineToolsInstall: true
)
case .missingSDK:
return ToolchainAlertContent(
title: "macOS SDK not found",
message: "swiftc was found but the macOS SDK is missing — your Xcode may be "
+ "incomplete. Install a full Xcode." + dslNote,
offersCommandLineToolsInstall: false
)
}
}
/// App Store product page for Xcode.
private let xcodeAppStoreURL: String = "macappstore://apps.apple.com/app/xcode/id497799835"
@MainActor
extension LyrebirdScratchMenuController {
/// Probe for a Swift toolchain off the main thread and, if missing, present a
/// one-time warning alert with install guidance. Safe no-op when the toolchain is
/// available. Called once at launch from `installMainMenu`.
public func warnIfSwiftToolchainMissing(probe: ToolchainProbe = ToolchainProbe()) {
DispatchQueue.global(qos: .utility).async {
let status: SwiftToolchainStatus = probe.status()
guard let content: ToolchainAlertContent = toolchainAlertContent(for: status) else { return }
DispatchQueue.main.async {
LyrebirdScratchMenuController.presentToolchainAlert(content)
}
}
}
/// Present the toolchain warning as a modal `NSAlert` with install actions.
static func presentToolchainAlert(_ content: ToolchainAlertContent) {
let alert: NSAlert = NSAlert()
alert.messageText = content.title
alert.informativeText = content.message
alert.alertStyle = .warning
// Buttons are added in first→last order; NSAlert returns .alertFirstButtonReturn
// for the first, +1 for the next, etc.
let installButton: NSButton? = content.offersCommandLineToolsInstall
? alert.addButton(withTitle: "Install Command Line Tools")
: nil
let xcodeButton: NSButton = alert.addButton(withTitle: "Get Xcode")
let okButton: NSButton = alert.addButton(withTitle: "OK")
// Make OK the default (Return) so an accidental Return dismisses rather than
// launching the installer / App Store. Clearing the other buttons' key
// equivalents removes their default-button status.
installButton?.keyEquivalent = ""
xcodeButton.keyEquivalent = ""
okButton.keyEquivalent = "\r"
let response: NSApplication.ModalResponse = alert.runModal()
let firstButton: NSApplication.ModalResponse = .alertFirstButtonReturn
let secondButton: NSApplication.ModalResponse =
NSApplication.ModalResponse(rawValue: firstButton.rawValue + 1)
if content.offersCommandLineToolsInstall {
if response == firstButton {
launchCommandLineToolsInstaller()
} else if response == secondButton {
openXcodeInAppStore()
}
// third button = OK: dismiss.
} else {
if response == firstButton {
openXcodeInAppStore()
}
// second button = OK: dismiss.
}
}
/// Launch Apple's Command Line Tools installer (`xcode-select --install`). This
/// opens Apple's own GUI installer dialog — the app never installs silently.
private static func launchCommandLineToolsInstaller() {
let xcodeSelectPath: String = "/usr/bin/xcode-select"
// Mirror the executable-file pre-check used by the other subprocess helpers.
guard FileManager.default.isExecutableFile(atPath: xcodeSelectPath) else {
openXcodeInAppStore()
return
}
let process: Process = Process()
process.executableURL = URL(fileURLWithPath: xcodeSelectPath)
process.arguments = ["--install"]
do {
try process.run()
} catch {
// If we can't launch it, fall back to the App Store (Xcode includes the tools).
openXcodeInAppStore()
}
}
/// Open Xcode's App Store product page.
private static func openXcodeInAppStore() {
guard let url: URL = URL(string: xcodeAppStoreURL) else { return }
NSWorkspace.shared.open(url)
}
}
#endif