-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (109 loc) · 3.33 KB
/
main.go
File metadata and controls
138 lines (109 loc) · 3.33 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
package statloc
import (
_ "embed"
"errors"
"os"
"path/filepath"
"sync"
"github.com/statloc/core/internal/mapping"
"github.com/statloc/core/internal/matching"
t "github.com/statloc/core/internal/tree"
)
var (
//go:embed "assets/languages.json"
rawLanguagesMapping string
//go:embed "assets/components.json"
rawComponentsMapping string
)
func GetStatistics(path string) (statistics Statistics, err error) {
mapping.Load(rawComponentsMapping, rawLanguagesMapping)
workdir, _ := os.Getwd()
tree := t.Tree{WorkDir: workdir}
list, err := tree.List(path)
var treePathError *t.PathError
if errors.As(err, &treePathError) {
err = &PathError{Path: path}
return
}
statistics = Statistics{
Languages: initItems(mapping.Languages),
Components: initItems(mapping.Components),
Total: TableItem{Files: 0, LOC: 0},
}
err = nil
componentsSet := componentSet{
Elements: make(map[string]struct{}),
Tail: nil,
}
var waitGroup sync.WaitGroup
var mutex sync.Mutex
waitGroup.Add(1)
tree.Chdir(path) //nolint:errcheck
goAroundCalculating(&waitGroup, &mutex, tree, list, &statistics, componentsSet)
tree.Chdir("..") //nolint:errcheck
waitGroup.Wait()
cleanStatistics(statistics.Languages)
cleanStatistics(statistics.Components)
return
}
func goAroundCalculating(
waitGroup *sync.WaitGroup,
mutex *sync.Mutex,
tree t.Tree,
list t.Nodes,
statistics *Statistics,
componentsSet componentSet,
) {
defer waitGroup.Done()
for _, node := range list {
if node.IsDir {
newComponentTitle, exists := matching.FindMatch(filepath.Base(node.Name), mapping.Components)
exists = exists && !componentsSet.In(newComponentTitle)
if exists {
componentsSet.Tail = componentsSet.Add(newComponentTitle)
}
newList, _ := tree.List(node.Name)
waitGroup.Add(1)
tree.Chdir(node.Name) //nolint:errcheck
go goAroundCalculating(waitGroup, mutex, tree.Copy(), newList, statistics, componentsSet.Copy())
tree.Chdir("..") //nolint:errcheck
if exists {
componentsSet.Pop()
}
} else {
language, exists := mapping.Languages[filepath.Ext(node.Name)]
if exists {
LOC := uint64(1)
tree.ReadNodeLineByLine(node.Name, proceedLine, &LOC)
mutex.Lock()
statistics.Total.Append(LOC, 1)
statistics.Languages[language].Append(LOC, 1)
newComponentTitle, exists := matching.FindMatch(filepath.Base(node.Name), mapping.Components)
if exists && !componentsSet.In(newComponentTitle) {
statistics.Components[newComponentTitle].Append(LOC, 1)
}
for componentTitle := range componentsSet.Elements {
statistics.Components[componentTitle].Append(LOC, 1)
}
mutex.Unlock()
}
}
}
}
func initItems(mapping map[string]string) (items Items) {
items = make(Items)
for _, value := range mapping {
items[value] = &TableItem{Files: 0, LOC: 0}
}
return
}
func cleanStatistics(items Items) {
for title, item := range items {
if item.Files == 0 {
delete(items, title)
}
}
}
func proceedLine(line string, counter *uint64) {
*counter++
}