forked from glincker/stacklit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
623 lines (563 loc) · 16.8 KB
/
engine.go
File metadata and controls
623 lines (563 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package engine
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/glincker/stacklit/internal/config"
"github.com/glincker/stacklit/internal/detect"
"github.com/glincker/stacklit/internal/git"
"github.com/glincker/stacklit/internal/graph"
"github.com/glincker/stacklit/internal/monorepo"
"github.com/glincker/stacklit/internal/parser"
"github.com/glincker/stacklit/internal/renderer"
"github.com/glincker/stacklit/internal/schema"
"github.com/glincker/stacklit/internal/summary"
"github.com/glincker/stacklit/internal/walker"
)
// Options configures an engine Run.
type Options struct {
Root string
Workspace string
InstallHook bool
Quiet bool
Summary bool
}
// Result holds the output paths and assembled index from a Run.
type Result struct {
JSONPath string
HTMLPath string
MermaidPath string
Index *schema.Index
Duration time.Duration
}
// purposeMap maps common directory names to human-readable descriptions.
var purposeMap = map[string]string{
"auth": "Authentication and authorization",
"api": "API endpoints and handlers",
"db": "Database access layer",
"models": "Data models and types",
"config": "Configuration management",
"components": "UI components",
"hooks": "React hooks",
"cmd": "Application entrypoints",
"internal": "Private application packages",
"pkg": "Public packages",
"lib": "Shared library code",
"utils": "Utility functions",
"services": "Business logic services",
"middleware": "HTTP middleware",
"cli": "Command-line interface",
"schema": "Data schema definitions",
"renderer": "Output renderers",
"walker": "File system walker",
"graph": "Dependency graph",
"engine": "Core orchestration engine",
"git": "Git integration",
"assets": "Static assets",
"parser": "Source code parsers",
"monorepo": "Monorepo detection",
"detect": "Framework and tool detection",
"mcp": "MCP server for AI agents",
"summary": "AI-powered codebase summaries",
}
// inferPurpose returns a human-readable description for a module path.
func inferPurpose(name string) string {
// Use the last path segment for lookup.
base := filepath.Base(name)
if desc, ok := purposeMap[base]; ok {
return desc
}
// Fall back to the name itself, capitalised.
if base == "." || base == "" || base == "root" {
return "Root package"
}
words := strings.Fields(strings.ReplaceAll(base, "_", " "))
for i, w := range words {
if len(w) > 0 {
words[i] = strings.ToUpper(w[:1]) + w[1:]
}
}
return strings.Join(words, " ")
}
// generateAddFeatureHint returns a hint string describing where to add a new feature.
func generateAddFeatureHint(modules map[string]schema.ModuleInfo, entrypoints []string) string {
for name := range modules {
base := filepath.Base(name)
if base == "api" || base == "handler" || base == "handlers" ||
strings.Contains(name, "/api") || strings.Contains(name, "/handler") {
if len(entrypoints) > 0 {
return fmt.Sprintf("Add handler in %s, register in %s", name, entrypoints[0])
}
return fmt.Sprintf("Add handler in %s", name)
}
}
if len(entrypoints) > 0 {
return fmt.Sprintf("Start from entrypoint %s", entrypoints[0])
}
return ""
}
func filterRetainedModules(names []string, retained map[string]bool) []string {
if len(names) == 0 {
return nil
}
out := make([]string, 0, len(names))
for _, name := range names {
if retained[name] {
out = append(out, name)
}
}
return out
}
func rankMostDepended(modules map[string]schema.ModuleInfo) []string {
names := make([]string, 0, len(modules))
for name := range modules {
names = append(names, name)
}
sort.SliceStable(names, func(i, j int) bool {
ci := len(modules[names[i]].DependedBy)
cj := len(modules[names[j]].DependedBy)
if ci != cj {
return ci > cj
}
return names[i] < names[j]
})
return names
}
func findIsolatedModules(modules map[string]schema.ModuleInfo) []string {
var isolated []string
for name, mod := range modules {
if len(mod.DependsOn) == 0 && len(mod.DependedBy) == 0 {
isolated = append(isolated, name)
}
}
sort.Strings(isolated)
return isolated
}
// detectDoNotTouch returns paths that should generally not be modified by hand.
func detectDoNotTouch(root string) []string {
candidates := []string{"migrations", "vendor", "generated", "proto", ".github"}
var paths []string
for _, c := range candidates {
if _, err := os.Stat(filepath.Join(root, c)); err == nil {
paths = append(paths, c+"/")
}
}
return paths
}
// detectTestCommand inspects root for well-known build files and returns the
// appropriate test command string.
func detectTestCommand(root string) string {
checks := []struct {
file string
cmd string
}{
{"package.json", "npm test"},
{"go.mod", "go test ./..."},
{"requirements.txt", "pytest"},
{"pyproject.toml", "pytest"},
{"Cargo.toml", "cargo test"},
{"Makefile", "make test"},
}
for _, c := range checks {
if _, err := os.Stat(filepath.Join(root, c.file)); err == nil {
return c.cmd
}
}
return ""
}
// Run executes the full stacklit pipeline and returns the result.
func Run(opts Options) (*Result, error) {
start := time.Now()
// 1. Resolve absolute root.
root, err := filepath.Abs(opts.Root)
if err != nil {
return nil, fmt.Errorf("resolving root: %w", err)
}
// 1a. Load config (best-effort; uses defaults if absent).
cfg := config.Load(root)
// 2. Detect monorepo layout.
mono, err := monorepo.Detect(root)
if err != nil {
// Non-fatal — treat as single repo.
mono = &monorepo.Result{Type: "single"}
}
if !opts.Quiet && mono.Type == "monorepo" {
fmt.Printf("[stacklit] monorepo detected: %s (%d workspaces)\n", mono.Tool, len(mono.Workspaces))
}
// 3. Walk the filesystem, honouring extra ignore patterns from config.
files, err := walker.Walk(root, cfg.ScanIgnore())
if err != nil {
return nil, fmt.Errorf("walking %s: %w", root, err)
}
if !opts.Quiet {
fmt.Printf("[stacklit] found %d files\n", len(files))
}
// 4. Parse all files.
parsed, parseErrs := parser.ParseAll(files)
if !opts.Quiet {
fmt.Printf("[stacklit] parsed %d files (%d errors)\n", len(parsed), len(parseErrs))
}
// 5. Build dependency graph with config-driven limits.
g := graph.Build(parsed, graph.BuildOptions{
MaxDepth: cfg.MaxDepth,
MaxModules: cfg.MaxModules,
})
// 6. Get git activity.
activity, err := git.GetActivity(root, 90)
if err != nil {
// Non-fatal.
activity = &git.Activity{}
}
// 7. Read file contents (needed for Merkle and env var detection).
contents := make(map[string][]byte, len(files))
for _, f := range files {
data, readErr := os.ReadFile(f)
if readErr == nil {
contents[f] = data
}
}
// 8. Assemble schema.Index.
idx := assembleIndex(root, mono, files, parsed, g, activity, contents, cfg)
// 9. Compute Merkle hash.
idx.MerkleHash = git.ComputeMerkle(files, contents)
// 10. Generate AI summary if requested.
if opts.Summary {
if !opts.Quiet {
fmt.Println("[stacklit] generating AI summary...")
}
text, summaryErr := summary.Generate(idx)
if summaryErr != nil {
fmt.Printf("[stacklit] warning: summary failed: %v\n", summaryErr)
} else {
idx.Architecture = schema.Architecture{Summary: text}
}
}
// 11. Write outputs using config-driven paths.
jsonPath := filepath.Join(root, cfg.Output.JSON)
mmdPath := filepath.Join(root, cfg.Output.Mermaid)
htmlPath := filepath.Join(root, cfg.Output.HTML)
if err := renderer.WriteJSON(idx, jsonPath); err != nil {
return nil, fmt.Errorf("writing JSON: %w", err)
}
if err := renderer.WriteMermaid(idx, mmdPath); err != nil {
return nil, fmt.Errorf("writing Mermaid: %w", err)
}
if err := renderer.WriteHTML(idx, htmlPath); err != nil {
return nil, fmt.Errorf("writing HTML: %w", err)
}
// 12. Install git hook if requested.
if opts.InstallHook {
if hookErr := git.InstallHook(root); hookErr != nil && !opts.Quiet {
fmt.Printf("[stacklit] warning: could not install hook: %v\n", hookErr)
}
}
dur := time.Since(start)
// 13. Print summary.
if !opts.Quiet {
fmt.Printf("[stacklit] done in %s — wrote %s, %s, %s\n",
dur.Round(time.Millisecond),
filepath.Base(jsonPath), filepath.Base(mmdPath), filepath.Base(htmlPath))
}
return &Result{
JSONPath: jsonPath,
HTMLPath: htmlPath,
MermaidPath: mmdPath,
Index: idx,
Duration: dur,
}, nil
}
// MultiOptions configures a RunMulti call.
type MultiOptions struct {
ReposFile string
Quiet bool
}
// MultiResult holds the output of a RunMulti call.
type MultiResult struct {
OutputPath string
RepoCount int
Duration time.Duration
}
// RunMulti reads a repos file, scans each repo, and writes a combined stacklit-multi.json.
func RunMulti(opts MultiOptions) (*MultiResult, error) {
start := time.Now()
// 1. Read repos file.
data, err := os.ReadFile(opts.ReposFile)
if err != nil {
return nil, fmt.Errorf("reading repos file: %w", err)
}
// 2. Parse lines (skip empty lines and comments).
var repos []string
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
repos = append(repos, line)
}
// 3. Run stacklit on each repo.
var indices []*schema.Index
for _, repo := range repos {
if !opts.Quiet {
fmt.Printf("[stacklit] scanning %s...\n", repo)
}
result, err := Run(Options{Root: repo, Quiet: true})
if err != nil {
fmt.Printf("[stacklit] warning: failed to scan %s: %v\n", repo, err)
continue
}
indices = append(indices, result.Index)
}
// 4. Generate cross-repo summary.
multi := buildMultiIndex(indices)
// 5. Write to stacklit-multi.json in current directory.
outputPath := "stacklit-multi.json"
multiData, err := json.MarshalIndent(multi, "", " ")
if err != nil {
return nil, fmt.Errorf("marshaling multi-index: %w", err)
}
if err := os.WriteFile(outputPath, append(multiData, '\n'), 0644); err != nil {
return nil, fmt.Errorf("writing %s: %w", outputPath, err)
}
dur := time.Since(start)
if !opts.Quiet {
fmt.Printf("[stacklit] multi-repo scan done in %s — %d repos, %d modules, %d files\n",
dur.Round(time.Millisecond), len(indices), multi.TotalModules, multi.TotalFiles)
}
return &MultiResult{OutputPath: outputPath, RepoCount: len(indices), Duration: dur}, nil
}
// buildMultiIndex assembles a MultiIndex from individual repo indices.
func buildMultiIndex(indices []*schema.Index) *schema.MultiIndex {
multi := &schema.MultiIndex{
Schema: "https://stacklit.dev/schema/v1-multi.json",
Version: "1",
Type: "polyrepo",
GeneratedAt: time.Now().UTC().Format(time.RFC3339),
}
for _, idx := range indices {
multi.TotalFiles += idx.Structure.TotalFiles
multi.TotalLines += idx.Structure.TotalLines
multi.TotalModules += len(idx.Modules)
multi.Repos = append(multi.Repos, schema.RepoSummary{
Name: idx.Project.Name,
Path: idx.Project.Root,
PrimaryLanguage: idx.Tech.PrimaryLanguage,
TotalFiles: idx.Structure.TotalFiles,
TotalLines: idx.Structure.TotalLines,
Modules: idx.Modules,
Frameworks: idx.Tech.Frameworks,
Entrypoints: idx.Structure.Entrypoints,
})
}
return multi
}
// assembleIndex builds a schema.Index from the pipeline outputs.
func assembleIndex(
root string,
mono *monorepo.Result,
files []string,
parsed []*parser.FileInfo,
g *graph.Graph,
activity *git.Activity,
contents map[string][]byte,
cfg *config.Config,
) *schema.Index {
// --- Project ---
projectName := filepath.Base(root)
projectType := mono.Type
// --- Tech: count languages and collect imports ---
langStats := map[string]schema.LangStats{}
totalLines := 0
var allImports []string
for _, f := range parsed {
lang := strings.ToLower(f.Language)
ls := langStats[lang]
ls.Files++
ls.Lines += f.LineCount
langStats[lang] = ls
totalLines += f.LineCount
// Skip testdata imports for framework detection
if !strings.HasPrefix(f.Path, "testdata") {
allImports = append(allImports, f.Imports...)
}
}
// --- Frameworks ---
frameworks := detect.DetectFrameworks(root, allImports)
detectedPatterns := detect.DetectFrameworkPatterns(root)
frameworkPatterns := make([]schema.FrameworkPattern, len(detectedPatterns))
for i, p := range detectedPatterns {
frameworkPatterns[i] = schema.FrameworkPattern{
Name: p.Name,
Config: p.Config,
Routes: p.Routes,
API: p.API,
Middleware: p.Middleware,
Models: p.Models,
Entry: p.Entry,
}
}
primaryLang := ""
primaryCount := 0
for lang, ls := range langStats {
if ls.Files > primaryCount {
primaryCount = ls.Files
primaryLang = lang
}
}
// --- Structure ---
rawEntrypoints := g.Entrypoints()
var entrypoints []string
for _, ep := range rawEntrypoints {
if !strings.HasPrefix(ep, "testdata") {
entrypoints = append(entrypoints, ep)
}
}
// --- Modules ---
// maxModules and maxExports come from config.
maxModules := cfg.MaxModules
maxExports := cfg.MaxExports
allMods := g.Modules()
// If we exceed the cap, keep the modules with the most files.
if maxModules > 0 && len(allMods) > maxModules {
sort.SliceStable(allMods, func(i, j int) bool {
return allMods[i].FileCount > allMods[j].FileCount
})
allMods = allMods[:maxModules]
}
retainedModules := make(map[string]bool, len(allMods))
for _, mod := range allMods {
if strings.HasPrefix(mod.Name, "testdata") {
continue
}
retainedModules[mod.Name] = true
}
modules := map[string]schema.ModuleInfo{}
for _, mod := range allMods {
if strings.HasPrefix(mod.Name, "testdata") {
continue
}
exports := mod.Exports
if maxExports > 0 && len(exports) > maxExports {
exports = exports[:maxExports]
}
fileList := mod.Files
if len(fileList) > 20 {
fileList = fileList[:20]
}
// Compute activity level from git hot files
activityLevel := "low"
for _, hf := range activity.HotFiles {
if strings.HasPrefix(hf.Path, mod.Name+"/") {
if hf.Commits90d > 10 {
activityLevel = "high"
break
} else if hf.Commits90d > 3 {
activityLevel = "medium"
}
}
}
// Cap type defs to maxExports per module in a deterministic order.
typeDefs := mod.TypeDefs
if maxExports > 0 && len(typeDefs) > maxExports {
keys := make([]string, 0, len(typeDefs))
for k := range typeDefs {
keys = append(keys, k)
}
sort.Strings(keys)
trimmed := make(map[string]string, maxExports)
for i, k := range keys {
if i >= maxExports {
break
}
trimmed[k] = typeDefs[k]
}
typeDefs = trimmed
}
modules[mod.Name] = schema.ModuleInfo{
Purpose: inferPurpose(mod.Name),
Language: mod.PrimaryLanguage,
Files: mod.FileCount,
Lines: mod.LineCount,
FileList: fileList,
Exports: exports,
TypeDefs: typeDefs,
DependsOn: filterRetainedModules(mod.DependsOn, retainedModules),
DependedBy: filterRetainedModules(mod.DependedBy, retainedModules),
Activity: activityLevel,
}
}
// --- Dependencies ---
edges := g.Edges()
var schemaEdges [][2]string
for _, e := range edges {
if strings.HasPrefix(e.From, "testdata") || strings.HasPrefix(e.To, "testdata") {
continue
}
if !retainedModules[e.From] || !retainedModules[e.To] {
continue
}
schemaEdges = append(schemaEdges, [2]string{e.From, e.To})
}
mostDepended := rankMostDepended(modules)
// Cap most-depended list.
if len(mostDepended) > 10 {
mostDepended = mostDepended[:10]
}
isolated := findIsolatedModules(modules)
// --- Git ---
hotFiles := make([]schema.HotFile, len(activity.HotFiles))
for i, hf := range activity.HotFiles {
hotFiles[i] = schema.HotFile{Path: hf.Path, Commits90d: hf.Commits90d}
}
// --- Hints ---
testCmd := detectTestCommand(root)
envVars := detect.DetectEnvVars(root, contents)
addFeature := generateAddFeatureHint(modules, entrypoints)
doNotTouch := detectDoNotTouch(root)
// --- Workspaces ---
var workspaces []string
if mono != nil {
workspaces = mono.Workspaces
}
return &schema.Index{
Version: "1",
Project: schema.Project{
Name: projectName,
Root: ".",
Type: projectType,
Workspaces: workspaces,
},
Tech: schema.Tech{
PrimaryLanguage: primaryLang,
Languages: langStats,
Frameworks: frameworks,
FrameworkPatterns: frameworkPatterns,
},
Structure: schema.Structure{
TotalFiles: len(files),
TotalLines: totalLines,
Entrypoints: entrypoints,
},
Modules: modules,
Dependencies: schema.Dependencies{
Edges: schemaEdges,
Entrypoints: entrypoints,
MostDepended: mostDepended,
Isolated: isolated,
},
Git: schema.GitInfo{
HotFiles: hotFiles,
Recent: activity.RecentFiles,
Stable: activity.StableFiles,
},
Hints: schema.Hints{
TestCmd: testCmd,
EnvVars: envVars,
AddFeature: addFeature,
DoNotTouch: doNotTouch,
},
}
}