// SwiftToolchain.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation /// Locates the `swift` executable for `lyrebird run` / `lyrebird repl`. /// /// Searches `PATH` first, so whatever toolchain the user's shell resolves /// `swift` to is honoured — a swiftly shim (`~/.local/share/swiftly/bin/swift`), /// Homebrew, or a custom prefix — then falls back to the well-known absolute /// locations for a bare environment with an empty `PATH`. Returns `nil` if no /// `swift` executable is found anywhere. /// /// The hardcoded fallbacks alone were macOS-only (`/usr/bin/swift` etc.) and /// missed every non-default Linux install, most notably swiftly. func lyrebirdLocateSwift() -> String? { let fileManager: FileManager = FileManager.default let executableName: String = "swift" // PATH, in order — the active toolchain wins over the fallbacks below. if let pathEnv: String = ProcessInfo.processInfo.environment["PATH"] { let directories: [String] = pathEnv .split(separator: ":", omittingEmptySubsequences: true) .map(String.init) for directory: String in directories { let candidate: String = directory.hasSuffix("/") ? directory + executableName : directory + "/" + executableName if fileManager.isExecutableFile(atPath: candidate) { return candidate } } } // Well-known absolute locations, for an environment with no usable PATH. let fallbacks: [String] = ["/usr/bin/swift", "/usr/local/bin/swift", "/opt/homebrew/bin/swift"] for path: String in fallbacks where fileManager.isExecutableFile(atPath: path) { return path } return nil }