at main 1.8 kB view raw
1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6import TSCBasic 7import llbuild2fx 8 9/// A simplified API for `llbuild2fx` that bundles the arguments of `FXKey.computeValue(_:_)`. 10public struct BuildContext<Key: FXKey> { 11 fileprivate let functionInterface: FXFunctionInterface<Key> 12 fileprivate let context: Context 13 14 var db: any LLBCASDatabase { context.db } 15 16 func request<X: FXKey>(_ key: X) async throws -> X.ValueType { 17 try await functionInterface.request(key, context) 18 } 19 20 func load(_ id: LLBDataID, type hint: LLBFileType? = nil) async throws -> LLBCASFSNode { 21 try await LLBCASFSClient(context.db).load(id, type: hint, context).get() 22 } 23 24 func read(blob: LLBCASBlob) async throws -> LLBByteBufferView { 25 try await blob.read(context).get() 26 } 27} 28 29public protocol BuildKey: AsyncFXKey { 30 func computeValue(_ ctx: BuildContext<Self>) async throws -> ValueType 31} 32 33extension BuildKey { 34 public func computeValue(_ fi: FXFunctionInterface<Self>, _ ctx: Context) async throws -> ValueType { 35 try await computeValue(BuildContext(functionInterface: fi, context: ctx)) 36 } 37} 38 39extension LLBCASFileTree { 40 public static func load<X: FXKey>(id: LLBDataID, in ctx: BuildContext<X>) async throws -> LLBCASFileTree { 41 try await load(id: id, from: ctx.db, ctx.context).get() 42 } 43 44 public func remove<X: FXKey>(path: AbsolutePath, in ctx: BuildContext<X>) async throws -> LLBCASFileTree { 45 try await remove(path: path, in: ctx.db, ctx.context).get() 46 } 47 48 public func traverse<X: FXKey>(root: AbsolutePath, in ctx: BuildContext<X>, _ callback: (AbsolutePath, LLBDataID, LLBDirectoryEntry) async throws -> Void) async throws { 49 try await traverse(root: root, in: ctx.db, ctx.context, callback) 50 } 51}