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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// +build ignore
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
)
type (
Type struct {
Notation string
Spec string
Prototype string
}
TypeMap map[string]Type
Imports []string
Config struct {
Package string
Imports Imports
Args string // arguments that we were invoked with; TODO(jdef) rename this to Flags?
Types TypeMap
}
)
func (i *Imports) String() string {
if i == nil {
return ""
}
return fmt.Sprintf("%#v", ([]string)(*i))
}
func (i *Imports) Set(s string) error {
*i = append(*i, s)
return nil
}
func (tm *TypeMap) Set(s string) error {
tok := strings.SplitN(s, ":", 3)
if len(tok) < 2 {
return errors.New("expected {notation}:{type-spec} syntax, instead of " + s)
}
if *tm == nil {
*tm = make(TypeMap)
}
t := (*tm)[tok[0]]
t.Notation, t.Spec = tok[0], tok[1]
if t.Notation == "" {
return fmt.Errorf("type notation in %q may not be an empty string", s)
}
if t.Spec == "" {
return fmt.Errorf("type specification in %q may not be an empty string", s)
}
if len(tok) == 3 {
t.Prototype = tok[2]
if t.Prototype == "" {
return fmt.Errorf("prototype specification in %q may not be an empty string", s)
}
}
(*tm)[tok[0]] = t
return nil
}
func (tm *TypeMap) String() string {
if tm == nil {
return ""
}
return fmt.Sprintf("%#v", *tm)
}
func (c *Config) Var(notation string, names ...string) string {
t := c.Type(notation)
if t == "" || len(names) == 0 {
return ""
}
return "var " + strings.Join(names, ",") + " " + t
}
func (c *Config) Arg(notation, name string) string {
t := c.Type(notation)
if t == "" {
return ""
}
if name == "" {
return t
}
if strings.HasSuffix(name, ",") {
return strings.TrimSpace(name[:len(name)-1]+" "+t) + ", "
}
return name + " " + t
}
func (c *Config) Ref(notation, name string) (string, error) {
t := c.Type(notation)
if t == "" || name == "" {
return "", nil
}
if strings.HasSuffix(name, ",") {
if len(name) < 2 {
return "", errors.New("expected ref name before comma")
}
return name[:len(name)-1] + ", ", nil
}
return name, nil
}
func (c *Config) RequireType(notation string) (string, error) {
_, ok := c.Types[notation]
if !ok {
return "", fmt.Errorf("type %q is required but not specified", notation)
}
return "", nil
}
func (c *Config) RequirePrototype(notation string) (string, error) {
t, ok := c.Types[notation]
if !ok {
// needed for optional types: don't require the prototype if the optional type is not defined
return "", nil
}
if t.Prototype == "" {
return "", fmt.Errorf("prototype for type %q is required but not specified", notation)
}
return "", nil
}
func (c *Config) Type(notation string) string {
t, ok := c.Types[notation]
if !ok {
return ""
}
return t.Spec
}
func (c *Config) Prototype(notation string) string {
t, ok := c.Types[notation]
if !ok {
return ""
}
return t.Prototype
}
func (c *Config) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&c.Package, "package", c.Package, "destination package")
fs.Var(&c.Imports, "import", "packages to import")
fs.Var(&c.Types, "type", "auxilliary type mappings in {notation}:{type-spec}:{prototype-expr} format")
}
func NewConfig() *Config {
var (
c = Config{
Package: os.Getenv("GOPACKAGE"),
}
)
return &c
}
func Run(src, test *template.Template, args ...string) {
if len(args) < 1 {
panic(errors.New("expected at least one arg"))
}
var (
c = NewConfig()
defaultOutput = "foo.go"
output string
)
if c.Package != "" {
defaultOutput = c.Package + "_generated.go"
}
fs := flag.NewFlagSet(args[0], flag.PanicOnError)
fs.StringVar(&output, "output", output, "path of the to-be-generated file")
c.AddFlags(fs)
if err := fs.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
fs.PrintDefaults()
}
panic(err)
}
c.Args = strings.Join(args[1:], " ")
if c.Package == "" {
c.Package = "foo"
}
if output == "" {
output = defaultOutput
}
genmap := make(map[string]*template.Template)
if src != nil {
genmap[output] = src
}
if test != nil {
testOutput := output + "_test"
if strings.HasSuffix(output, ".go") {
testOutput = output[:len(output)-3] + "_test.go"
}
genmap[testOutput] = test
}
if len(genmap) == 0 {
panic(errors.New("neither src or test templates were provided"))
}
Generate(genmap, c, func(err error) { panic(err) })
}
func Generate(items map[string]*template.Template, data interface{}, eh func(error)) {
for filename, t := range items {
func() {
f, err := os.Create(filename)
if err != nil {
eh(err)
return
}
closer := safeClose(f)
defer closer()
log.Println("generating file", filename)
err = t.Execute(f, data)
if err != nil {
eh(err)
}
err = f.Sync()
if err != nil {
eh(err)
}
err = closer()
if err != nil {
eh(err)
}
}()
}
}
func safeClose(c io.Closer) func() error {
var s bool
return func() error {
if s {
return nil
}
s = true
return c.Close()
}
}
|