Skip to content

Commit

Permalink
feat: add flag to add go:generate directive in the generated file (#46)
Browse files Browse the repository at this point in the history
As you can read by the PR title, I added a flag (false by default) that
allows mockgen to add the go:generate instruction as part of the
generated file.

This is something I was looking to see supported, and I finally had some
time to add this tiny feature.
It would save a lot of time, and it is similar to what other tools
already ([gowrap](https://github.com/hexdigest/gowrap) for example).

Resolves: #45

---------

Co-authored-by: Moises Vega <[email protected]>
Co-authored-by: Sung Yoon Whang <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2023
1 parent 2417c65 commit 50aba74
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
10 changes: 10 additions & 0 deletions mockgen/internal/tests/add_generate_directive/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package add_generate_directive makes sure output places the go:generate command as a directive in the generated code.
package add_generate_directive

type Message struct {
Text string
}

type Foo interface {
Bar(channels []string, message chan<- Message)
}
52 changes: 52 additions & 0 deletions mockgen/internal/tests/add_generate_directive/mock.go

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

27 changes: 16 additions & 11 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ var (
)

var (
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
destination = flag.String("destination", "", "Output file; defaults to stdout.")
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.")
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
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.")
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
destination = flag.String("destination", "", "Output file; defaults to stdout.")
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
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, "Put the //go:generate directive into the generated code")
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
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.")

debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
showVersion = flag.Bool("version", false, "Print version.")
Expand Down Expand Up @@ -383,6 +384,10 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
g.out()
g.p(")")

if *writeGenerateDirective {
g.p("//go:generate %v", strings.Join(os.Args, " "))
}

for _, intf := range pkg.Interfaces {
if err := g.GenerateMockInterface(intf, outputPackagePath); err != nil {
return err
Expand Down

0 comments on commit 50aba74

Please sign in to comment.