Skip to content

Commit

Permalink
feat: mockgen flag --build_constraint to add //go:build directi…
Browse files Browse the repository at this point in the history
…ves (#191)

Resolves #190

Note that `//go:generate` has unusual handling of flags with spaces so
the quotes have to include the flag name
([example](https://github.com/ARR4N/mock/blob/36b5e87796c7f82be7275e40bd54965c8605875c/mockgen/internal/tests/build_constraint/input.go#L3))
for complex constraints. This revealed a bug in
`--write_generate_directive`, which just prints space-delimited
`os.Args`.

The workaround of using `--copyright_file` is neither viable (it adds a
space between `//` and `go:build`) nor good practice as overloading
functionality can result in bugs due to unanticipated usage (e.g. the
aforementioned space).
  • Loading branch information
ARR4N authored Sep 25, 2024
1 parent c50d83c commit 8adc9de
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
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

0 comments on commit 8adc9de

Please sign in to comment.