diff options
author | Michael Muré <batolettre@gmail.com> | 2018-12-23 17:11:37 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-12-23 17:11:37 +0100 |
commit | 1410a1af75b1ab9ea3f980a7e372728f9a123abf (patch) | |
tree | e24db8f84c48b20158b1f1fd6d281d700421279c /vendor/github.com/99designs/gqlgen/codegen/templates/templates.go | |
parent | 8fc15a032f021c855abf66ed303c003d57c340ea (diff) | |
download | git-bug-1410a1af75b1ab9ea3f980a7e372728f9a123abf.tar.gz |
upgrade gqlgen to v0.7.1
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/codegen/templates/templates.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/codegen/templates/templates.go | 48 |
1 files changed, 23 insertions, 25 deletions
diff --git a/vendor/github.com/99designs/gqlgen/codegen/templates/templates.go b/vendor/github.com/99designs/gqlgen/codegen/templates/templates.go index df909cb5..22e5d739 100644 --- a/vendor/github.com/99designs/gqlgen/codegen/templates/templates.go +++ b/vendor/github.com/99designs/gqlgen/codegen/templates/templates.go @@ -14,21 +14,25 @@ import ( "text/template" "unicode" - "log" + "github.com/99designs/gqlgen/internal/imports" "github.com/pkg/errors" - "golang.org/x/tools/imports" ) +// this is done with a global because subtemplates currently get called in functions. Lets aim to remove this eventually. +var CurrentImports *Imports + func Run(name string, tpldata interface{}) (*bytes.Buffer, error) { t := template.New("").Funcs(template.FuncMap{ - "ucFirst": ucFirst, - "lcFirst": lcFirst, - "quote": strconv.Quote, - "rawQuote": rawQuote, - "toCamel": ToCamel, - "dump": dump, - "prefixLines": prefixLines, + "ucFirst": ucFirst, + "lcFirst": lcFirst, + "quote": strconv.Quote, + "rawQuote": rawQuote, + "toCamel": ToCamel, + "dump": dump, + "prefixLines": prefixLines, + "reserveImport": CurrentImports.Reserve, + "lookupImport": CurrentImports.Lookup, }) for filename, data := range data { @@ -149,27 +153,21 @@ func prefixLines(prefix, s string) string { } func RenderToFile(tpl string, filename string, data interface{}) error { + if CurrentImports != nil { + panic(fmt.Errorf("recursive or concurrent call to RenderToFile detected")) + } + CurrentImports = &Imports{destDir: filepath.Dir(filename)} + var buf *bytes.Buffer buf, err := Run(tpl, data) if err != nil { return errors.Wrap(err, filename+" generation failed") } - if err := write(filename, buf.Bytes()); err != nil { - return err - } - - log.Println(filename) + b := bytes.Replace(buf.Bytes(), []byte("%%%IMPORTS%%%"), []byte(CurrentImports.String()), -1) + CurrentImports = nil - return nil -} - -func gofmt(filename string, b []byte) ([]byte, error) { - out, err := imports.Process(filename, b, nil) - if err != nil { - return b, errors.Wrap(err, "unable to gofmt") - } - return out, nil + return write(filename, b) } func write(filename string, b []byte) error { @@ -178,9 +176,9 @@ func write(filename string, b []byte) error { return errors.Wrap(err, "failed to create directory") } - formatted, err := gofmt(filename, b) + formatted, err := imports.Prune(filename, b) if err != nil { - fmt.Fprintf(os.Stderr, "gofmt failed: %s\n", err.Error()) + fmt.Fprintf(os.Stderr, "gofmt failed on %s: %s\n", filepath.Base(filename), err.Error()) formatted = b } |