NewAstGrepScanner creates a scanner, extracting rules to temp dir
()
| 47 | |
| 48 | // NewAstGrepScanner creates a scanner, extracting rules to temp dir |
| 49 | func NewAstGrepScanner() (*AstGrepScanner, error) { |
| 50 | // Find ast-grep binary (installed as "sg" via brew, "ast-grep" via cargo/pipx) |
| 51 | binary := findAstGrepBinary() |
| 52 | |
| 53 | // Create temp directory for rules |
| 54 | rulesDir, err := os.MkdirTemp("", "codemap-sg-rules-*") |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | // Extract embedded rules |
| 60 | entries, err := sgRules.ReadDir("sg-rules") |
| 61 | if err != nil { |
| 62 | os.RemoveAll(rulesDir) |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | for _, entry := range entries { |
| 67 | content, err := sgRules.ReadFile("sg-rules/" + entry.Name()) |
| 68 | if err != nil { |
| 69 | continue |
| 70 | } |
| 71 | os.WriteFile(filepath.Join(rulesDir, entry.Name()), content, 0644) |
| 72 | } |
| 73 | |
| 74 | return &AstGrepScanner{rulesDir: rulesDir, binary: binary}, nil |
| 75 | } |
| 76 | |
| 77 | // extractJSONArray finds and extracts a JSON array from output that may have garbage before it |
| 78 | // This handles ast-grep 0.40.2 which had a debug println! bug |