Skip to content

Commit

Permalink
all: allow using go run in go:generate
Browse files Browse the repository at this point in the history
Leaving this feature as opt-in for backward compatibility.

Relates: google#160, google#106
  • Loading branch information
ernado committed Jan 28, 2020
1 parent fda1135 commit c1d3a93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/wire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down

0 comments on commit c1d3a93

Please # to comment.