Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mockgen flag --build_constraint to add //go:build directives #191

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mockgen/internal/tests/build_constraint/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package empty_interface

//go:generate mockgen -package empty_interface -destination mock.go -source input.go "-build_constraint=(linux && 386) || (darwin && !cgo) || usertag"

type Empty interface{}
44 changes: 44 additions & 0 deletions mockgen/internal/tests/build_constraint/mock.go

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

12 changes: 11 additions & 1 deletion mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.")
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
Expand Down Expand Up @@ -143,7 +144,9 @@ func main() {
}
}

g := new(generator)
g := &generator{
buildConstraint: *buildConstraint,
}
if *source != "" {
g.filename = *source
} else {
Expand Down Expand Up @@ -251,6 +254,7 @@ type generator struct {
destination string // may be empty
srcPackage, srcInterfaces string // may be empty
copyrightHeader string
buildConstraint string // may be empty

packageMap map[string]string // map from import path to package name
}
Expand Down Expand Up @@ -306,6 +310,12 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
g.p("")
}

if g.buildConstraint != "" {
g.p("//go:build %s", g.buildConstraint)
// https://pkg.go.dev/cmd/go#hdr-Build_constraints:~:text=a%20build%20constraint%20should%20be%20followed%20by%20a%20blank%20line
g.p("")
}

g.p("// Code generated by MockGen. DO NOT EDIT.")
if *writeSourceComment {
if g.filename != "" {
Expand Down