-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
401 lines (356 loc) · 13.9 KB
/
main.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"flag"
"fmt"
"hash/fnv"
"io/ioutil"
"os"
"sort"
"strings"
"time"
"gopkg.in/yaml.v3"
)
const (
// InfoColor ...
InfoColor = "\033[1;34m%s\033[0m"
// NoticeColor ...
NoticeColor = "\033[1;36m%s\033[0m"
// WarningColor ...
WarningColor = "\033[1;33m%s\033[0m"
// ErrorColor ...
ErrorColor = "\033[1;31m%s\033[0m"
// DebugColor ...
DebugColor = "\033[0;36m%s\033[0m"
)
const (
// InfoColor ...bold
hInfoColor = "Blue"
// NoticeColor ... bold
hNoticeColor = "Cyan"
// WarningColor ...bold
hWarningColor = "Yellow"
// ErrorColor ...bold
hErrorColor = "Red"
// DebugColor ...
hDebugColor = "Cyan"
)
const (
// Version for current version of css-checker
Version = "0.4.1"
)
// Params setting parameters
type Params struct {
Version bool `yaml:"version"`
ColorsCheck bool `yaml:"colors"`
CSS bool `yaml:"css"`
SectionsCheck bool `yaml:"sections"`
SimilarityCheck bool `yaml:"sim"`
SimilarityThreshold int `yaml:"sim-threshold"`
StyledComponents bool `yaml:"styled"`
LongScriptsCheck bool `yaml:"long-line"`
Path string `yaml:"path"`
LongScriptLength int `yaml:"length-threshold"`
Ignores []string `yaml:"ignores"`
Unused bool `yaml:"unused"`
Unrestricted bool `yaml:"unrestricted"`
ConfigPath string `yaml:"config"`
ToFile bool `yaml:"to-file"`
OutputFileName string `yaml:"file-name"`
}
var params = Params{
Version: false,
ColorsCheck: true,
SectionsCheck: true,
LongScriptsCheck: true,
CSS: true,
Path: ".",
LongScriptLength: 20,
Ignores: []string{},
ToFile: true,
OutputFileName: "css-checker.html",
}
// StyleSection ...
type StyleSection struct {
name string
filePath string
value []string
valueHash uint64
}
// Script records scripts that might be extracted as variables
type Script struct {
filePath string
sectionName string
key string
value string
hashValue uint64
}
// ScriptSummary Here, script stands for css colors and lint lines (although they are not actual scripts ^_^).
type ScriptSummary struct {
hashValue uint64
value string
scripts []Script
count int
}
// SectionSummary for one section, the classes and count that occured under given paths
type SectionSummary struct {
names []string
value string
count int
}
// SimilaritySummary records 2 sections' similarities and their common lines (here, duplicatedScript stands for common line)
type SimilaritySummary struct {
sections [2]StyleSection
similarity int
duplicatedScripts []string
}
// StyleHashRecorder records sections index and original string
type StyleHashRecorder struct {
sectionIndex int
originString string
}
var styleList = []StyleSection{}
var longScriptList = []Script{}
var colorScriptList = []Script{}
var hashCounters = map[uint64][]StyleHashRecorder{} // hashValue -> section
func hash(s string) uint64 {
h := fnv.New64()
h.Write([]byte(s))
return h.Sum64()
}
// HashOrigin hashvalue and its origin
type HashOrigin struct {
hash uint64
origin string
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func getSimilarSections() []SimilaritySummary {
records := map[[2]int][]HashOrigin{}
summary := []SimilaritySummary{}
// Convert map LineHash -> Section => [SectionIndex1][SectionIndex2] <-> Duplicated Hashes [O(n)], n for identical hash, section stands for css class
for key, element := range hashCounters {
if len(element) < 2 {
continue
}
for i := 0; i < len(element)-1; i++ {
for j := i + 1; j < len(element); j++ {
if element[i].sectionIndex < element[j].sectionIndex {
if record, found := records[[2]int{element[i].sectionIndex, element[j].sectionIndex}]; found {
records[[2]int{element[i].sectionIndex, element[j].sectionIndex}] = append(record, HashOrigin{hash: key, origin: element[i].originString})
} else {
records[[2]int{element[i].sectionIndex, element[j].sectionIndex}] = []HashOrigin{{hash: key, origin: element[i].originString}}
}
}
}
}
}
// In map: [SectionIndex1][SectionIndex2] -> Duplicated Hashes, number of the duplicated hashes stands for duplicated lines between classes.
threshold := float32(params.SimilarityThreshold) / float32(100)
for key, element := range records {
left, right := styleList[key[0]], styleList[key[1]]
lengthLeft, lengthRight := len(left.value), len(right.value)
if float32(len(element)) > float32(lengthLeft)*threshold || float32(len(element)) > float32(lengthRight)*threshold {
if len(element) == min(lengthLeft, lengthRight) {
continue
}
duplicatedStrings := []string{}
for _, hashOrigin := range element {
duplicatedStrings = append(duplicatedStrings, hashOrigin.origin)
}
summary = append(summary, SimilaritySummary{
sections: [2]StyleSection{left, right},
similarity: 100 * len(element) / min(lengthLeft, lengthRight),
duplicatedScripts: duplicatedStrings,
})
}
}
sort.SliceStable(summary, func(i, j int) bool {
return summary[i].similarity < summary[j].similarity
})
return summary
}
// return values: (isConfigFileFound, error)
func getConf(conf *Params, path string) (bool, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return false, nil // no config file is not an error
}
fmt.Printf("Config YAML found, using configs in: %s\n", path)
err = yaml.Unmarshal(buf, conf)
if err != nil {
fmt.Printf(ErrorColor, fmt.Sprintf("Config Error: in file %q: %v\n", path, err)) // config file in wrong format is an error
return true, err
}
return true, err
}
// ParamsParse parse the given config from command line and .yaml file
func ParamsParse() {
ignorePathsString := ""
flag.BoolVar(¶ms.ColorsCheck, "colors", true, "whether to check colors")
flag.BoolVar(¶ms.CSS, "css", true, "whether to check css files")
flag.StringVar(&ignorePathsString, "ignores", "", "paths and files to be ignored (e.g. node_modules,*.example.css)")
flag.IntVar(¶ms.LongScriptLength, "length-threshold", 20, "Min length of a single style value (no including the key) that to be considered as long script line")
flag.BoolVar(¶ms.LongScriptsCheck, "long-line", true, "whether to check duplicated long script lines")
flag.StringVar(¶ms.Path, "path", ".", "set path to files, default to be current folder")
flag.BoolVar(¶ms.SectionsCheck, "sections", true, "whether to check css class duplications")
flag.BoolVar(¶ms.SimilarityCheck, "sim", true, "whether to check similar css classes")
flag.IntVar(¶ms.SimilarityThreshold, "sim-threshold", 80, "Threshold for Similarity Check (int only, >=20 && < 100, e.g. 80 for 80%)")
flag.BoolVar(¶ms.StyledComponents, "styled", false, "checks for styled components")
flag.BoolVar(¶ms.Unrestricted, "unrestricted", false, "search all files (gitignore)")
flag.BoolVar(¶ms.Unused, "unused", false, "whether to check unused classes (Beta)")
flag.BoolVar(¶ms.Version, "version", false, "prints current version and exits")
flag.StringVar(¶ms.ConfigPath, "config", "", "set configuration file, check github.com/ruilisi/css-checker for details")
flag.BoolVar(¶ms.ToFile, "to-file", true, "output result to a html file. default value is true")
flag.StringVar(¶ms.OutputFileName, "file-name", "css-checker.html", "set output file name. default is css-checker.html")
flag.Parse()
if len(ignorePathsString) > 0 {
params.Ignores = strings.Split(ignorePathsString, ",")
}
dirname, err := os.UserHomeDir()
if err != nil {
fmt.Printf(ErrorColor, "Home path not found")
}
if strings.Contains(params.Path, "~") {
params.Path = strings.Replace(params.Path, "~", dirname, 1) // 通过flags拿到的路径中~并不会被转译为$HOME导致读取文件错误
}
if strings.Contains(params.ConfigPath, "~") {
params.ConfigPath = strings.Replace(params.ConfigPath, "~", dirname, 1)
}
if params.SimilarityThreshold < 20 {
params.SimilarityThreshold = 20
} else if params.SimilarityThreshold >= 100 {
params.SimilarityThreshold = 99
}
}
func main() {
t1 := time.Now()
ParamsParse()
// 创建输出文件
createOutuputFile(params.OutputFileName)
// 是否将结果写到文件
wtf := params.ToFile
// 获取输出的html文件
hf := getHtmlFile(params.OutputFileName)
if params.Version {
vMsg := fmt.Sprintf("<p>Version: v%s</p>\n", Version)
// fmt.Printf("Version: v%s\n", Version)
fmt.Printf(vMsg)
writeToFile(hf, vMsg, wtf)
return
}
// Read Config File
configPath := params.ConfigPath
if len(params.ConfigPath) == 0 {
configPath = fmt.Sprintf("css-checker.yaml")
}
found, err := getConf(¶ms, configPath)
if err != nil {
return // config file in wrong format
}
// File Walk Starts
patternsToCheck := []string{""}
if params.StyledComponents {
patternsToCheck = []string{"*.js", "*.jsx", "*.ts", "*.tsx"}
}
if params.CSS {
patternsToCheck = append(patternsToCheck, "*.css")
}
files, err := WalkMatch(params.Path, WalkMatchOptions{patterns: patternsToCheck, ignores: params.Ignores, unrestricted: params.Unrestricted})
if err != nil {
eMsg := fmt.Sprintf("<p>No css files found at given path: %s</p>", params.Path)
fmt.Printf(ErrorColor, fmt.Sprintf("No css files found at given path: %s", params.Path))
writeToFile(hf, eMsg, wtf)
return
}
fmt.Println("\nChecking starts. this may take seconds.")
csf := fmt.Sprintf("<p style='color: %s'>Found %d css files. Begin to scan.</p>", hNoticeColor , len(files))
fmt.Printf(NoticeColor, fmt.Sprintf("Found %d css files. Begin to scan.\n", len(files)))
writeToFile(hf, csf, wtf)
// CSS Parsing
for _, path := range files {
longScripts, colorScripts := SectionsParse(path, params.SimilarityThreshold)
longScriptList = append(longScriptList, longScripts...)
colorScriptList = append(colorScriptList, colorScripts...)
}
fcs := fmt.Sprintf("<p style='color: %s'>Found %d css sections. Begin to compare.</p><br/><br/>", hDebugColor, len(styleList))
fmt.Printf(DebugColor, fmt.Sprintf("Found %d css sections. Begin to compare.\n", len(styleList)))
writeToFile(hf, fcs, wtf)
// Begin Checking
dupScripts, dupColors, dupSections := []ScriptSummary{}, []ScriptSummary{}, []SectionSummary{}
similaritySummarys := []SimilaritySummary{}
notFoundSections := []StyleSection{}
if params.LongScriptsCheck {
dupScripts = DupScriptsChecker(longScriptList)
LongScriptsWarning(dupScripts, hf, wtf)
}
if params.ColorsCheck {
dupColors = DupScriptsChecker(colorScriptList)
ColorScriptsWarning(dupColors, hf, wtf)
}
if params.SectionsCheck {
dupSections = DupStyleSectionsChecker(styleList)
StyleSectionsWarning(dupSections, hf, wtf)
}
if params.SimilarityCheck {
similaritySummarys = getSimilarSections()
SimilarSectionsWarning(similaritySummarys, params.SimilarityThreshold, hf, wtf)
}
if params.Unused {
notFoundSections = UnusedClassesChecker()
UnusedScriptsWarning(notFoundSections, hf, wtf)
}
t2 := time.Now()
// Results ...
fmt.Printf(DebugColor, fmt.Sprintf("\nCss Scan Completed.\n"))
csc := fmt.Sprintf("<p style='color: %s'> Css Scan Completed. </p>", hDebugColor)
writeToFile(hf, csc, wtf)
if params.LongScriptsCheck && len(dupScripts) > 0 {
fdl := fmt.Sprintf("<p style='color: %s'>Found %s duplicated long script values</p>", hWarningColor, fmt.Sprintf("<span style='color: %s'>%d</span>", hErrorColor, len(dupScripts)))
fmt.Printf(WarningColor, fmt.Sprintf("Found %s duplicated long script values\n", fmt.Sprintf(ErrorColor, fmt.Sprintf("%d", len(dupScripts)))))
writeToFile(hf, fdl, wtf)
}
if params.ColorsCheck && len(dupColors) > 0 {
fdc := fmt.Sprintf("<p style='color: %s'>Found %s duplicated colors</p>", hWarningColor,fmt.Sprintf("<span style='color: %s'>%d</span>", hErrorColor, len(dupColors)))
fmt.Printf(WarningColor, fmt.Sprintf("Found %s duplicated colors\n", fmt.Sprintf(ErrorColor, fmt.Sprintf("%d", len(dupColors)))))
writeToFile(hf, fdc, wtf)
}
if params.SectionsCheck && len(dupSections) > 0 {
fdcc := fmt.Sprintf("<p style='color: %s'>Found %s duplicated css classes</p>", hWarningColor, fmt.Sprintf("<span style='color: %s'>%d</span>", hErrorColor, len(dupSections)))
fmt.Printf(WarningColor, fmt.Sprintf("Found %s duplicated css classes\n", fmt.Sprintf(ErrorColor, fmt.Sprintf("%d", len(dupSections)))))
writeToFile(hf, fdcc, wtf)
}
if params.SimilarityCheck && len(similaritySummarys) > 0 {
fsc := fmt.Sprintf("<p style='color: %s'>Found %s similar css classes (%d%% <= sim < 100%%)</p>", hWarningColor, fmt.Sprintf("<span style='color: %s'>%d</span>", hErrorColor, len(similaritySummarys)), params.SimilarityThreshold)
fmt.Printf(WarningColor, fmt.Sprintf("Found %s similar css classes (%d%% <= sim < 100%%)\n", fmt.Sprintf(ErrorColor, fmt.Sprintf("%d", len(similaritySummarys))), params.SimilarityThreshold))
writeToFile(hf, fsc, wtf)
}
if params.Unused && len(notFoundSections) > 0 {
nrfc := fmt.Sprintf("<p style='color: %s'>Found %s css classes not referred in your js/jsx/ts/tsx/htm/html code</p>", hWarningColor, fmt.Sprintf("<span style='color: %s'>%d</span>",hErrorColor, len(notFoundSections)))
fmt.Printf(WarningColor, fmt.Sprintf("Found %s css classes not referred in your js/jsx/ts/tsx/htm/html code\n", fmt.Sprintf(ErrorColor, fmt.Sprintf("%d", len(notFoundSections)))))
writeToFile(hf, nrfc, wtf)
}
diff := t2.Sub(t1)
if !found {
nfy := "<p>Checking completed, you can also create a css-checker.yaml file to customize your scan. </p>"
fmt.Println("Checking completed, you can also create a css-checker.yaml file to customize your scan.")
writeToFile(hf, nfy, wtf)
}
ct := "<p>Time consumed (not including printing process): " + shortDur(diff) + "</p>"
fmt.Println("Time consumed (not including printing process): ", diff)
writeToFile(hf, ct, wtf)
writeToFile(hf, "</body></html>", wtf)
}
func shortDur(d time.Duration) string {
s := d.String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return s
}