Skip to content

Commit

Permalink
returned support for build_flags flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tulzke committed Oct 2, 2024
1 parent b14e7ad commit dcf893d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
8 changes: 2 additions & 6 deletions mockgen/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions mockgen/internal/tests/build_flags/directive.go
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions mockgen/internal/tests/build_flags/interfaces_1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build tag1

package build_flags

type Interface interface {
HelloWorld() string
}
7 changes: 7 additions & 0 deletions mockgen/internal/tests/build_flags/interfaces_2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build tag2

package build_flags

type Interface interface {
Foo()
}
54 changes: 54 additions & 0 deletions mockgen/internal/tests/build_flags/mock1/interfaces_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions mockgen/internal/tests/build_flags/mock2/interfaces_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion mockgen/package_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit dcf893d

Please sign in to comment.