This repository was archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (117 loc) · 2.95 KB
/
main.go
File metadata and controls
140 lines (117 loc) · 2.95 KB
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
package main
import (
"bytes"
"flag"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"golang.org/x/tools/imports"
"github.com/pkg/errors"
)
type stringList struct {
strings *[]string
}
var _ flag.Value = (*stringList)(nil)
func (l *stringList) Set(str string) error {
if str == "" {
*l.strings = nil
return nil
}
*l.strings = strings.Split(str, ",")
for i := 0; i < len(*l.strings); i++ {
(*l.strings)[i] = strings.TrimSpace((*l.strings)[i])
}
return nil
}
func (l *stringList) String() string {
return strings.Join(*l.strings, ",")
}
// naming conventions
const (
NamingConventionUnderscore = "underscore"
NamingConventionCamelCase = "camelcase"
)
var options = struct {
gopath string
out string
in string
cfg *ReifyConfig
namingConvention string
}{
gopath: os.Getenv("GOPATH"),
cfg: &ReifyConfig{TypeSpecs: make(map[string][]string)},
namingConvention: NamingConventionCamelCase,
}
func main() {
log.SetFlags(0)
flag.StringVar(&options.out, "out", options.out, "the file to write")
flag.StringVar(&options.namingConvention, "naming-convention", options.namingConvention, "the naming convention to use")
// these 2 can also be sent in positionally
flag.StringVar(&options.in, "in", options.in, "the type(s) or function(s) to reify")
flag.Var(options.cfg, "types", "the types to generate")
flag.Parse()
// positional args
offset := 0
if options.in == "" {
options.in = flag.Arg(offset)
offset++
}
for flag.Arg(offset) != "" {
options.cfg.Set(flag.Arg(offset))
offset++
}
if options.gopath == "" {
log.Fatalln("GOPATH must be set")
}
if options.in == "" || !strings.ContainsRune(options.in, '.') {
log.Fatalln("an input type or function is required")
}
if len(options.cfg.TypeSpecs) == 0 {
log.Fatalln("one or more output types are required")
}
if options.out == "" {
_, entities := getIn()
options.out = "reified_" + strings.ToLower(entities[0]) + ".go"
}
os.Remove(options.out)
err := generate()
if err != nil {
log.Fatalf("failed to generate code: %v", err)
}
}
func getIn() (pkg string, names []string) {
pos := strings.LastIndexByte(options.in, '.')
return options.in[:pos], strings.Split(options.in[pos+1:], ",")
}
func generate() error {
pkg, entities := getIn()
working := filepath.Join(options.gopath, "src", pkg)
fset := new(token.FileSet)
pkgs, err := parser.ParseDir(fset, working, nil,
parser.AllErrors|parser.ParseComments)
if err != nil {
return errors.Wrapf(err, "failed to parse package")
}
g := NewGenerator(path.Base(pkg), fset)
for _, pkg := range pkgs {
for _, file := range pkg.Files {
err = g.GenerateFromFile(file, entities, options.cfg)
if err != nil {
return err
}
}
}
var buf bytes.Buffer
g.Export(&buf)
raw := buf.Bytes()
bs, err := imports.Process(options.out, raw, nil)
if err != nil {
bs = raw
}
return ioutil.WriteFile(options.out, bs, 0755)
}