-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
processor.go
233 lines (197 loc) · 6.55 KB
/
processor.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
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
package processor
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"runtime"
"runtime/debug"
"sort"
"strings"
)
// Flags set via the CLI which control how the output is displayed
var Files = false
var Languages = false
var Verbose = false
var Debug = false
var Trace = false
var Duplicates = false
var Complexity = false
var More = false
var Cocomo = false
var DisableCheckBinary = false
var SortBy = ""
var Exclude = ""
var Format = ""
var FileOutput = ""
var PathBlacklist = ""
var FileListQueueSize = runtime.NumCPU()
var FileReadJobQueueSize = runtime.NumCPU()
var FileReadJobWorkers = runtime.NumCPU() * 4
var FileReadContentJobQueueSize = runtime.NumCPU()
var FileProcessJobQueueSize = runtime.NumCPU()
var FileProcessJobWorkers = runtime.NumCPU() * 4
var FileSummaryJobQueueSize = runtime.NumCPU()
var WhiteListExtensions = ""
var AverageWage int64 = 56286
var GcFileCount = 10000
var gcPercent = -1
// Not set via flags but by arguments following the the flags
var DirFilePaths = []string{}
// Loaded from the JSON that is in constants.go
var ExtensionToLanguage = map[string]string{}
var LanguageFeatures = map[string]LanguageFeature{}
// This needs to be set outside of ProcessConstants because it should only be enabled in command line
// mode https://github.com/boyter/scc/issues/32
func ConfigureGc() {
gcPercent = debug.SetGCPercent(gcPercent)
}
// ProcessConstants is responsible for setting up the language features based on the JSON file that is stored in constants
// Needs to be called at least once in order for anything to actually happen
func ProcessConstants() {
var database = loadDatabase()
startTime := makeTimestampNano()
for name, value := range database {
for _, ext := range value.Extensions {
ExtensionToLanguage[ext] = name
}
}
if Trace {
printTrace(fmt.Sprintf("nanoseconds build extension to language: %d", makeTimestampNano()-startTime))
}
startTime = makeTimestampMilli()
for name, value := range database {
complexityTrie := &Trie{}
slCommentTrie := &Trie{}
mlCommentTrie := &Trie{}
stringTrie := &Trie{}
tokenTrie := &Trie{}
complexityMask := byte(0)
singleLineCommentMask := byte(0)
multiLineCommentMask := byte(0)
stringMask := byte(0)
processMask := byte(0)
for _, v := range value.ComplexityChecks {
complexityMask |= v[0]
complexityTrie.Insert(T_COMPLEXITY, []byte(v))
if !Complexity {
tokenTrie.Insert(T_COMPLEXITY, []byte(v))
}
}
if !Complexity {
processMask |= complexityMask
}
for _, v := range value.LineComment {
singleLineCommentMask |= v[0]
slCommentTrie.Insert(T_SLCOMMENT, []byte(v))
tokenTrie.Insert(T_SLCOMMENT, []byte(v))
}
processMask |= singleLineCommentMask
for _, v := range value.MultiLine {
multiLineCommentMask |= v[0][0]
mlCommentTrie.InsertClose(T_MLCOMMENT, []byte(v[0]), []byte(v[1]))
tokenTrie.InsertClose(T_MLCOMMENT, []byte(v[0]), []byte(v[1]))
}
processMask |= multiLineCommentMask
for _, v := range value.Quotes {
stringMask |= v[0][0]
stringTrie.InsertClose(T_STRING, []byte(v[0]), []byte(v[1]))
tokenTrie.InsertClose(T_STRING, []byte(v[0]), []byte(v[1]))
}
processMask |= stringMask
LanguageFeatures[name] = LanguageFeature{
Complexity: complexityTrie,
MultiLineComments: mlCommentTrie,
SingleLineComments: slCommentTrie,
Strings: stringTrie,
Tokens: tokenTrie,
Nested: value.NestedMultiLine,
ComplexityCheckMask: complexityMask,
MultiLineCommentMask: multiLineCommentMask,
SingleLineCommentMask: singleLineCommentMask,
StringCheckMask: stringMask,
ProcessMask: processMask,
}
}
if Trace {
printTrace(fmt.Sprintf("milliseconds build language features: %d", makeTimestampMilli()-startTime))
}
}
func processFlags() {
// If wide/more mode is enabled we want the complexity calculation
// to happen regardless as thats the only purpose of the flag
if More && Complexity {
Complexity = false
}
if Debug {
printDebug(fmt.Sprintf("Path Black List: %s", PathBlacklist))
printDebug(fmt.Sprintf("Sort By: %s", SortBy))
printDebug(fmt.Sprintf("White List: %s", WhiteListExtensions))
printDebug(fmt.Sprintf("Files Output: %t", Files))
printDebug(fmt.Sprintf("Verbose: %t", Verbose))
printDebug(fmt.Sprintf("Duplicates Detection: %t", Duplicates))
printDebug(fmt.Sprintf("Complexity Calculation: %t", !Complexity))
printDebug(fmt.Sprintf("Wide: %t", More))
printDebug(fmt.Sprintf("Average Wage: %d", AverageWage))
printDebug(fmt.Sprintf("Cocomo: %t", !Cocomo))
}
}
func loadDatabase() map[string]Language {
var database map[string]Language
startTime := makeTimestampMilli()
data, err := base64.StdEncoding.DecodeString(languages)
if err != nil {
panic(fmt.Sprintf("failed to base64 decode languages: %v", err))
}
if err := json.Unmarshal(data, &database); err != nil {
panic(fmt.Sprintf("languages json invalid: %v", err))
}
if Trace {
printTrace(fmt.Sprintf("milliseconds unmarshal: %d", makeTimestampMilli()-startTime))
}
return database
}
func printLanguages() {
database := loadDatabase()
var names []string
for key := range database {
names = append(names, key)
}
sort.Slice(names, func(i, j int) bool {
return strings.Compare(strings.ToLower(names[i]), strings.ToLower(names[j])) < 0
})
for _, name := range names {
fmt.Println(fmt.Sprintf("%s (%s)", name, strings.Join(database[name].Extensions, ",")))
}
}
func Process() {
if Languages {
printLanguages()
return
}
ProcessConstants()
processFlags()
// Clean up any invalid arguments before setting everything up
if len(DirFilePaths) == 0 {
DirFilePaths = append(DirFilePaths, ".")
}
SortBy = strings.ToLower(SortBy)
if Debug {
printDebug(fmt.Sprintf("NumCPU: %d", runtime.NumCPU()))
printDebug(fmt.Sprintf("SortBy: %s", SortBy))
printDebug(fmt.Sprintf("PathBlacklist: %s", PathBlacklist))
}
fileListQueue := make(chan *FileJob, FileListQueueSize) // Files ready to be read from disk
fileReadContentJobQueue := make(chan *FileJob, FileReadContentJobQueueSize) // Files ready to be processed
fileSummaryJobQueue := make(chan *FileJob, FileSummaryJobQueueSize) // Files ready to be summerised
go walkDirectoryParallel(DirFilePaths[0], fileListQueue)
go fileReaderWorker(fileListQueue, fileReadContentJobQueue)
go fileProcessorWorker(fileReadContentJobQueue, fileSummaryJobQueue)
result := fileSummarize(fileSummaryJobQueue)
if FileOutput == "" {
fmt.Println(result)
} else {
ioutil.WriteFile(FileOutput, []byte(result), 0600)
fmt.Println("results written to " + FileOutput)
}
}