Skip to content

Commit 26e33b3

Browse files
committed
feat: support go tool invocation in write directive
The new -write_tool_generate_directive works the same way as the -write_generate_directive flag, except that it correctly regenerates "go tool mockgen" invocations of the mockgen tool.
1 parent 6568d88 commit 26e33b3

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

mockgen/mockgen.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,23 @@ var (
5454
)
5555

5656
var (
57-
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
58-
destination = flag.String("destination", "", "Output file; defaults to stdout.")
59-
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
60-
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
61-
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.")
62-
writeCmdComment = flag.Bool("write_command_comment", true, "Writes the command used as a comment if true.")
63-
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
64-
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (package mode) comment if true.")
65-
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
66-
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
67-
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
68-
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
69-
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
70-
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
71-
excludeInterfaces = flag.String("exclude_interfaces", "", "(source mode) Comma-separated names of interfaces to be excluded")
72-
modelGob = flag.String("model_gob", "", "Skip package/source loading entirely and use the gob encoded model.Package at the given path")
57+
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
58+
destination = flag.String("destination", "", "Output file; defaults to stdout.")
59+
mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
60+
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
61+
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.")
62+
writeCmdComment = flag.Bool("write_command_comment", true, "Writes the command used as a comment if true.")
63+
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
64+
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (package mode) comment if true.")
65+
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
66+
writeToolGenerateDirective = flag.Bool("write_tool_generate_directive", false, "Add //go:generate directive to regenerate the mock, for indirect \"go tool mockgen\" invocation")
67+
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
68+
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
69+
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
70+
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
71+
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
72+
excludeInterfaces = flag.String("exclude_interfaces", "", "(source mode) Comma-separated names of interfaces to be excluded")
73+
modelGob = flag.String("model_gob", "", "Skip package/source loading entirely and use the gob encoded model.Package at the given path")
7374

7475
debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
7576
showVersion = flag.Bool("version", false, "Print version.")
@@ -437,8 +438,14 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
437438
g.out()
438439
g.p(")")
439440

440-
if *writeGenerateDirective {
441+
switch {
442+
case *writeGenerateDirective && *writeToolGenerateDirective:
443+
return fmt.Errorf("write_generate_directive and write_tool_generate_directive are mutually exclusive")
444+
case *writeGenerateDirective:
441445
g.p("//go:generate %v", strings.Join(os.Args, " "))
446+
case *writeToolGenerateDirective:
447+
g.p("//go:generate %v",
448+
fmt.Sprintf("go tool %s %s", path.Base(os.Args[0]), strings.Join(os.Args[1:], " ")))
442449
}
443450

444451
for _, intf := range pkg.Interfaces {

0 commit comments

Comments
 (0)