Skip to content

Commit

Permalink
Build mockgen in makefile once rather than each invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Greenleaf authored and jacobgreenleaf committed Feb 16, 2019
1 parent 3f09164 commit fa94472
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ EXAMPLE_SERVICES_DIR = $(EXAMPLE_BASE_DIR)build/services/
EXAMPLE_SERVICES = $(sort $(dir $(wildcard $(EXAMPLE_SERVICES_DIR)*/)))
GOIMPORTS = "$(PWD)/vendor/golang.org/x/tools/cmd/goimports"
GOBINDATA = "$(PWD)/vendor/github.com/jteeuwen/go-bindata/go-bindata"
GOMOCK = "$(PWD)/vendor/github.com/golang/mock/mockgen"

.PHONY: install
install:
Expand All @@ -33,6 +34,7 @@ install:
glide install
go build -o $(GOIMPORTS)/goimports ./vendor/golang.org/x/tools/cmd/goimports/
go build -o $(GOBINDATA)/go-bindata ./vendor/github.com/jteeuwen/go-bindata/go-bindata/
go build -o $(GOMOCK)/mockgen ./vendor/github.com/golang/mock/mockgen/

.PHONY: check-licence
check-licence:
Expand Down Expand Up @@ -110,7 +112,7 @@ generate:
@./node_modules/.bin/uber-licence --file "production.gen.go" --dir "config" > /dev/null
@$(GOBINDATA)/go-bindata -pkg templates -nocompress -modtime 1 -prefix codegen/templates -o codegen/template_bundle/template_files.go codegen/templates/...
@gofmt -w -e -s "codegen/template_bundle/template_files.go"
@PATH=$(GOIMPORTS):$(PATH) bash ./scripts/generate.sh
@PATH=$(GOIMPORTS):$(GOMOCK):$(PATH) bash ./scripts/generate.sh

.PHONY: check-generate
check-generate:
Expand Down
38 changes: 1 addition & 37 deletions codegen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import (
"github.com/golang/mock/mockgen/model"
"github.com/pkg/errors"
"go/token"
"os"
"os/exec"
"path"
"runtime"
"sort"
"strconv"
"strings"
Expand All @@ -40,47 +38,13 @@ const (

// MockgenBin is a struct abstracts the mockgen binary built from mockgen package in vendor
type MockgenBin struct {
// Bin is the absolute path to the mockgen binary built from vendor
Bin string

pkgHelper *PackageHelper
tmpl *Template
}

// NewMockgenBin builds the mockgen binary from vendor directory
func NewMockgenBin(h *PackageHelper, t *Template) (*MockgenBin, error) {
// we assume that the vendor directory is flattened as Glide does
mockgenDir := path.Join("vendor", mockgenPkg)
if _, err := os.Stat(mockgenDir); err != nil {
return nil, errors.Wrapf(
err, "error finding mockgen package in the vendor dir: %q does not exist", mockgenDir,
)
}

var mockgenBin = "mockgen.bin"
if runtime.GOOS == "windows" {
// Windows won't execute a program unless it has a ".exe" suffix.
mockgenBin += ".exe"
}

cmd := exec.Command("go", "build", "-o", mockgenBin, ".")
cmd.Dir = mockgenDir

var stderr, stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, errors.Wrapf(
err,
"error running command %q in %s: %s",
strings.Join(cmd.Args, " "),
mockgenDir,
stderr.String(),
)
}

return &MockgenBin{
Bin: path.Join(mockgenDir, mockgenBin),
pkgHelper: h,
tmpl: t,
}, nil
Expand All @@ -89,7 +53,7 @@ func NewMockgenBin(h *PackageHelper, t *Template) (*MockgenBin, error) {
// GenMock generates mocks for given module instance, pkg is the package name of the generated mocks,
// and intf is the interface name to generate mock for
func (m MockgenBin) GenMock(importPath, pkg, intf string) ([]byte, error) {
cmd := exec.Command(m.Bin, "-package", pkg, importPath, intf)
cmd := exec.Command("mockgen", "-package", pkg, importPath, intf)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down

0 comments on commit fa94472

Please sign in to comment.