From bf4b71653fb98952c81f5f1d9a2161a3c8331d9c Mon Sep 17 00:00:00 2001 From: Egutierrez Date: Sat, 28 Mar 2026 02:07:08 +0100 Subject: [PATCH] feat: indexer que regenera registry.db desde los .md Index() recorre functions/ y types/, parsea cada .md con frontmatter, purga la BD existente e inserta todas las entradas. Devuelve conteos y errores sin abortar ante fallos individuales. Co-Authored-By: Claude Opus 4.6 (1M context) --- registry/indexer.go | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 registry/indexer.go diff --git a/registry/indexer.go b/registry/indexer.go new file mode 100644 index 00000000..b2602d1b --- /dev/null +++ b/registry/indexer.go @@ -0,0 +1,87 @@ +package registry + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// IndexResult holds stats from an indexing run. +type IndexResult struct { + Functions int + Types int + Errors []string +} + +// Index walks the registry root, parses all .md files, and populates the database. +// It purges existing data first to ensure a clean rebuild. +func Index(db *DB, root string) (*IndexResult, error) { + if err := db.Purge(); err != nil { + return nil, fmt.Errorf("purging database: %w", err) + } + + result := &IndexResult{} + + // Index functions + functionsDir := filepath.Join(root, "functions") + if _, err := os.Stat(functionsDir); err == nil { + err := filepath.Walk(functionsDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + if info.IsDir() || !strings.HasSuffix(path, ".md") { + return nil + } + + f, err := ParseFunctionMD(path) + if err != nil { + result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", path, err)) + return nil + } + + if err := db.InsertFunction(f); err != nil { + result.Errors = append(result.Errors, fmt.Sprintf("insert %s: %v", f.ID, err)) + return nil + } + + result.Functions++ + return nil + }) + if err != nil { + return nil, fmt.Errorf("walking functions: %w", err) + } + } + + // Index types + typesDir := filepath.Join(root, "types") + if _, err := os.Stat(typesDir); err == nil { + err := filepath.Walk(typesDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + if info.IsDir() || !strings.HasSuffix(path, ".md") { + return nil + } + + t, err := ParseTypeMD(path) + if err != nil { + result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", path, err)) + return nil + } + + if err := db.InsertType(t); err != nil { + result.Errors = append(result.Errors, fmt.Sprintf("insert %s: %v", t.ID, err)) + return nil + } + + result.Types++ + return nil + }) + if err != nil { + return nil, fmt.Errorf("walking types: %w", err) + } + } + + return result, nil +}