Files

116 lines
4.3 KiB
Swift

import Foundation
import HelderCore
import HelderServices
/// A config service that always returns the same config.
struct FixedConfig: ConfigServicing {
var config = HelderConfig.defaults
func resolve() async -> HelderConfig { config }
func current() async -> HelderConfig { config }
}
/// A search service without ripgrep, which sends FileService down its walk.
struct NoRipgrepSearch: SearchServicing {
func isAvailable() async -> Bool { false }
func searchContent(_ query: String) async -> [ContentGroup] { [] }
func listFiles(under directory: String?) async -> [String] { [] }
}
/// One process call a `ScriptedRunner` saw.
struct RecordedCall: Sendable, Equatable {
var executable: String
var arguments: [String]
var environment: [String: String]?
var directory: URL? = nil
var input: String? = nil
}
/// Records every call and answers from a script; for tests of argument construction and error paths.
final class ScriptedRunner: ProcessRunning, @unchecked Sendable {
private let lock = NSLock()
private var recorded: [RecordedCall] = []
private let respond: @Sendable (RecordedCall) async throws -> ProcessResult
init(_ respond: @escaping @Sendable (RecordedCall) async throws -> ProcessResult) {
self.respond = respond
}
var calls: [RecordedCall] {
lock.withLock { recorded }
}
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?) async throws -> ProcessResult {
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment, directory: directory)
lock.withLock { recorded.append(call) }
return try await respond(call)
}
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?,
input: String) async throws -> ProcessResult {
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment, directory: directory, input: input)
lock.withLock { recorded.append(call) }
return try await respond(call)
}
}
enum Ripgrep {
/// `$RG_BINARY`, or the ripgrep in `vendor/ripgrep`; nil when neither exists.
static var binary: URL? {
if let env = ProcessInfo.processInfo.environment["RG_BINARY"], FileManager.default.isExecutableFile(atPath: env) {
return URL(fileURLWithPath: env)
}
let packageRoot = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
let bundled = packageRoot.appendingPathComponent("vendor/ripgrep/rg")
return FileManager.default.isExecutableFile(atPath: bundled.path) ? bundled : nil
}
static var isPresent: Bool { binary != nil }
/// A locator that finds only that ripgrep.
static var locator: ExecutableLocator {
ExecutableLocator(directories: binary.map { [$0.deletingLastPathComponent().path] } ?? [])
}
/// A locator that finds no ripgrep, whatever is installed.
static let absent = ExecutableLocator(directories: [])
}
/// Polls until `condition` holds or `timeout` passes; returns whether it held.
func eventually(timeout: TimeInterval = 5, _ condition: () -> Bool) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if condition() { return true }
try? await Task.sleep(nanoseconds: 20_000_000)
}
return condition()
}
/// Polls an async condition until it holds or `timeout` passes; returns whether it held.
func eventuallyAsync(timeout: TimeInterval = 5, _ condition: () async -> Bool) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if await condition() { return true }
try? await Task.sleep(nanoseconds: 5_000_000)
}
return await condition()
}
/// Holds every waiter until `open()`; waiters arriving after that pass straight through.
actor Gate {
private var isOpen = false
private var waiters: [CheckedContinuation<Void, Never>] = []
func wait() async {
if isOpen { return }
await withCheckedContinuation { waiters.append($0) }
}
func open() {
isOpen = true
waiters.forEach { $0.resume() }
waiters = []
}
}