-
Notifications
You must be signed in to change notification settings - Fork 13
/
gen.go
141 lines (117 loc) · 3.16 KB
/
gen.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
package main
import (
"bufio"
"log"
"os"
"path/filepath"
"text/template"
"time"
gozip "github.com/straightdave/gozip/lib"
"github.com/straightdave/lesphina/v2"
)
func mustGenProtoTypeFile(files []*SourceFile, destDir string) {
if err := genProtoTypeFile(files, destDir); err != nil {
log.Fatal(err)
}
}
func genProtoTypeFile(files []*SourceFile, destDir string) error {
var lesTypeList []string
// get all gRPC request / response types
for _, file := range files {
allStructs := file.Les.Query().ByKind(lesphina.KindStruct).All()
for _, s := range allStructs {
str, ok := s.(*lesphina.Struct)
if !ok {
continue
}
// should have ProtoMessage() method,
// which makes it an ProtoMessage (interface)
for _, m := range str.GetMethods() {
if m.Name == "ProtoMessage" {
lesTypeList = append(lesTypeList, s.GetName())
break
}
}
}
}
newFile := filepath.Join(destDir, "z_types.go")
log.Println("generating types file:", newFile)
tpl, err := template.New("meta").Parse(gozip.DecompressString(_compressedMeta))
if err != nil {
return err
}
tf, err := os.Create(newFile)
if err != nil {
return err
}
defer tf.Close()
return tpl.Execute(tf, struct {
GeneratedTime string
LesDump string
List []string
}{
GeneratedTime: time.Now().Format(time.UnixDate),
LesDump: mergeLesphina(files).DumpString(),
List: lesTypeList,
})
}
func mergeLesphina(files []*SourceFile) *lesphina.Lesphina {
totalMeta := new(lesphina.Meta)
for _, file := range files {
meta := file.Les.Meta
totalMeta.Consts = append(totalMeta.Consts, meta.Consts...)
totalMeta.Functions = append(totalMeta.Functions, meta.Functions...)
totalMeta.Imports = append(totalMeta.Imports, meta.Imports...)
totalMeta.Interfaces = append(totalMeta.Interfaces, meta.Interfaces...)
totalMeta.Structs = append(totalMeta.Structs, meta.Structs...)
totalMeta.Vars = append(totalMeta.Vars, meta.Vars...)
totalMeta.NumConst += totalMeta.NumConst
totalMeta.NumFunction += totalMeta.NumFunction
totalMeta.NumImport += totalMeta.NumImport
totalMeta.NumInterface += totalMeta.NumInterface
totalMeta.NumStruct += totalMeta.NumStruct
totalMeta.NumVar += totalMeta.NumVar
}
return &lesphina.Lesphina{
Meta: totalMeta,
}
}
func mustRestoreFile(raw, dir, file string) {
if err := restoreFile(raw, dir, file); err != nil {
log.Fatal(err)
}
}
func restoreFile(raw, dirName, fileName string) error {
fullName := filepath.Join(dirName, fileName)
log.Println("creating new source file:", fullName)
f, err := os.Create(fullName)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
w.WriteString(gozip.DecompressString(raw))
return w.Flush()
}
func mustGenModFile(modName, dir string) {
if err := genModFile(modName, dir); err != nil {
log.Fatal(err)
}
}
func genModFile(modName, dir string) error {
newFile := filepath.Join(dir, "go.mod")
tpl, err := template.New("mod").Parse(gozip.DecompressString(_modFile))
if err != nil {
return err
}
tf, err := os.Create(newFile)
if err != nil {
return err
}
defer tf.Close()
return tpl.Execute(tf, struct {
ModName string
}{
ModName: modName,
})
}