Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Sources/Cadova/Concrete Layer/Generator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation

public struct Generator {
private let evaluationContext = EvaluationContext()

public init() {}

public func build(
named name: String? = nil,
options: ModelOptions...,
@ModelContentBuilder content: @Sendable @escaping () -> [BuildDirective]
) async throws -> Product {
let directives = content()
let options = ModelOptions(options).adding(modelName: name, directives: directives)
let environment = EnvironmentValues.defaultEnvironment.adding(directives: directives, modelOptions: options)

let (dataProvider, warnings) = try await directives.build(with: options, in: environment, context: evaluationContext)
return Product(dataProvider: dataProvider, evaluationContext: evaluationContext, buildWarnings: warnings)
}
}

public struct Product {
private let dataProvider: OutputDataProvider
private let evaluationContext: EvaluationContext
public let buildWarnings: [BuildWarning]

public var fileExtension: String { dataProvider.fileExtension }

internal init(dataProvider: OutputDataProvider, evaluationContext: EvaluationContext, buildWarnings: [BuildWarning]) {
self.dataProvider = dataProvider
self.evaluationContext = evaluationContext
self.buildWarnings = buildWarnings
}

public func data() async throws -> Data {
try await dataProvider.generateOutput(context: evaluationContext)
}

public func write(to fileURL: URL) async throws {
try await dataProvider.writeOutput(to: fileURL, context: evaluationContext)
}
}
Loading