@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user