diff --git a/mockgen/deprecated.go b/mockgen/deprecated.go index ee7fa21..0b45a2e 100644 --- a/mockgen/deprecated.go +++ b/mockgen/deprecated.go @@ -7,15 +7,13 @@ import ( ) const ( - deprecatedFlagProgOnly = "prog_only" - deprecatedFlagExecOnly = "exec_only" - deprecatedFlagBuildFlags = "build_flags" + deprecatedFlagProgOnly = "prog_only" + deprecatedFlagExecOnly = "exec_only" ) var ( _ = flag.Bool("prog_only", false, "DEPRECATED (reflect mode) Only generate the reflection program; write it to stdout and exit.") _ = flag.String("exec_only", "", "DEPRECATED (reflect mode) If set, execute this reflection program.") - _ = flag.String("build_flags", "", "DEPRECATED (reflect mode) Additional flags for go build.") ) // notifyAboutDeprecatedFlags prints a warning message for a deprecated flags if they are set. @@ -29,8 +27,6 @@ func notifyAboutDeprecatedFlags() { logger.Println("The -prog_only flag is deprecated and has no effect.", resetColorPostfix) case deprecatedFlagExecOnly: logger.Println("The -exec_only flag is deprecated and has no effect.", resetColorPostfix) - case deprecatedFlagBuildFlags: - logger.Println("The -build_flags flag is deprecated and has no effect.", resetColorPostfix) } }) } diff --git a/mockgen/internal/tests/build_flags/directive.go b/mockgen/internal/tests/build_flags/directive.go new file mode 100644 index 0000000..4bfc9e9 --- /dev/null +++ b/mockgen/internal/tests/build_flags/directive.go @@ -0,0 +1,6 @@ +package build_flags + +// one build flag +//go:generate mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface +// multiple build flags +//go:generate mockgen -destination=mock2/interfaces_mock.go "-build_flags=-race -tags=tag2" . Interface diff --git a/mockgen/internal/tests/build_flags/interfaces_1.go b/mockgen/internal/tests/build_flags/interfaces_1.go new file mode 100644 index 0000000..e159cd6 --- /dev/null +++ b/mockgen/internal/tests/build_flags/interfaces_1.go @@ -0,0 +1,7 @@ +//go:build tag1 + +package build_flags + +type Interface interface { + HelloWorld() string +} diff --git a/mockgen/internal/tests/build_flags/interfaces_2.go b/mockgen/internal/tests/build_flags/interfaces_2.go new file mode 100644 index 0000000..5404cf7 --- /dev/null +++ b/mockgen/internal/tests/build_flags/interfaces_2.go @@ -0,0 +1,7 @@ +//go:build tag2 + +package build_flags + +type Interface interface { + Foo() +} diff --git a/mockgen/internal/tests/build_flags/mock1/interfaces_mock.go b/mockgen/internal/tests/build_flags/mock1/interfaces_mock.go new file mode 100644 index 0000000..7ca80bd --- /dev/null +++ b/mockgen/internal/tests/build_flags/mock1/interfaces_mock.go @@ -0,0 +1,54 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/build_flags (interfaces: Interface) +// +// Generated by this command: +// +// mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface +// + +// Package mock_build_flags is a generated GoMock package. +package mock_build_flags + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockInterface is a mock of Interface interface. +type MockInterface struct { + ctrl *gomock.Controller + recorder *MockInterfaceMockRecorder + isgomock struct{} +} + +// MockInterfaceMockRecorder is the mock recorder for MockInterface. +type MockInterfaceMockRecorder struct { + mock *MockInterface +} + +// NewMockInterface creates a new mock instance. +func NewMockInterface(ctrl *gomock.Controller) *MockInterface { + mock := &MockInterface{ctrl: ctrl} + mock.recorder = &MockInterfaceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { + return m.recorder +} + +// HelloWorld mocks base method. +func (m *MockInterface) HelloWorld() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HelloWorld") + ret0, _ := ret[0].(string) + return ret0 +} + +// HelloWorld indicates an expected call of HelloWorld. +func (mr *MockInterfaceMockRecorder) HelloWorld() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HelloWorld", reflect.TypeOf((*MockInterface)(nil).HelloWorld)) +} diff --git a/mockgen/internal/tests/build_flags/mock2/interfaces_mock.go b/mockgen/internal/tests/build_flags/mock2/interfaces_mock.go new file mode 100644 index 0000000..4f21187 --- /dev/null +++ b/mockgen/internal/tests/build_flags/mock2/interfaces_mock.go @@ -0,0 +1,52 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/build_flags (interfaces: Interface) +// +// Generated by this command: +// +// mockgen -destination=mock2/interfaces_mock.go -build_flags=-race -tags=tag2 . Interface +// + +// Package mock_build_flags is a generated GoMock package. +package mock_build_flags + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockInterface is a mock of Interface interface. +type MockInterface struct { + ctrl *gomock.Controller + recorder *MockInterfaceMockRecorder + isgomock struct{} +} + +// MockInterfaceMockRecorder is the mock recorder for MockInterface. +type MockInterfaceMockRecorder struct { + mock *MockInterface +} + +// NewMockInterface creates a new mock instance. +func NewMockInterface(ctrl *gomock.Controller) *MockInterface { + mock := &MockInterface{ctrl: ctrl} + mock.recorder = &MockInterfaceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { + return m.recorder +} + +// Foo mocks base method. +func (m *MockInterface) Foo() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Foo") +} + +// Foo indicates an expected call of Foo. +func (mr *MockInterfaceMockRecorder) Foo() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockInterface)(nil).Foo)) +} diff --git a/mockgen/package_mode.go b/mockgen/package_mode.go index 540a83e..abc9c7a 100644 --- a/mockgen/package_mode.go +++ b/mockgen/package_mode.go @@ -2,13 +2,19 @@ package main import ( "errors" + "flag" "fmt" "go/types" + "strings" "go.uber.org/mock/mockgen/model" "golang.org/x/tools/go/packages" ) +var ( + buildFlags = flag.String("build_flags", "", "(package mode) Additional flags for go build.") +) + type packageModeParser struct { pkgName string } @@ -34,8 +40,14 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (* } func (p *packageModeParser) loadPackage(packageName string) (*packages.Package, error) { + var buildFlagsSet []string + if *buildFlags != "" { + buildFlagsSet = strings.Split(*buildFlags, " ") + } + cfg := &packages.Config{ - Mode: packages.NeedDeps | packages.NeedImports | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedEmbedFiles, + Mode: packages.NeedDeps | packages.NeedImports | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedEmbedFiles, + BuildFlags: buildFlagsSet, } pkgs, err := packages.Load(cfg, packageName) if err != nil {