From e21995b15c6a406e4dc7ff4b2ce06eaeabffe09e Mon Sep 17 00:00:00 2001 From: Ben Boyter Date: Tue, 23 Feb 2021 11:58:44 +1100 Subject: [PATCH] csv formats --- SCC-OUTPUT-REPORT.html | 30 +++++++------- processor/formatters.go | 89 ++++++++++++++++++++++++++++++++++++++++- test-all.sh | 20 ++++++++- 3 files changed, 122 insertions(+), 17 deletions(-) diff --git a/SCC-OUTPUT-REPORT.html b/SCC-OUTPUT-REPORT.html index d48d815ca..b4c4c0f7f 100644 --- a/SCC-OUTPUT-REPORT.html +++ b/SCC-OUTPUT-REPORT.html @@ -12,12 +12,12 @@ Go 34 - 8424 - 1377 + 8511 + 1389 398 - 6649 - 1376 - 332840 + 6724 + 1384 + 334654 Java 24 @@ -93,12 +93,12 @@ Shell 3 - 1051 - 139 + 1069 + 141 84 - 828 - 92 - 37689 + 844 + 94 + 38381 C# 2 @@ -571,11 +571,11 @@ Total 168 - 25580 - 2922 + 25685 + 2936 1718 - 20940 - 2325 - 1773450 + 21031 + 2335 + 1775956 \ No newline at end of file diff --git a/processor/formatters.go b/processor/formatters.go index ca77dc2b0..88583cabd 100644 --- a/processor/formatters.go +++ b/processor/formatters.go @@ -259,6 +259,93 @@ func toJSON(input chan *FileJob) string { } func toCSV(input chan *FileJob) string { + if Files { + return toCSVFiles(input) + } + + return toCSVSummary(input) +} + +func toCSVSummary(input chan *FileJob) string { + languages := map[string]LanguageSummary{} + + for res := range input { + _, ok := languages[res.Language] + + if !ok { + files := []*FileJob{} + if Files { + files = append(files, res) + } + + languages[res.Language] = LanguageSummary{ + Name: res.Language, + Lines: res.Lines, + Code: res.Code, + Comment: res.Comment, + Blank: res.Blank, + Complexity: res.Complexity, + Count: 1, + Files: files, + Bytes: res.Bytes, + } + } else { + tmp := languages[res.Language] + files := tmp.Files + if Files { + files = append(files, res) + } + + languages[res.Language] = LanguageSummary{ + Name: res.Language, + Lines: tmp.Lines + res.Lines, + Code: tmp.Code + res.Code, + Comment: tmp.Comment + res.Comment, + Blank: tmp.Blank + res.Blank, + Complexity: tmp.Complexity + res.Complexity, + Count: tmp.Count + 1, + Files: files, + Bytes: res.Bytes + tmp.Bytes, + } + } + } + + language := []LanguageSummary{} + for _, summary := range languages { + language = append(language, summary) + } + language = sortLanguageSummary(language) + + records := [][]string{{ + "Language", + "Lines", + "Code", + "Comments", + "Blanks", + "Complexity", + "Bytes"}, + } + + for _, result := range language { + records = append(records, []string{ + result.Name, + fmt.Sprint(result.Lines), + fmt.Sprint(result.Code), + fmt.Sprint(result.Comment), + fmt.Sprint(result.Blank), + fmt.Sprint(result.Complexity), + fmt.Sprint(result.Bytes)}) + } + + b := &bytes.Buffer{} + w := csv.NewWriter(b) + _ = w.WriteAll(records) + w.Flush() + + return b.String() +} + +func toCSVFiles(input chan *FileJob) string { records := [][]string{{ "Language", "Location", @@ -286,7 +373,7 @@ func toCSV(input chan *FileJob) string { b := &bytes.Buffer{} w := csv.NewWriter(b) - w.WriteAll(records) + _ = w.WriteAll(records) w.Flush() return b.String() diff --git a/test-all.sh b/test-all.sh index a399decc9..1a921ac57 100755 --- a/test-all.sh +++ b/test-all.sh @@ -642,7 +642,25 @@ else exit fi -if ./scc --format-multi "tabular:stdout,html:stdout,csv:stdout" | grep -q "Language,Location"; then +if ./scc -f csv | grep -q "Language,Lines,Code,Comments,Blanks,Complexity,Bytes"; then + echo -e "${GREEN}PASSED csv summary" +else + echo -e "${RED}=======================================================" + echo -e "FAILED csv summary" + echo -e "=======================================================${NC}" + exit +fi + +if ./scc -f csv --by-file | grep -q "Language,Location,Filename,Lines,Code,Comments,Blanks,Complexity,Bytes"; then + echo -e "${GREEN}PASSED csv file" +else + echo -e "${RED}=======================================================" + echo -e "FAILED csv file" + echo -e "=======================================================${NC}" + exit +fi + +if ./scc --by-file --format-multi "tabular:stdout,html:stdout,csv:stdout" | grep -q "Language,Location"; then echo -e "${GREEN}PASSED format multi check" else echo -e "${RED}======================================================="