-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
166 lines (144 loc) · 3.95 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
package main
import (
"flag"
"fmt"
"os"
"io"
"io/ioutil"
"bufio"
"strings"
"path/filepath"
)
var (
pkgName string
pkgPath string
dotFile string
depth int
)
func depthParse(s, tag string) string {
lens := len(tag)
src := s[lens:]
if len(src) != 0 && src[0] == '/' {
src = src[1:]
}
parts := strings.Split(src, "/")
pkgs := strings.Split(pkgName, "/")
ret := pkgs[len(pkgs)-1]
for i:=0; i<depth && i<len(parts); i++ {
if strings.Contains(parts[i], ".go") {
break
}
ret += "/" + parts[i]
}
//fmt.Println(ret)
return ret
}
func analyseGoFile(path string) error {
dst := depthParse(path, pkgPath)
srcMap := make(map[string]bool)
f, err := os.Open(path)
if err != nil {
fmt.Printf("Error: %s\n", err)
return err
}
defer f.Close()
br := bufio.NewReader(f)
flag := false
for {
a, _, err := br.ReadLine()
if err == io.EOF {
break
}
line := strings.Trim(string(a), " ")
if line == "import (" {
flag = true
}
if line == ")" {
flag = false
}
if flag == true {
if indx := strings.Index(line, pkgName); indx != -1 {
srcPkg := depthParse(strings.Trim(line[indx:], "\""), pkgName)
if _, ok := srcMap[srcPkg]; !ok {
srcMap[srcPkg] = true
}
}
}
}
fd, _ := os.OpenFile(dotFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
defer fd.Close()
for k, _ := range srcMap {
fd.Write([]byte(fmt.Sprintf("\t\"%s\" -> \"%s\"\n", k, dst)))
}
return nil
}
func analyseDir(dir string) error {
fd, _ := os.OpenFile(dotFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
fd.Write([]byte("digraph G {\n"))
fd.Close()
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err)
return err
}
if !info.IsDir() {
if strings.Contains(path, ".go") && !strings.Contains(path, pkgPath + "/vendor"){
//fmt.Printf("visited file: %q\n", path)
analyseGoFile(path)
}
}
return nil
})
if err != nil {
return err
}
fd, _ = os.OpenFile(dotFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
fd.Write([]byte("}\n"))
fd.Close()
return nil
}
func processDotFile() {
fileContent := make(map[string]bool)
f, err := os.Open(dotFile)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
br := bufio.NewReader(f)
for {
a, _, err := br.ReadLine()
if err == io.EOF {
break
}
if _, ok := fileContent[string(a)]; !ok {
fileContent[string(a)] = true
}
}
f.Close()
content := "digraph G {\n"
for k, _ := range fileContent {
if k == "digraph G {" || k == "}" {
continue
}
content += k + "\n"
}
content += "}\n"
ioutil.WriteFile(dotFile, []byte(content), 0644)
}
func main() {
flag.StringVar(&pkgName, "pkg_name", "", "the package name")
flag.StringVar(&pkgPath, "pkg_path", "", "the package path")
flag.StringVar(&dotFile, "dot_file_path", "godag.dot", "we generated the .dot file path")
flag.IntVar(&depth, "depth", 1, "dependencies analyse depth")
flag.Parse()
if len(pkgPath) == 0 || len(pkgName) == 0 {
fmt.Println("You MUST assign the package name and path")
return
}
fmt.Println(pkgName)
fmt.Println(pkgPath)
fmt.Println(dotFile)
analyseDir(pkgPath)
processDotFile()
//analyseFile("/Users/kltao/code/go/src/github.com/legendtkl/godag/main.go")
//depthParse("/Users/kltao/code/go/src/github.com/astaxie/beego/session/session.go")
}