Method
executeTemplate(_:arguments:)
Executes a template with the specified arguments.
@MainActor func executeTemplate(_ template: MTLTemplate, arguments: [(any EcoreValue)?]) async throws
Parameters
template-
The template to execute
arguments-
The arguments matching the template parameters
Discussion
This method handles:
-
Parameter binding
-
Guard condition evaluation
-
Template body execution
-
Post-condition evaluation
-
Statistics updates
Guard Conditions
If the template has a guard condition and it evaluates to false, the template body is not executed:
let template = MTLTemplate(
name: "generatePublicClass",
parameters: [MTLVariable(name: "class", type: "Class")],
guard: MTLExpression(/* class.isPublic */),
body: ...
)
// If guard fails, body is skipped
try await generator.executeTemplate(template, arguments: [privateClass])
Post-conditions
If the template has a post-condition and it evaluates to false after execution, an error is thrown:
let template = MTLTemplate(
name: "generateFile",
parameters: [MTLVariable(name: "path", type: "String")],
post: MTLExpression(/* fileExists(path) */),
body: ...
)
// If post fails, throws error
try await generator.executeTemplate(template, arguments: ["/output/file.txt"])