Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/genall/genall.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,14 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) {
// ForRoots produces a Runtime to run the given generators against the
// given packages. It outputs to /dev/null by default.
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
roots, err := loader.LoadRoots(rootPaths...)
return g.ForRootsWithConfig(&packages.Config{}, rootPaths...)
}

// ForRootsWithConfig produces a Runtime to run the given generators against the
// given packages, but with the specified configuration. It outputs to /dev/null
// by default.
func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) {
roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...)
if err != nil {
return nil, err
}
Expand Down
35 changes: 33 additions & 2 deletions pkg/genall/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"fmt"
"strings"

"golang.org/x/tools/go/packages"
"sigs.k8s.io/controller-tools/pkg/markers"
)

var (
InputPathsMarker = markers.Must(markers.MakeDefinition("paths", markers.DescribesPackage, InputPaths(nil)))
BuildTagsMarker = markers.Must(markers.MakeDefinition("buildTag", markers.DescribesPackage, InputBuildTags(nil)))
)

// +controllertools:marker:generateHelp:category=""
Expand All @@ -34,6 +36,13 @@ var (
// Multiple paths can be specified using "{path1, path2, path3}".
type InputPaths []string

// +controllertools:marker:generateHelp:category=""

// InputBuildTags represents go build tags used when including or excluding code during builds.
//
// Multiple tags can be specified by using this marker multiple times: buildTag=tag1 buildTag=tag2
type InputBuildTags []string

// RegisterOptionsMarkers registers "mandatory" options markers for FromOptions into the given registry.
// At this point, that's just InputPaths.
func RegisterOptionsMarkers(into *markers.Registry) error {
Expand All @@ -44,6 +53,14 @@ func RegisterOptionsMarkers(into *markers.Registry) error {
if helpGiver, hasHelp := ((interface{})(InputPaths(nil))).(HasHelp); hasHelp {
into.AddHelp(InputPathsMarker, helpGiver.Help())
}

if err := into.Register(BuildTagsMarker); err != nil {
return err
}
if helpGiver, hasHelp := ((interface{})(InputBuildTags(nil))).(HasHelp); hasHelp {
into.AddHelp(BuildTagsMarker, helpGiver.Help())
}

return nil
}

Expand Down Expand Up @@ -78,9 +95,8 @@ func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime,
if err != nil {
return nil, err
}

// make the runtime
genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...)
genRuntime, err := protoRt.Generators.ForRootsWithConfig(protoRt.getConfig(), protoRt.Paths...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -112,6 +128,7 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
ByGenerator: make(map[*Generator]OutputRule),
}
var paths []string
var tags []string

// collect the generators first, so that we can key the output on the actual
// generator, which matters if there's settings in the gen object and it's not a pointer.
Expand Down Expand Up @@ -151,6 +168,8 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
continue
case InputPaths:
paths = append(paths, val...)
case InputBuildTags:
tags = append(tags, val...)
default:
return protoRuntime{}, fmt.Errorf("unknown option marker %q", defn.Name)
}
Expand All @@ -171,6 +190,7 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
Generators: gens,
OutputRules: rules,
GeneratorsByName: gensByName,
Tags: tags,
}, nil
}

Expand All @@ -181,6 +201,7 @@ type protoRuntime struct {
Generators Generators
OutputRules OutputRules
GeneratorsByName map[string]*Generator
Tags []string
}

// splitOutputRuleOption splits a marker name of "output:rule:gen" or "output:rule"
Expand All @@ -194,3 +215,13 @@ func splitOutputRuleOption(name string) (ruleName string, genName string) {
// output:<rule>
return parts[1], ""
}

// getConfig generates the initial config based on the the optional arguments,
// which are currently only tags
func (p protoRuntime) getConfig() *packages.Config {
cfg := &packages.Config{}
for _, tag := range p.Tags {
cfg.BuildFlags = append(cfg.BuildFlags, "-tags", tag)
}
return cfg
}
11 changes: 11 additions & 0 deletions pkg/genall/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.