-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
175 lines (149 loc) · 2.81 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
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"io/ioutil"
"strings"
"text/template"
"github.com/wzshiming/gotype"
)
type m struct {
Name string
Doc string
Comment string
}
var (
i = flag.String("i", "", "Input package path")
p = flag.String("p", "", "Package name")
o = flag.String("o", "", "Out file")
)
func init() {
flag.Parse()
}
func main() {
imp := gotype.NewImporter()
n, err := imp.Import(*i, "")
if err != nil {
fmt.Println(err)
return
}
ts := []m{}
vs := []m{}
fs := []m{}
nc := n.NumChild()
for i := 0; i != nc; i++ {
v := n.Child(i)
if ast.IsExported(v.Name()) {
switch v.Kind() {
case gotype.Declaration:
decl := v.Declaration()
switch decl.Kind() {
default:
vs = append(vs, m{
Name: v.Name(),
Doc: comment(v.Doc().Text()),
Comment: comment(v.Comment().Text()),
})
case gotype.Func:
fs = append(fs, m{
Name: v.Name(),
Doc: comment(v.Doc().Text()),
Comment: comment(v.Comment().Text()),
})
}
case gotype.Scope, gotype.Invalid:
// No action
default:
doc := v
ts = append(ts, m{
Name: v.Name(),
Doc: comment(doc.Doc().Text()),
Comment: comment(doc.Comment().Text()),
})
}
}
}
buf := bytes.NewBuffer(nil)
if *p == "" {
*p = n.Name()
}
fn := *o
if fn == "" {
fn = (*p)
}
templateFile.ExecuteTemplate(buf, "temp", map[string]interface{}{
"PackageName": p,
"Origin": i,
"TypeSpec": ts,
"ValueSpec": vs,
"FuncSpec": fs,
"Filename": fn,
})
d, err := format.Source([]byte(buf.String()))
if err != nil {
fmt.Println(buf.String(), err)
return
}
if *o == "" {
fmt.Print(string(d))
} else {
ioutil.WriteFile(*o, d, 0666)
}
}
func comment(s string) string {
ss := []string{}
dd := strings.Split(s, "\n")
for i, v := range dd {
if v == "" && i == len(dd)-1 {
continue
}
v = strings.TrimSpace(v)
if strings.Index(v, "//") != 0 {
v = "// " + v
}
ss = append(ss, v)
}
return strings.Join(ss, "\n")
}
var templateFile = template.New("temp")
func init() {
templateFile.Parse(temp)
}
var temp = `// Code generated by "pkgimport -p {{.PackageName}} -i {{.Origin}} -o {{.Filename}}"; DO NOT EDIT.
// Install by "go get -u -v github.com/wzshiming/gotype/cmd/pkgimport";
//go:generate pkgimport -p {{.PackageName}} -i {{.Origin}} -o {{.Filename}}
package {{.PackageName}}
import (
origin "{{.Origin}}"
)
{{if .ValueSpec}}
// Value
var (
{{range .ValueSpec}}
{{.Doc}}
{{.Name}} = origin.{{.Name}} {{.Comment}}
{{end}}
)
{{end}}
{{if .TypeSpec}}
// Type
type (
{{range .TypeSpec}}
{{.Doc}}
{{.Name}} = origin.{{.Name}} {{.Comment}}
{{end}}
)
{{end}}
{{if .FuncSpec}}
// Function
var (
{{range .FuncSpec}}
{{.Doc}}
{{.Name}} = origin.{{.Name}} {{.Comment}}
{{end}}
)
{{end}}
`