90dc00f323
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
142 lines
5.9 KiB
Swift
142 lines
5.9 KiB
Swift
// 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
|