|
| 1 | +package detect |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// FrameworkPattern holds detected structural patterns for a framework. |
| 11 | +type FrameworkPattern struct { |
| 12 | + Name string `json:"name"` |
| 13 | + Config []string `json:"config_files,omitempty"` |
| 14 | + Routes string `json:"routes,omitempty"` |
| 15 | + API string `json:"api,omitempty"` |
| 16 | + Middleware string `json:"middleware,omitempty"` |
| 17 | + Models string `json:"models,omitempty"` |
| 18 | + Entry string `json:"entry,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +// fileExists returns true if the file at root/rel exists and is a regular file. |
| 22 | +func fileExists(root, rel string) bool { |
| 23 | + info, err := os.Stat(filepath.Join(root, filepath.FromSlash(rel))) |
| 24 | + return err == nil && !info.IsDir() |
| 25 | +} |
| 26 | + |
| 27 | +// dirExists returns true if the directory at root/rel exists. |
| 28 | +func dirExists(root, rel string) bool { |
| 29 | + info, err := os.Stat(filepath.Join(root, filepath.FromSlash(rel))) |
| 30 | + return err == nil && info.IsDir() |
| 31 | +} |
| 32 | + |
| 33 | +// firstExisting returns the first candidate (relative path) that exists as a file under root, |
| 34 | +// or "" if none exist. |
| 35 | +func firstExisting(root string, candidates []string) string { |
| 36 | + for _, c := range candidates { |
| 37 | + if fileExists(root, c) { |
| 38 | + return c |
| 39 | + } |
| 40 | + } |
| 41 | + return "" |
| 42 | +} |
| 43 | + |
| 44 | +// firstExistingDir returns the first candidate (relative path) that exists as a directory under root, |
| 45 | +// or "" if none exist. |
| 46 | +func firstExistingDir(root string, candidates []string) string { |
| 47 | + for _, c := range candidates { |
| 48 | + if dirExists(root, c) { |
| 49 | + return c + "/" |
| 50 | + } |
| 51 | + } |
| 52 | + return "" |
| 53 | +} |
| 54 | + |
| 55 | +// hasPkgJSONDep returns true if the given package name appears in dependencies or devDependencies |
| 56 | +// of the package.json at root. |
| 57 | +func hasPkgJSONDep(root, pkg string) bool { |
| 58 | + data, err := os.ReadFile(filepath.Join(root, "package.json")) |
| 59 | + if err != nil { |
| 60 | + return false |
| 61 | + } |
| 62 | + var p struct { |
| 63 | + Dependencies map[string]string `json:"dependencies"` |
| 64 | + DevDependencies map[string]string `json:"devDependencies"` |
| 65 | + } |
| 66 | + if json.Unmarshal(data, &p) != nil { |
| 67 | + return false |
| 68 | + } |
| 69 | + if _, ok := p.Dependencies[pkg]; ok { |
| 70 | + return true |
| 71 | + } |
| 72 | + _, ok := p.DevDependencies[pkg] |
| 73 | + return ok |
| 74 | +} |
| 75 | + |
| 76 | +// hasRequirement returns true if the given package name (case-insensitive prefix match on lines) |
| 77 | +// appears in requirements.txt at root. |
| 78 | +func hasRequirement(root, pkg string) bool { |
| 79 | + data, err := os.ReadFile(filepath.Join(root, "requirements.txt")) |
| 80 | + if err != nil { |
| 81 | + return false |
| 82 | + } |
| 83 | + lower := strings.ToLower(string(data)) |
| 84 | + pkgLower := strings.ToLower(pkg) |
| 85 | + for _, line := range strings.Split(lower, "\n") { |
| 86 | + line = strings.TrimSpace(line) |
| 87 | + if strings.HasPrefix(line, pkgLower) { |
| 88 | + return true |
| 89 | + } |
| 90 | + } |
| 91 | + return false |
| 92 | +} |
| 93 | + |
| 94 | +// hasGoMod returns true if go.mod at root contains the given module path substring. |
| 95 | +func hasGoMod(root, substr string) bool { |
| 96 | + data, err := os.ReadFile(filepath.Join(root, "go.mod")) |
| 97 | + if err != nil { |
| 98 | + return false |
| 99 | + } |
| 100 | + return strings.Contains(string(data), substr) |
| 101 | +} |
| 102 | + |
| 103 | +// hasBuildFile returns true if the given file (pom.xml or build.gradle) at root contains substr. |
| 104 | +func hasBuildFile(root, file, substr string) bool { |
| 105 | + data, err := os.ReadFile(filepath.Join(root, file)) |
| 106 | + if err != nil { |
| 107 | + return false |
| 108 | + } |
| 109 | + return strings.Contains(string(data), substr) |
| 110 | +} |
| 111 | + |
| 112 | +// DetectFrameworkPatterns inspects root and returns structural patterns for detected frameworks. |
| 113 | +func DetectFrameworkPatterns(root string) []FrameworkPattern { |
| 114 | + var patterns []FrameworkPattern |
| 115 | + |
| 116 | + // --- Next.js --- |
| 117 | + nextConfigFiles := []string{"next.config.js", "next.config.ts", "next.config.mjs"} |
| 118 | + isNext := firstExisting(root, nextConfigFiles) != "" || hasPkgJSONDep(root, "next") |
| 119 | + if isNext { |
| 120 | + p := FrameworkPattern{Name: "Next.js"} |
| 121 | + for _, f := range nextConfigFiles { |
| 122 | + if fileExists(root, f) { |
| 123 | + p.Config = append(p.Config, f) |
| 124 | + } |
| 125 | + } |
| 126 | + // Routes: prefer app/ over pages/ |
| 127 | + if dirExists(root, "app") { |
| 128 | + p.Routes = "app/" |
| 129 | + } else if dirExists(root, "pages") { |
| 130 | + p.Routes = "pages/" |
| 131 | + } |
| 132 | + // API dir |
| 133 | + if dirExists(root, "app/api") { |
| 134 | + p.API = "app/api/" |
| 135 | + } else if dirExists(root, "pages/api") { |
| 136 | + p.API = "pages/api/" |
| 137 | + } |
| 138 | + // Middleware |
| 139 | + if mid := firstExisting(root, []string{"middleware.ts", "middleware.js"}); mid != "" { |
| 140 | + p.Middleware = mid |
| 141 | + } |
| 142 | + patterns = append(patterns, p) |
| 143 | + } |
| 144 | + |
| 145 | + // --- Express --- |
| 146 | + if hasPkgJSONDep(root, "express") { |
| 147 | + p := FrameworkPattern{Name: "Express"} |
| 148 | + if dirExists(root, "routes") { |
| 149 | + p.Routes = "routes/" |
| 150 | + } |
| 151 | + if entry := firstExisting(root, []string{"app.js", "server.js"}); entry != "" { |
| 152 | + p.Entry = entry |
| 153 | + } |
| 154 | + patterns = append(patterns, p) |
| 155 | + } |
| 156 | + |
| 157 | + // --- FastAPI --- |
| 158 | + if hasRequirement(root, "fastapi") { |
| 159 | + p := FrameworkPattern{Name: "FastAPI"} |
| 160 | + if routesDir := firstExistingDir(root, []string{"app/routers", "routers"}); routesDir != "" { |
| 161 | + p.Routes = routesDir |
| 162 | + } |
| 163 | + if entry := firstExisting(root, []string{"app/main.py", "main.py"}); entry != "" { |
| 164 | + p.Entry = entry |
| 165 | + } |
| 166 | + if dirExists(root, "app/models") { |
| 167 | + p.Models = "app/models/" |
| 168 | + } |
| 169 | + patterns = append(patterns, p) |
| 170 | + } |
| 171 | + |
| 172 | + // --- Django --- |
| 173 | + isDjango := fileExists(root, "manage.py") || hasRequirement(root, "django") |
| 174 | + if isDjango { |
| 175 | + p := FrameworkPattern{Name: "Django"} |
| 176 | + p.Entry = "manage.py" |
| 177 | + patterns = append(patterns, p) |
| 178 | + } |
| 179 | + |
| 180 | + // --- Gin --- |
| 181 | + if hasGoMod(root, "gin-gonic/gin") { |
| 182 | + p := FrameworkPattern{Name: "Gin"} |
| 183 | + if entry := firstExisting(root, []string{"main.go", "cmd/server/main.go"}); entry != "" { |
| 184 | + p.Entry = entry |
| 185 | + } |
| 186 | + patterns = append(patterns, p) |
| 187 | + } |
| 188 | + |
| 189 | + // --- Spring Boot --- |
| 190 | + isSpring := hasBuildFile(root, "pom.xml", "spring-boot") || |
| 191 | + hasBuildFile(root, "build.gradle", "spring-boot") |
| 192 | + if isSpring { |
| 193 | + p := FrameworkPattern{Name: "Spring Boot"} |
| 194 | + if dirExists(root, "src/main/java") { |
| 195 | + p.Routes = "src/main/java/" |
| 196 | + } |
| 197 | + patterns = append(patterns, p) |
| 198 | + } |
| 199 | + |
| 200 | + return patterns |
| 201 | +} |
0 commit comments