-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcloc.go
96 lines (81 loc) · 2.17 KB
/
gcloc.go
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
package gcloc
import (
"github.com/Scorpio69t/gcloc/pkg/file"
"github.com/Scorpio69t/gcloc/pkg/language"
"github.com/Scorpio69t/gcloc/pkg/option"
log "github.com/Scorpio69t/gcloc/pkg/simplelog"
"github.com/Scorpio69t/gcloc/pkg/syncmap"
"sync"
)
// Parser is the main struct for parsing files.
type Parser struct {
languages *language.DefinedLanguages
opts *option.GClocOptions
}
// Result is the main struct for the result of parsing files.
type Result struct {
Total *language.Language
Files map[string]*file.GClocFile
Languages map[string]*language.Language
MaxPathLength int
}
// NewParser creates a new Parser.
func NewParser(languages *language.DefinedLanguages, opts *option.GClocOptions) *Parser {
return &Parser{
languages: languages,
opts: opts,
}
}
// Analyze analyzes the files in the given paths.
func (p *Parser) Analyze(paths []string) (*Result, error) {
total := language.NewLanguage("Total", []string{}, [][]string{{"", ""}}) // Create a new language for the total.
languages, err := language.GetAllFiles(paths, p.languages, p.opts)
if err != nil {
log.Error("Error getting all files: %v", err)
return nil, err
}
maxPathLen := 0
num := 0
for _, lang := range languages {
num += len(lang.Files)
for _, f := range lang.Files {
l := len(f)
if maxPathLen < l {
maxPathLen = l
}
}
}
gClocFiles := syncmap.NewSyncMap[string, *file.GClocFile](num)
var mu sync.Mutex
var wg sync.WaitGroup
for _, lang := range languages {
wg.Add(1)
go func(l *language.Language) {
defer wg.Done()
for _, filename := range l.Files {
cf := file.AnalyzeFile(filename, l, p.opts)
cf.Language = l.Name
l.Codes += cf.Codes
l.Comments += cf.Comments
l.Blanks += cf.Blanks
gClocFiles.Store(filename, cf)
}
mu.Lock()
defer mu.Unlock()
files := uint32(len(l.Files))
if files > 0 {
total.Total += files
total.Blanks += l.Blanks
total.Comments += l.Comments
total.Codes += l.Codes
}
}(lang)
}
wg.Wait()
return &Result{
Total: total,
Files: gClocFiles.ToMap(), // Convert syncmap to map.
Languages: languages,
MaxPathLength: maxPathLen,
}, nil
}