loadBuiltinEngineDefinitions reads all *.md files from the embedded data/engines/ directory, parses each EngineDefinition from its frontmatter, and registers the file content in the parser's builtin virtual FS. It panics on parse errors to surface misconfigured built-in definitions early.
()
| 85 | // content in the parser's builtin virtual FS. |
| 86 | // It panics on parse errors to surface misconfigured built-in definitions early. |
| 87 | func loadBuiltinEngineDefinitions() []*EngineDefinition { |
| 88 | engineDefinitionLoaderLog.Print("Loading built-in engine definitions from embedded Markdown files") |
| 89 | |
| 90 | var definitions []*EngineDefinition |
| 91 | |
| 92 | err := fs.WalkDir(builtinEngineFS, "data/engines", func(path string, d fs.DirEntry, err error) error { |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | if d.IsDir() { |
| 97 | return nil |
| 98 | } |
| 99 | if filepath.Ext(path) != ".md" { |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | data, readErr := builtinEngineFS.ReadFile(path) |
| 104 | if readErr != nil { |
| 105 | return fmt.Errorf("failed to read embedded engine file %s: %w", path, readErr) |
| 106 | } |
| 107 | |
| 108 | // Extract the frontmatter YAML from the Markdown file. |
| 109 | frontmatterYAML, fmErr := extractMarkdownFrontmatterYAML(data) |
| 110 | if fmErr != nil { |
| 111 | return fmt.Errorf("failed to extract frontmatter from %s: %w", path, fmErr) |
| 112 | } |
| 113 | |
| 114 | // Parse the engine definition from the frontmatter. |
| 115 | var wrapper engineDefinitionFile |
| 116 | if parseErr := yaml.Unmarshal(frontmatterYAML, &wrapper); parseErr != nil { |
| 117 | return fmt.Errorf("failed to parse embedded engine file %s: %w", path, parseErr) |
| 118 | } |
| 119 | |
| 120 | def := wrapper.Engine |
| 121 | |
| 122 | // Default runtime-id to engine id when omitted. |
| 123 | if def.RuntimeID == "" { |
| 124 | def.RuntimeID = def.ID |
| 125 | } |
| 126 | |
| 127 | // Register the full .md content in the parser's builtin virtual FS so the |
| 128 | // file can be resolved and read during import processing. |
| 129 | parser.RegisterBuiltinVirtualFile(builtinEnginePath(def.ID), data) |
| 130 | |
| 131 | engineDefinitionLoaderLog.Printf("Loaded built-in engine definition: id=%s runtime-id=%s", def.ID, def.RuntimeID) |
| 132 | definitions = append(definitions, &def) |
| 133 | return nil |
| 134 | }) |
| 135 | |
| 136 | if err != nil { |
| 137 | panic(fmt.Sprintf("failed to walk embedded engine definitions directory: %v", err)) |
| 138 | } |
| 139 | |
| 140 | engineDefinitionLoaderLog.Printf("Loaded %d built-in engine definitions", len(definitions)) |
| 141 | return definitions |
| 142 | } |
no test coverage detected