-
Notifications
You must be signed in to change notification settings - Fork 0
/
chio.go
105 lines (83 loc) · 2.21 KB
/
chio.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
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/fatih/color"
"flag"
"os"
"strings"
"github.com/khanhtc1202/chio/pkg"
"github.com/khanhtc1202/chio/pkg/loaders"
"github.com/olekukonko/tablewriter"
)
type CommandParams struct {
Path string
Language string
Depth string
}
func parseParams() *CommandParams {
var modulePath string
var language string
var depth string
flag.StringVar(&modulePath, "p", ".", "path to module")
flag.StringVar(&language, "l", "go", "language(s): go")
flag.StringVar(&depth, "d", "n", "dir as module, default n-depth (n)")
flag.Parse()
return &CommandParams{
Path: modulePath,
Language: language,
Depth: depth,
}
}
func colorfulMetric(data float64) string {
if data < 0.3 {
return color.GreenString("%.3f", data)
}
if data < 0.6 {
return color.YellowString("%.3f", data)
}
return color.RedString("%.3f", data)
}
func printTable(srcPath string, modules pkg.Modules) {
var data [][]string
for _, module := range modules {
row := []string{
strings.Replace(module.RootPath, srcPath, "", -1),
fmt.Sprintf("%d", len(module.SourceFiles)),
fmt.Sprintf("%d", module.ConcreteMember),
fmt.Sprintf("%d", module.AbstractMember),
fmt.Sprintf("%d", module.FanInDep),
fmt.Sprintf("%d", module.FanOutDep),
fmt.Sprintf("%.3f", module.Abstractness()),
fmt.Sprintf("%.3f", module.Instability()),
colorfulMetric(module.Distance()),
}
data = append(data, row)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Module Path", "Files", "Concrete", "Abstract", "FanIn", "FanOut", "Abstractness", "Instability", "Distance"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
func main() {
cmdParams := parseParams()
srcPath, _ := filepath.Abs(cmdParams.Path)
language := pkg.ValueOfLanguage(cmdParams.Language)
moduleFact := pkg.ModuleFactoryBySign(cmdParams.Depth, srcPath)
modules, err := moduleFact.Load(language)
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
// loader object by language
loader := loaders.LoaderFactory(language)
// load modules
err = modules.Load(loader)
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
// print output
printTable(srcPath, modules)
}