Permit specifying build tags for analysis (#252)

Fixes #251
This commit is contained in:
Matt Wlazlo
2020-07-09 23:41:00 +08:00
committed by GitHub
parent 8d4ff5b1ae
commit 523d8fbe88
3 changed files with 38 additions and 16 deletions

View File

@@ -248,8 +248,8 @@ type Field struct {
// env is nil or empty, it is interpreted as an empty set of variables.
// In case of duplicate environment variables, the last one in the list
// takes precedence.
func Load(ctx context.Context, wd string, env []string, patterns []string) (*Info, []error) {
pkgs, errs := load(ctx, wd, env, patterns)
func Load(ctx context.Context, wd string, env []string, tags string, patterns []string) (*Info, []error) {
pkgs, errs := load(ctx, wd, env, tags, patterns)
if len(errs) > 0 {
return nil, errs
}
@@ -349,7 +349,7 @@ func Load(ctx context.Context, wd string, env []string, patterns []string) (*Inf
// env is nil or empty, it is interpreted as an empty set of variables.
// In case of duplicate environment variables, the last one in the list
// takes precedence.
func load(ctx context.Context, wd string, env []string, patterns []string) ([]*packages.Package, []error) {
func load(ctx context.Context, wd string, env []string, tags string, patterns []string) ([]*packages.Package, []error) {
cfg := &packages.Config{
Context: ctx,
Mode: packages.LoadAllSyntax,
@@ -358,6 +358,9 @@ func load(ctx context.Context, wd string, env []string, patterns []string) ([]*p
BuildFlags: []string{"-tags=wireinject"},
// TODO(light): Use ParseFile to skip function bodies and comments in indirect packages.
}
if len(tags) > 0 {
cfg.BuildFlags[0] += " " + tags
}
escaped := make([]string, len(patterns))
for i := range patterns {
escaped[i] = "pattern=" + patterns[i]

View File

@@ -65,6 +65,7 @@ type GenerateOptions struct {
// Header will be inserted at the start of each generated file.
Header []byte
PrefixOutputFile string
Tags string
}
// Generate performs dependency injection for the packages that match the given
@@ -83,7 +84,7 @@ func Generate(ctx context.Context, wd string, env []string, patterns []string, o
if opts == nil {
opts = &GenerateOptions{}
}
pkgs, errs := load(ctx, wd, env, patterns)
pkgs, errs := load(ctx, wd, env, opts.Tags, patterns)
if len(errs) > 0 {
return nil, errs
}
@@ -103,7 +104,7 @@ func Generate(ctx context.Context, wd string, env []string, patterns []string, o
continue
}
copyNonInjectorDecls(g, injectorFiles, pkg.TypesInfo)
goSrc := g.frame()
goSrc := g.frame(opts.Tags)
if len(opts.Header) > 0 {
goSrc = append(opts.Header, goSrc...)
}
@@ -257,13 +258,16 @@ func newGen(pkg *packages.Package) *gen {
}
// frame bakes the built up source body into an unformatted Go source file.
func (g *gen) frame() []byte {
func (g *gen) frame(tags string) []byte {
if g.buf.Len() == 0 {
return nil
}
var buf bytes.Buffer
if len(tags) > 0 {
tags = fmt.Sprintf(" gen -tags \"%s\"", tags)
}
buf.WriteString("// Code generated by Wire. DO NOT EDIT.\n\n")
buf.WriteString("//go:generate wire\n")
buf.WriteString("//go:generate wire" + tags + "\n")
buf.WriteString("//+build !wireinject\n\n")
buf.WriteString("package ")
buf.WriteString(g.pkg.Name)