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
|
package showcase
import (
"bufio"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// detectLanguages detects programming languages used in the repository with line counts
// Returns both programming languages and documentation/text files separately
func detectLanguages(repoPath string) (languages []LanguageStats, documentation []LanguageStats, err error) {
languageLines := make(map[string]int)
documentationLines := make(map[string]int)
// Define common language extensions
langExtensions := map[string]string{
".go": "Go",
".py": "Python",
".js": "JavaScript",
".ts": "TypeScript",
".java": "Java",
".c": "C",
".cpp": "C++",
".cc": "C++",
".cxx": "C++",
".h": "C/C++",
".hpp": "C++",
".hxx": "C++",
".cs": "C#",
".rb": "Ruby",
".php": "PHP",
".swift": "Swift",
".kt": "Kotlin",
".rs": "Rust",
".scala": "Scala",
".r": "R",
".m": "Objective-C",
".mm": "Objective-C++",
".sh": "Shell",
".bash": "Shell",
".zsh": "Shell",
".fish": "Shell",
".pl": "Perl",
".pm": "Perl",
".raku": "Raku",
".rakumod": "Raku",
".rakudoc": "Raku",
".rakutest": "Raku",
".p6": "Raku",
".pm6": "Raku",
".lua": "Lua",
".vim": "Vim Script",
".el": "Emacs Lisp",
".clj": "Clojure",
".hs": "Haskell",
".ml": "OCaml",
".ex": "Elixir",
".exs": "Elixir",
".dart": "Dart",
".jl": "Julia",
".nim": "Nim",
".v": "V",
".zig": "Zig",
".html": "HTML",
".htm": "HTML",
".css": "CSS",
".scss": "SCSS",
".sass": "Sass",
".less": "Less",
".xml": "XML",
".json": "JSON",
".yaml": "YAML",
".yml": "YAML",
".toml": "TOML",
".ini": "INI",
".cfg": "Config",
".conf": "Config",
".sql": "SQL",
".tf": "HCL",
".tfvars": "HCL",
".hcl": "HCL",
".awk": "AWK",
}
// Define documentation/text extensions
docExtensions := map[string]string{
".md": "Markdown",
".rst": "reStructuredText",
".tex": "LaTeX",
".txt": "Text",
".adoc": "AsciiDoc",
".org": "Org",
}
// Special files that indicate specific languages
specialFiles := map[string]string{
"makefile": "Make",
"gnumakefile": "Make",
"dockerfile": "Docker",
"dockerfile.*": "Docker",
"cmakelists.txt": "CMake",
"rakefile": "Ruby",
"gemfile": "Ruby",
"package.json": "JavaScript",
"cargo.toml": "Rust",
"go.mod": "Go",
"go.sum": "Go",
"pom.xml": "Java",
"build.gradle": "Gradle",
"build.gradle.kts": "Kotlin",
"requirements.txt": "Python",
"setup.py": "Python",
"pyproject.toml": "Python",
"composer.json": "PHP",
"*.dockerfile": "Docker",
"containerfile": "Docker",
"jenkinsfile": "Groovy",
"vagrantfile": "Ruby",
}
// Count lines for each language
err = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // Skip errors
}
// Skip directories
if info.IsDir() {
name := info.Name()
// Skip hidden directories and common non-code directories
if strings.HasPrefix(name, ".") && name != "." ||
name == "node_modules" ||
name == "vendor" ||
name == "target" ||
name == "dist" ||
name == "build" ||
name == "out" ||
name == "__pycache__" ||
name == "coverage" {
return filepath.SkipDir
}
return nil
}
// Skip binary and large files
if info.Size() > 10*1024*1024 { // Skip files larger than 10MB
return nil
}
// Get the filename and extension
basename := strings.ToLower(filepath.Base(path))
ext := strings.ToLower(filepath.Ext(path))
// Determine the language or documentation type
var language string
var isDoc bool
// Check special files first
if lang, ok := specialFiles[basename]; ok {
language = lang
} else {
// Check documentation extensions
if docType, ok := docExtensions[ext]; ok {
language = docType
isDoc = true
} else if lang, ok := langExtensions[ext]; ok {
// Check programming language extensions
language = lang
}
}
// Check shebang for executable files when no language was detected
if language == "" && info.Mode()&0111 != 0 {
if file, err := os.Open(path); err == nil {
scanner := bufio.NewScanner(file)
if scanner.Scan() {
firstLine := scanner.Text()
if strings.HasPrefix(firstLine, "#!") {
// Check for various interpreters in shebang
if strings.Contains(firstLine, "python") {
language = "Python"
} else if strings.Contains(firstLine, "node") {
language = "JavaScript"
} else if strings.Contains(firstLine, "ruby") {
language = "Ruby"
} else if strings.Contains(firstLine, "perl") && !strings.Contains(firstLine, "perl6") {
language = "Perl"
} else if strings.Contains(firstLine, "perl6") || strings.Contains(firstLine, "raku") {
language = "Raku"
} else if strings.Contains(firstLine, "awk") || strings.Contains(firstLine, "gawk") || strings.Contains(firstLine, "mawk") {
language = "AWK"
} else if strings.Contains(firstLine, "sh") || strings.Contains(firstLine, "bash") || strings.Contains(firstLine, "zsh") || strings.Contains(firstLine, "fish") {
language = "Shell"
} else if strings.Contains(firstLine, "php") {
language = "PHP"
} else if strings.Contains(firstLine, "lua") {
language = "Lua"
}
}
}
file.Close()
}
}
// If we identified a language, count its lines
if language != "" {
lines, err := countFileLines(path)
if err == nil {
if isDoc {
documentationLines[language] += lines
} else {
languageLines[language] += lines
}
}
}
return nil
})
if err != nil {
return nil, nil, err
}
// Process programming languages
totalCodeLines := 0
for _, lines := range languageLines {
totalCodeLines += lines
}
var languageStats []LanguageStats
for lang, lines := range languageLines {
percentage := 0.0
if totalCodeLines > 0 {
percentage = float64(lines) * 100.0 / float64(totalCodeLines)
}
languageStats = append(languageStats, LanguageStats{
Name: lang,
Lines: lines,
Percentage: percentage,
})
}
// Sort languages by percentage (descending)
sort.Slice(languageStats, func(i, j int) bool {
return languageStats[i].Percentage > languageStats[j].Percentage
})
// Process documentation
totalDocLines := 0
for _, lines := range documentationLines {
totalDocLines += lines
}
var docStats []LanguageStats
for docType, lines := range documentationLines {
percentage := 0.0
if totalDocLines > 0 {
percentage = float64(lines) * 100.0 / float64(totalDocLines)
}
docStats = append(docStats, LanguageStats{
Name: docType,
Lines: lines,
Percentage: percentage,
})
}
// Sort documentation by percentage (descending)
sort.Slice(docStats, func(i, j int) bool {
return docStats[i].Percentage > docStats[j].Percentage
})
return languageStats, docStats, nil
}
// countFileLines counts the number of lines in a file
func countFileLines(path string) (int, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
lines := 0
for scanner.Scan() {
lines++
}
if err := scanner.Err(); err != nil {
return 0, err
}
return lines, nil
}
// FormatLanguagesWithPercentages formats languages with their percentages
func FormatLanguagesWithPercentages(languages []LanguageStats) string {
if len(languages) == 0 {
return ""
}
var parts []string
for _, lang := range languages {
if lang.Percentage >= 0.1 { // Only show languages with at least 0.1%
parts = append(parts, fmt.Sprintf("%s (%.1f%%)", lang.Name, lang.Percentage))
}
}
// If all languages are below 0.1%, just show the names
if len(parts) == 0 {
for _, lang := range languages {
parts = append(parts, lang.Name)
}
}
return strings.Join(parts, ", ")
}
|