From c1d3a937fcc0e767d657e3d94767606ff017c555 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 28 Jan 2020 18:07:00 +0300 Subject: [PATCH] all: allow using go run in go:generate Leaving this feature as opt-in for backward compatibility. Relates: #160, #106 --- cmd/wire/main.go | 3 +++ internal/wire/wire.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/wire/main.go b/cmd/wire/main.go index 36c06e98..0502fb6b 100644 --- a/cmd/wire/main.go +++ b/cmd/wire/main.go @@ -100,6 +100,7 @@ func newGenerateOptions(headerFile string) (*wire.GenerateOptions, error) { type genCmd struct { headerFile string prefixFileName string + useGoRun bool } func (*genCmd) Name() string { return "gen" } @@ -117,6 +118,7 @@ func (*genCmd) Usage() string { func (cmd *genCmd) SetFlags(f *flag.FlagSet) { f.StringVar(&cmd.headerFile, "header_file", "", "path to file to insert as a header in wire_gen.go") f.StringVar(&cmd.prefixFileName, "output_file_prefix", "", "string to prepend to output file names.") + f.BoolVar(&cmd.useGoRun, "use_go_run", false, "use go run in //go:generate instead of binary name") } func (cmd *genCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { @@ -132,6 +134,7 @@ func (cmd *genCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interfa } opts.PrefixOutputFile = cmd.prefixFileName + opts.UseGoRun = cmd.useGoRun outs, errs := wire.Generate(ctx, wd, os.Environ(), packages(f), opts) if len(errs) > 0 { diff --git a/internal/wire/wire.go b/internal/wire/wire.go index b55ff29f..74047b1e 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -65,6 +65,7 @@ type GenerateOptions struct { // Header will be inserted at the start of each generated file. Header []byte PrefixOutputFile string + UseGoRun bool } // Generate performs dependency injection for the packages that match the given @@ -96,7 +97,11 @@ func Generate(ctx context.Context, wd string, env []string, patterns []string, o continue } generated[i].OutputPath = filepath.Join(outDir, opts.PrefixOutputFile+"wire_gen.go") - g := newGen(pkg) + command := "wire" + if opts.UseGoRun { + command = "go run github.com/google/wire/cmd/wire" + } + g := newGen(pkg, command) injectorFiles, errs := generateInjectors(g, pkg) if len(errs) > 0 { generated[i].Errs = errs @@ -245,11 +250,13 @@ type gen struct { imports map[string]importInfo anonImports map[string]bool values map[ast.Expr]string + command string } -func newGen(pkg *packages.Package) *gen { +func newGen(pkg *packages.Package, command string) *gen { return &gen{ pkg: pkg, + command: command, anonImports: make(map[string]bool), imports: make(map[string]importInfo), values: make(map[ast.Expr]string), @@ -263,7 +270,7 @@ func (g *gen) frame() []byte { } var buf bytes.Buffer buf.WriteString("// Code generated by Wire. DO NOT EDIT.\n\n") - buf.WriteString("//go:generate wire\n") + buf.WriteString("//go:generate " + g.command + "\n") buf.WriteString("//+build !wireinject\n\n") buf.WriteString("package ") buf.WriteString(g.pkg.Name)