forked from roblillack/mars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
172 lines (150 loc) · 4.06 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
package main
import (
"fmt"
"os"
"path"
"text/template"
"time"
"github.com/TheHippo/mars"
"github.com/codegangsta/cli"
)
func fatalf(layout string, args ...interface{}) {
fmt.Fprintf(os.Stderr, layout+"\n", args...)
os.Exit(1)
}
func main() {
app := cli.NewApp()
app.Name = "mars-gen"
app.Usage = "Code generation tool for the Mars web framework"
app.Commands = []cli.Command{
{
Name: "register-controllers",
Usage: "Generates code to register your controllers with the framework",
Action: registerControllers,
Flags: []cli.Flag{
cli.StringFlag{
Name: "n",
Value: "RegisterControllers",
Usage: "Function name to generate",
},
cli.StringFlag{
Name: "o",
Value: "register_controllers.gen.go",
Usage: "Name of the file to generate",
},
},
},
{
Name: "reverse-routes",
Usage: "Generates code that allows generating reverse routes",
Action: reverseRoutes,
Flags: []cli.Flag{
cli.StringFlag{
Name: "n",
Value: "routes",
Usage: "Package name to generate",
},
cli.StringFlag{
Name: "o",
Value: "routes.gen.go",
Usage: "Path of the file to generate",
},
},
},
}
app.Run(os.Args)
}
func registerControllers(ctx *cli.Context) {
dir := "."
if len(ctx.Args()) > 0 {
dir = ctx.Args()[0]
}
sourceInfo, procErr := ProcessSource(dir)
if procErr != nil {
fatalf(procErr.Error())
}
generateSources(registerTemplate, path.Join(dir, ctx.String("o")), map[string]interface{}{
"packageName": sourceInfo.PackageName,
"functionName": ctx.String("n"),
"controllers": sourceInfo.ControllerSpecs(),
"ImportPaths": sourceInfo.CalcImportAliases(),
"time": time.Now(),
})
}
func reverseRoutes(ctx *cli.Context) {
dir := "."
if len(ctx.Args()) > 0 {
dir = ctx.Args()[0]
}
sourceInfo, procErr := ProcessSource(dir)
if procErr != nil {
fatalf(procErr.Error())
}
generateSources(routesTemplate, ctx.String("o"), map[string]interface{}{
"packageName": ctx.String("n"),
"controllers": sourceInfo.ControllerSpecs(),
"ImportPaths": sourceInfo.CalcImportAliases(),
"time": time.Now(),
})
}
func generateSources(tpl, filename string, templateArgs map[string]interface{}) {
sourceCode := mars.ExecuteTemplate(template.Must(template.New("").Parse(tpl)), templateArgs)
os.MkdirAll(path.Dir(filename), 0755)
// Create the file
file, err := os.Create(filename)
defer file.Close()
if err != nil {
fatalf("Failed to create file: %v", err)
}
_, err = file.WriteString(sourceCode)
if err != nil {
fatalf("Failed to write to file: %v", err)
}
}
const registerTemplate = `// DO NOT EDIT -- code generated by mars-gen
package {{.packageName}}
import (
"reflect"
"github.com/TheHippo/mars"{{range $k, $v := $.ImportPaths}}
{{$v}} "{{$k}}"{{end}}
)
var (
// So compiler won't complain if the generated code doesn't reference reflect package...
_ = reflect.Invalid
)
func {{.functionName}}() {
{{range $i, $c := .controllers}}
mars.RegisterController((*{{.StructName}})(nil),
[]*mars.MethodType{
{{range .MethodSpecs}}&mars.MethodType{
Name: "{{.Name}}",
Args: []*mars.MethodArg{ {{range .Args}}
&mars.MethodArg{Name: "{{.Name}}", Type: reflect.TypeOf((*{{index $.ImportPaths .ImportPath | .TypeExpr.TypeName}})(nil)) },{{end}}
},
},
{{end}}
})
{{end}}
}
`
const routesTemplate = `// DO NOT EDIT -- code generated by mars-gen
package {{.packageName}}
import (
"github.com/TheHippo/mars"{{range $k, $v := $.ImportPaths}}
{{$v}} "{{$k}}"{{end}}
)
{{range $i, $c := .controllers}}
type t{{.StructName}} struct {}
var {{.StructName}} t{{.StructName}}
{{range .MethodSpecs}}
func (_ t{{$c.StructName}}) {{.Name}}({{range .Args}}
{{.Name}}_ {{if .ImportPath}}{{index $.ImportPaths .ImportPath | .TypeExpr.TypeName}}{{else}}{{.TypeExpr.TypeName ""}}{{end}},{{end}}
) string {
args := make(map[string]string)
{{range .Args}}
mars.Unbind(args, "{{.Name}}", {{.Name}}_){{end}}
return mars.MainRouter.Reverse("{{$c.StructName}}.{{.Name}}", args).Url
}
{{end}}
{{end}}
`