-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (72 loc) · 1.85 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
package main
import (
"go/token"
"os"
"runtime/debug"
"github.com/alecthomas/kong"
"github.com/sirkon/errors"
"github.com/sirkon/gogh"
"github.com/sirkon/message"
)
func main() {
argParams := os.Args[1:]
if len(argParams) > 0 && argParams[0] == "--" {
argParams = argParams[1:]
}
var args arguments
cliParser := kong.Must(
&args,
kong.Name(appName),
kong.Description("Generate codegen wrapper type for the given type with methods chaining."),
kong.UsageOnError(),
)
if _, err := cliParser.Parse(argParams); err != nil {
message.Warning(errors.Wrap(err, "parse command line arguments"))
cliParser.FatalIfErrorf(err)
}
if args.Version {
var version string
info, ok := debug.ReadBuildInfo()
if !ok {
version = "(devel)"
} else {
version = info.Main.Version
}
message.Info(appName, "version", version)
return
}
l := newSouceLoader(token.NewFileSet())
typ, err := getSourceType(l, args.Type)
if err != nil {
message.Fatal(errors.Wrap(err, "get source type info"))
}
constrs := getChainableTypeConstructors(typ.Obj().Pkg(), typ)
methods := getTypePublicChainableMethods(typ)
prj, err := gogh.New(
gogh.FancyFmt,
func(r *gogh.Imports) *gogh.Imports {
return r
},
)
if err != nil {
message.Fatal(errors.Wrap(err, "set up rendering project"))
}
p, err := prj.Package("", args.Dst.Path)
if err != nil {
message.Fatal(errors.Wrap(err, "set up rendering package").Str("pkg-path", args.Dst.Path))
}
fileName := gogh.Underscored(args.Dst.ID) + "_generated.go"
r := p.Go(fileName, gogh.Shy)
g := &generator{
src: args.Type,
dst: args.Dst,
typ: typ,
v: p.Void(),
r: r,
quoteStrings: args.StringArgsQuoted,
}
g.generate(typ, constrs, methods)
if err := prj.Render(); err != nil {
message.Fatal(errors.Wrap(err, "render generated source code"))
}
}