This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (133 loc) · 3.58 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
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"io/ioutil"
stdlog "log"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/tools/go/loader"
)
const genericsPackageName = "github.com/rastech/gogen/generic"
var (
quiet bool
verbose bool
)
func main() {
stdlog.SetPrefix("[gogen] ")
stdlog.SetFlags(0)
outdir := flag.String("o", "", "Output directory (relative). Required.")
flag.BoolVar(&quiet, "q", false, "Suppress logging output.")
flag.BoolVar(&verbose, "v", false, "Increase logging output.")
noGi := flag.Bool("no-gi", false, "Do not generate a .gitignore in the output directory.")
help := flag.Bool("h", false, "Print help.")
flag.Parse()
if *help || flag.NArg() < 2 {
me := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s usage:\n\t%s [-q] [-h] [--no-gi] -o OUT_DIR IN_PKG [InType=OutType ...]\n\nFor more usage details see https://github.com/rastech/gogen/blob/master/README.md\n\nFlags:\n", me, me)
flag.PrintDefaults()
os.Exit(2)
}
defer func() {
if r := recover(); r != nil {
vlog("Caught panic. Exiting to system with error.")
os.Exit(1)
}
}()
absPkgPath, err := filepath.Abs(*outdir)
handle(err)
newPkgName := filepath.Base(absPkgPath)
typeNames := make(map[string]string, flag.NArg()-1)
for _, v := range flag.Args()[1:] {
split := strings.SplitN(v, "=", 2)
typeNames[split[0]] = split[1]
}
var conf loader.Config
log("Loading and type checking generic code...")
conf.Import(flag.Arg(0))
conf.Import(genericsPackageName)
program, err := conf.Load()
handle(err)
log("Determining generic type...")
genericPkgInfo := program.Package(genericsPackageName)
gWlkr := genericWalker{
pkgInfo: genericPkgInfo,
genericType: nil,
}
for _, fileAst := range genericPkgInfo.Files {
ast.Walk(&gWlkr, fileAst)
if gWlkr.genericType != nil {
break
}
}
if gWlkr.genericType == nil {
err = fmt.Errorf("Couldn't determine generic type!")
}
handle(err)
pkgInfo := program.Package(flag.Arg(0))
tdir := filepath.Join(absPkgPath, fmt.Sprintf("gogen-%v", time.Now().Nanosecond()))
os.MkdirAll(tdir, 0744)
defer func() {
vlog("Cleaning up...")
err := os.RemoveAll(tdir)
handle(err)
vlog("Complete.")
}()
log("Generating type-specific code...")
wlkr := walker{typeNames, pkgInfo, gWlkr.genericType}
var buf bytes.Buffer
for _, fileAst := range pkgInfo.Files {
buf.Reset()
ast.Walk((*typeWalker)(&wlkr), fileAst)
ast.Walk((*nameWalker)(&wlkr), fileAst)
fileAst.Name.Name = newPkgName
err := format.Node(&buf, program.Fset, fileAst)
handle(err)
b, err := format.Source(buf.Bytes())
handle(err)
fname := filepath.Join(tdir, filepath.Base(program.Fset.Position(fileAst.Pos()).Filename))
err = ioutil.WriteFile(fname, b, 0644)
handle(err)
}
log("Type-checking generated code...")
var genConf loader.Config
genConf.CreateFromFiles(newPkgName, pkgInfo.Files...)
program, err = genConf.Load()
handle(err)
if !*noGi {
vlog("Creating .gitignore...")
ioutil.WriteFile(filepath.Join(tdir, ".gitignore"), []byte("*\n"), 0644)
} else {
vlog("Skipping .gitignore creation...")
}
vlog("Copying generated code to output...")
f, err := os.Open(tdir)
handle(err)
defer f.Close()
fi, err := f.Readdir(0)
handle(err)
for _, v := range fi {
err := os.Rename(filepath.Join(tdir, v.Name()), filepath.Join(absPkgPath, v.Name()))
handle(err)
}
}
func vlog(s string, args ...interface{}) {
if verbose {
log(s, args...)
}
}
func log(s string, args ...interface{}) {
if !quiet {
stdlog.Printf(s+"\n", args...)
}
}
func handle(err error) {
if err != nil {
stdlog.Panicf("ERROR: %v", err)
}
}