forked from qax-os/goreporter
/
report2html.go
executable file
·514 lines (481 loc) · 18.4 KB
/
report2html.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright 2017 The GoReporter Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package engine
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/golang/glog"
"github.com/json-iterator/go"
)
var (
issues int
)
// UnitTest is a struct that contains some features in a report of html.
// GoReporter HTML Report Features
//
// +----------------------------------------------------------------------+
// | Feature | Information |
// +=======================+==============================================+
// | Project | The path address of the item being detected |
// +-----------------------+----------------------------------------------+
// | Score | The score of the tested project |
// +-----------------------+----------------------------------------------+
// | Tests | Unit test results |
// +-----------------------+----------------------------------------------+
// | Date | Date assessment of the project |
// +-----------------------+----------------------------------------------+
// | Issues | Issues number of the project |
// +-----------------------+----------------------------------------------+
// | FileCount | Go file number of the peoject |
// +-----------------------+----------------------------------------------+
// | CodeLines | Number of lines of code |
// +-----------------------+----------------------------------------------+
// | CommentLines | Number of lines of Comment |
// +-----------------------+----------------------------------------------+
// | TestSummaryCoverAvg | Code cover average of all unit test |
// +-----------------------+----------------------------------------------+
// | AveragePackageCover | Package cover average of all packages |
// +-----------------------+----------------------------------------------+
// | SimpleIssues | Simpled issues number |
// +-----------------------+----------------------------------------------+
// | DeadcodeIssues | Dead code issues number |
// +-----------------------+----------------------------------------------+
// | CycloBigThan15 | Cyclo more than 15 number |
// +-----------------------+----------------------------------------------+
// | Races | Race result of all packages |
// +-----------------------+----------------------------------------------+
// | NoTests | No unit test packages information |
// +-----------------------+----------------------------------------------+
// | Simples | Simpled cases of all packages information |
// +-----------------------+----------------------------------------------+
// | Interfacers | Warns about types specific necessary |
// +-----------------------+----------------------------------------------+
// | SimpleLevel | Simple level of path |
// +-----------------------+----------------------------------------------+
// | Deadcodes | Dead code cases information |
// +-----------------------+----------------------------------------------+
// | Copycodes | Copy code cases information |
// +-----------------------+----------------------------------------------+
// | Cyclos | Cyclo of function cases information |
// +-----------------------+----------------------------------------------+
// | DepGraph | Depend graph of all packages in the project |
// +-----------------------+----------------------------------------------+
// | LastRefresh | Last refresh time of one project |
// +-----------------------+----------------------------------------------+
// | HumanizedLastRefresh | Humanized last refresh setting |
// +-----------------------+----------------------------------------------+
//
// And the HtmlData just as data for default html template. If you want to customize
// your own template file, please follow these data, or you can redefine it yourself.
type HtmlData struct {
Project string
Score int
Tests string
Date string
Issues int
FileCount int
CodeLines int
CommentLines int
TotalLines int
TestSummaryCoverAvg string
AveragePackageCover float64
SimpleIssues int
DeadcodeIssues int
CycloBigThan15 int
DepthBigThan3 int
Races []Race
NoTests string
Simples string
Interfacers string
Spells string
SimpleLevel int
Deadcodes string
Copycodes string
Cyclos string
Depths string
DepGraph template.HTML
LastRefresh time.Time `json:"last_refresh"`
HumanizedLastRefresh string `json:"humanized_last_refresh"`
}
// converterUnitTest provides function that convert unit test data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterUnitTest(structData Reporter) {
testHtmlRes := make([]Test, 0)
if result, ok := structData.Metrics["UnitTestTips"]; ok {
for pkgName, testRes := range result.Summaries {
var packageUnitTestResult PackageTest
jsoniter.Unmarshal([]byte(testRes.Description), &packageUnitTestResult)
srcLastIndex := strings.LastIndex(pkgName, hd.Project)
if srcLastIndex < len(pkgName) && srcLastIndex >= 0 {
test := Test{
Path: pkgName[srcLastIndex:],
Time: packageUnitTestResult.Time,
}
if len(packageUnitTestResult.Coverage) > 1 {
test.Cover, _ = strconv.ParseFloat(packageUnitTestResult.Coverage[:(len(packageUnitTestResult.Coverage)-1)], 64)
}
if packageUnitTestResult.IsPass {
test.Result = 1
}
testHtmlRes = append(testHtmlRes, test)
}
}
hd.TestSummaryCoverAvg = fmt.Sprintf("%0.1f", result.Percentage)
}
stringTestJson, err := jsoniter.Marshal(testHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Tests = string(stringTestJson)
}
// converterCyclo provides function that convert cyclo data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterCyclo(structData Reporter) {
cycloHtmlRes := make([]Cyclo, 0)
if result, ok := structData.Metrics["CycloTips"]; ok {
for pkgName, summary := range result.Summaries {
cycloTips := summary.Errors
cyclo := Cyclo{
Pkg: pkgName,
Size: len(cycloTips),
}
var infos []CycloInfo
for i := 0; i < len(cycloTips); i++ {
cycloTip := strings.Split(cycloTips[i].ErrorString, ":")
if len(cycloTip) >= 3 {
cycloInfo := CycloInfo{
Comp: cycloTips[i].LineNumber,
Info: strings.Join(cycloTip[0:], ":"),
}
if cycloTips[i].LineNumber > 15 {
hd.CycloBigThan15 = hd.CycloBigThan15 + 1
issues = issues + 1
}
infos = append(infos, cycloInfo)
}
}
cyclo.Info = infos
cycloHtmlRes = append(cycloHtmlRes, cyclo)
}
}
stringCycloJson, err := jsoniter.Marshal(cycloHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Cyclos = string(stringCycloJson)
}
// converterCyclo provides function that convert cyclo data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterDepth(structData Reporter) {
// convert depth result
depthHtmlRes := make([]Depth, 0)
if result, ok := structData.Metrics["DepthTips"]; ok {
for pkgName, summary := range result.Summaries {
depthTips := summary.Errors
depth := Depth{
Pkg: pkgName,
Size: len(depthTips),
}
var infos []DepthInfo
for i := 0; i < len(depthTips); i++ {
depthTip := strings.Split(depthTips[i].ErrorString, ":")
if len(depthTip) >= 3 {
depthInfo := DepthInfo{
Comp: depthTips[i].LineNumber,
Info: strings.Join(depthTip[0:], ":"),
}
if depthTips[i].LineNumber > 3 {
hd.CycloBigThan15 = hd.CycloBigThan15 + 1
issues = issues + 1
}
infos = append(infos, depthInfo)
}
}
depth.Info = infos
depthHtmlRes = append(depthHtmlRes, depth)
}
}
stringDepthJson, err := jsoniter.Marshal(depthHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Depths = string(stringDepthJson)
}
// converterSimple provides function that convert simplecode data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterSimple(structData Reporter) {
simpleHtmlRes := make([]Simple, 0)
if result, ok := structData.Metrics["SimpleTips"]; ok {
for _, summary := range result.Summaries {
simpleCodeTips := summary.Errors
for i := 0; i < len(simpleCodeTips); i++ {
simpleCodeTip := strings.Split(simpleCodeTips[i].ErrorString, ":")
if len(simpleCodeTip) == 4 {
simpecode := Simple{
Path: strings.Join(simpleCodeTip[0:3], ":"),
Info: simpleCodeTip[3],
}
simpleHtmlRes = append(simpleHtmlRes, simpecode)
issues = issues + 1
} else if len(simpleCodeTip) == 5 {
simpecode := Simple{
Path: strings.Join(simpleCodeTip[0:4], ":"),
Info: simpleCodeTip[4],
}
simpleHtmlRes = append(simpleHtmlRes, simpecode)
issues = issues + 1
}
}
}
}
stringSimpleJson, err := jsoniter.Marshal(simpleHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Simples = string(stringSimpleJson)
hd.SimpleIssues = len(simpleHtmlRes)
}
// converterInterfacer provides function that convert interfacer data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterInterfacer(structData Reporter) {
interfacerHtmlRes := make([]Interfacer, 0)
if result, ok := structData.Metrics["InterfacerTips"]; ok {
for _, summary := range result.Summaries {
interfacerCodeTips := summary.Errors
for i := 0; i < len(interfacerCodeTips); i++ {
interfacerCodeTip := strings.Split(interfacerCodeTips[i].ErrorString, ":")
if len(interfacerCodeTip) == 4 {
interfacer := Interfacer{
Path: strings.Join(interfacerCodeTip[0:3], ":"),
Info: interfacerCodeTip[3],
}
interfacerHtmlRes = append(interfacerHtmlRes, interfacer)
issues = issues + 1
} else if len(interfacerCodeTip) == 5 {
interfacer := Interfacer{
Path: strings.Join(interfacerCodeTip[0:4], ":"),
Info: interfacerCodeTip[4],
}
interfacerHtmlRes = append(interfacerHtmlRes, interfacer)
issues = issues + 1
}
}
}
}
stringInterfacerJson, err := jsoniter.Marshal(interfacerHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Interfacers = string(stringInterfacerJson)
}
// converterSpell provides function that convert spellcheck data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterSpell(structData Reporter) {
spellHtmlRes := make([]Spell, 0)
if result, ok := structData.Metrics["SpellCheckTips"]; ok {
for _, summary := range result.Summaries {
spellCodeTips := summary.Errors
for i := 0; i < len(spellCodeTips); i++ {
spellCodeTip := strings.Split(spellCodeTips[i].ErrorString, ":")
if len(spellCodeTip) == 4 {
spellcode := Spell{
Path: strings.Join(spellCodeTip[0:3], ":"),
Info: spellCodeTip[3],
}
spellHtmlRes = append(spellHtmlRes, spellcode)
} else if len(spellCodeTip) == 5 {
spellcode := Spell{
Path: strings.Join(spellCodeTip[0:4], ":"),
Info: spellCodeTip[4],
}
spellHtmlRes = append(spellHtmlRes, spellcode)
}
}
}
}
stringSpellJson, err := jsoniter.Marshal(spellHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Spells = string(stringSpellJson)
}
// converterCopy provides function that convert copycode data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterCopy(structData Reporter) {
copyHtmlRes := make([]Copycode, 0)
if result, ok := structData.Metrics["CopyCodeTips"]; ok {
for _, copyResult := range result.Summaries {
copyTips := copyResult.Errors
var copyCodePathList []string
for i := 0; i < len(copyTips); i++ {
copyCodeTip := strings.Split(copyTips[i].ErrorString, ":")
if len(copyCodeTip) >= 2 {
copyCodePathList = append(copyCodePathList, strings.Join(copyCodeTip[0:], ":"))
}
}
copycode := Copycode{
Files: strconv.Itoa(len(copyTips)),
Path: copyCodePathList,
}
copyHtmlRes = append(copyHtmlRes, copycode)
issues = issues + 1
}
}
stringCopyJson, err := jsoniter.Marshal(copyHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Copycodes = string(stringCopyJson)
}
// converterDead provides function that convert deadcode data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterDead(structData Reporter) {
deadcodeHtmlRes := make([]Deadcode, 0)
if result, ok := structData.Metrics["DeadCodeTips"]; ok {
for _, deadCodeResult := range result.Summaries {
deadCodeTips := deadCodeResult.Errors
for i := 0; i < len(deadCodeTips); i++ {
deadCodeTip := strings.Split(deadCodeTips[i].ErrorString, ":")
if len(deadCodeTip) == 4 {
deadcode := Deadcode{
Path: strings.Join(deadCodeTip[0:3], ":"),
Info: deadCodeTip[3],
}
deadcodeHtmlRes = append(deadcodeHtmlRes, deadcode)
issues = issues + 1
} else if len(deadCodeTip) == 5 {
deadcode := Deadcode{
Path: strings.Join(deadCodeTip[0:4], ":"),
Info: deadCodeTip[4],
}
deadcodeHtmlRes = append(deadcodeHtmlRes, deadcode)
issues = issues + 1
}
}
}
}
stringDeadCodeJson, err := jsoniter.Marshal(deadcodeHtmlRes)
if err != nil {
glog.Errorln(err)
}
hd.Deadcodes = string(stringDeadCodeJson)
hd.DeadcodeIssues = len(deadcodeHtmlRes)
}
// converterCount provides function that convert countcode data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterCount(structData Reporter) {
if result, ok := structData.Metrics["CountCodeTips"]; ok {
hd.FileCount, _ = strconv.Atoi(result.Summaries["FileCount"].Description)
hd.CodeLines, _ = strconv.Atoi(result.Summaries["CodeLines"].Description)
hd.CommentLines, _ = strconv.Atoi(result.Summaries["CommentLines"].Description)
hd.TotalLines, _ = strconv.Atoi(result.Summaries["TotalLines"].Description)
}
}
// converterDependGraph provides function that convert depend graph data into the
// format required in the html template.It will extract from the structData
// need to convert the data.The result will be saved in the hd's attributes.
func (hd *HtmlData) converterDependGraph(structData Reporter) {
hd.DepGraph = template.HTML(structData.Metrics["DependGraphTips"].Summaries["graph"].Description)
}
// SaveAsHtml is a function that save HtmlData as a html report.And will receive
// htmlData, projectPath, savePath and tpl parameters.
func SaveAsHtml(htmlData HtmlData, projectPath, savePath, timestamp, tpl string) {
t, err := template.New("goreporter").Parse(tpl)
if err != nil {
glog.Errorln(err)
}
var (
out bytes.Buffer
htmlpath string
)
err = t.Execute(&out, htmlData)
if err != nil {
glog.Errorln(err)
}
projectName := ProjectName(projectPath)
if savePath != "" {
htmlpath = strings.Replace(savePath+string(filepath.Separator)+projectName+"-"+timestamp+".html", string(filepath.Separator)+string(filepath.Separator), string(filepath.Separator), -1)
err = ioutil.WriteFile(htmlpath, out.Bytes(), 0666)
if err != nil {
glog.Errorln(err)
} else {
glog.Info("Html report was saved in:", htmlpath)
}
absPath, err := filepath.Abs(htmlpath)
if err != nil {
log.Println(err)
} else {
displayReport(absPath)
}
} else {
htmlpath = projectName + "-" + timestamp + ".html"
err = ioutil.WriteFile(htmlpath, out.Bytes(), 0666)
if err != nil {
glog.Errorln(err)
} else {
glog.Info("Html report was saved in:", htmlpath)
}
absPath, err := filepath.Abs("." + string(filepath.Separator) + htmlpath)
if err != nil {
log.Println(err)
} else {
displayReport(absPath)
}
}
}
// displayReport function can be open system default browser automatic.
func displayReport(filePath string) {
fileURL := fmt.Sprintf("file://%v", filePath)
log.Println("To display report", fileURL, "in browser")
var err error
switch runtime.GOOS {
case "linux":
err = callSystemCmd("xdg-open", fileURL)
case "darwin":
err = callSystemCmd("open", fileURL)
case "windows":
r := strings.NewReplacer("&", "^&")
err = callSystemCmd("cmd", "/c", "start", r.Replace(fileURL))
default:
err = fmt.Errorf("Unsupported platform,please view report file.")
}
if err != nil {
log.Println(err)
}
}
// callSystemCmd call system command opens a new browser window pointing to url.
func callSystemCmd(prog string, args ...string) error {
cmd := exec.Command(prog, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}