Skip to content

Commit

Permalink
Remove goimports binary dependency (#54)
Browse files Browse the repository at this point in the history
* go.mod: add golang.org/x/tools@654352759326 and cleanup

* generator: defer declare f

* generator: remove goimports binary dependency

* Makefile: remove installing goimports from deps target
  • Loading branch information
zchee authored Dec 9, 2020
1 parent 07e3eaf commit 52123fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 54 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ regen: tplbin/templates.go ## regenerate template bin

deps:
go get -u github.com/jessevdk/go-assets-builder
go get -u golang.org/x/tools/cmd/goimports

tplbin/templates.go: $(wildcard templates/*.tpl)
go-assets-builder \
Expand Down
38 changes: 29 additions & 9 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"text/template"

"golang.org/x/tools/imports"

"go.mercari.io/yo/internal"
templates "go.mercari.io/yo/tplbin"
)
Expand Down Expand Up @@ -198,6 +200,13 @@ func (g *Generator) getFile(ds *basicDataSet, t *TBuf) (*os.File, error) {
return f, nil
}

// importsOptions is the same as x/tools/cmd/goimports options except Fragment.
var importsOptions = &imports.Options{
TabWidth: 8,
TabIndent: true,
Comments: true,
}

// writeTypes writes the generated definitions.
func (g *Generator) writeTypes(ds *basicDataSet) error {
var err error
Expand All @@ -209,14 +218,13 @@ func (g *Generator) writeTypes(ds *basicDataSet) error {

// loop, writing in order
for _, t := range out {
var f *os.File

// check if generated template is only whitespace/empty
bufStr := strings.TrimSpace(t.Buf.String())
if len(bufStr) == 0 {
continue
}

var f *os.File
// get file and filename
f, err = g.getFile(ds, &t)
if err != nil {
Expand All @@ -234,20 +242,32 @@ func (g *Generator) writeTypes(ds *basicDataSet) error {
}
}

// build goimports parameters, closing files
params := []string{"-w"}
// format by imports, closing files
for k, f := range g.files {
params = append(params, k)

// close
err = f.Close()
if err != nil {
return err
}

// imports.Process needs absolute filepath for accurate fix
abs, err := filepath.Abs(k)
if err != nil {
return err
}
// format
formatted, err := imports.Process(abs, nil, importsOptions)
if err != nil {
return err
}

// since abs file exists, set perm to 0
if err := ioutil.WriteFile(abs, formatted, 0); err != nil {
return err
}
}

// process written files with goimports
return exec.Command("goimports", params...).Run()
return nil
}

// ExecuteTemplate loads and parses the supplied template with name and
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ require (
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/jinzhu/inflection v1.0.0
github.com/kenshaw/snaker v0.1.0
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/tools v0.0.0-20201204222352-654352759326
google.golang.org/api v0.21.0
google.golang.org/genproto v0.0.0-20200416231807-8751e049a2a0
google.golang.org/grpc v1.28.1
Expand Down
Loading

0 comments on commit 52123fd

Please sign in to comment.