entc/gen: replace goimports with golang.org/x/tools/imports

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/53

Reviewed By: alexsn

Differential Revision: D17738814

fbshipit-source-id: 9865bc3c8eec766bd5c8e02db5c7afcaadb48c1f
This commit is contained in:
Ariel Mashraki
2019-10-03 12:04:47 -07:00
committed by Facebook Github Bot
parent 8e66691db5
commit 480eb714d3
4 changed files with 20 additions and 63 deletions

View File

@@ -153,7 +153,7 @@ func receiver(s string) (r string) {
for _, w := range parts[1:] {
r += w[:i]
}
if _, ok := imports[r]; !ok {
if _, ok := importPkg[r]; !ok {
return r
}
}

View File

@@ -11,7 +11,6 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"text/template"
"text/template/parse"
@@ -19,6 +18,8 @@ import (
"github.com/facebookincubator/ent/dialect/sql/schema"
"github.com/facebookincubator/ent/entc/load"
"github.com/facebookincubator/ent/schema/field"
"golang.org/x/tools/imports"
)
type (
@@ -86,7 +87,7 @@ func (g *Graph) Gen() (err error) {
b := bytes.NewBuffer(nil)
check(templates.ExecuteTemplate(b, tmpl.Name, n), "execute template %q", tmpl.Name)
target := filepath.Join(g.Config.Target, tmpl.Format(n))
check(ioutil.WriteFile(target, b.Bytes(), 0644), "create file %q", target)
check(writefmt(target, b.Bytes()), "write file %s", target)
}
}
for _, tmpl := range append(GraphTemplates[:], external...) {
@@ -100,9 +101,9 @@ func (g *Graph) Gen() (err error) {
b := bytes.NewBuffer(nil)
check(templates.ExecuteTemplate(b, tmpl.Name, g), "execute template %q", tmpl.Name)
target := filepath.Join(g.Config.Target, tmpl.Format)
check(ioutil.WriteFile(target, b.Bytes(), 0644), "create file %q", target)
check(writefmt(target, b.Bytes()), "write file %s", target)
}
return goimports(g.Config.Target)
return
}
// Describe writes a description of the graph to the given writer.
@@ -439,17 +440,15 @@ func catch(err *error) {
}
}
// goimports runs goimports on the given target.
func goimports(target string) error {
cmd := exec.Command("goimports", "-w", target)
out := bytes.NewBuffer(nil)
cmd.Stderr = out
switch err := cmd.Run(); err.(type) {
case nil:
return nil
case *exec.ExitError:
return fmt.Errorf("entc/gen: goimports: %s", out)
default:
return fmt.Errorf("entc/gen: %s", err)
func writefmt(target string, src []byte) error {
source, err := imports.Process(target, src, &imports.Options{
TabWidth: 8,
TabIndent: true,
Comments: true,
Fragment: true,
})
if err != nil {
return fmt.Errorf("running goimports %s", string(src))
}
return ioutil.WriteFile(target, source, 0644)
}

View File

@@ -113,8 +113,8 @@ var (
// the init function below initializes the templates and its
// funcs to avoid initialization loop.
templates = template.New("templates")
// imports are the import packages used for code generation.
imports = make(map[string]string)
// importPkg are the import packages used for code generation.
importPkg = make(map[string]string)
)
func init() {
@@ -129,11 +129,11 @@ func init() {
for _, spec := range f.Imports {
path, err := strconv.Unquote(spec.Path.Value)
check(err, "unquote import path")
imports[filepath.Base(path)] = path
importPkg[filepath.Base(path)] = path
}
for _, s := range drivers {
for _, path := range s.Imports {
imports[filepath.Base(path)] = path
importPkg[filepath.Base(path)] = path
}
}
}