import Foundation /// Finds command-line tools for a GUI app, whose PATH is only /usr/bin:/bin:/usr/sbin:/sbin. public struct ExecutableLocator: Sendable { /// Directories searched in order. public var directories: [String] public init(directories: [String]) { self.directories = directories } /// The app's own Helpers folder (where bundle.sh puts ripgrep), then PATH, then Homebrew. public static var standard: ExecutableLocator { var dirs: [String] = [] dirs.append(Bundle.main.bundleURL.appendingPathComponent("Contents/Helpers").path) let path = ProcessInfo.processInfo.environment["PATH"] ?? "/usr/bin:/bin:/usr/sbin:/sbin" dirs += path.split(separator: ":").map(String.init) dirs += ["/opt/homebrew/bin", "/usr/local/bin"] var seen = Set() return ExecutableLocator(directories: dirs.filter { seen.insert($0).inserted }) } /// The first executable file called `name`, or nil. A name with a slash is checked as given. public func locate(_ name: String) -> URL? { let fm = FileManager.default if name.contains("/") { return fm.isExecutableFile(atPath: name) ? URL(fileURLWithPath: name) : nil } for dir in directories { let candidate = (dir as NSString).appendingPathComponent(name) var isDir: ObjCBool = false if fm.fileExists(atPath: candidate, isDirectory: &isDir), !isDir.boolValue, fm.isExecutableFile(atPath: candidate) { return URL(fileURLWithPath: candidate) } } return nil } }