| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
// tracer is a trivial gengo/v2 program which prints the various hooks as they |
| 18 |
// are called. |
| 19 |
package main |
| 20 |
|
| 21 |
import ( |
| 22 |
goflag "flag" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"os" |
| 26 |
"strings" |
| 27 |
|
| 28 |
"github.com/spf13/pflag" |
| 29 |
"k8s.io/gengo/v2" |
| 30 |
"k8s.io/gengo/v2/generator" |
| 31 |
"k8s.io/gengo/v2/namer" |
| 32 |
"k8s.io/gengo/v2/types" |
| 33 |
"k8s.io/klog/v2" |
| 34 |
) |
| 35 |
|
| 36 |
func main() { |
| 37 |
// Collect and parse flags. |
| 38 |
klog.InitFlags(nil) |
| 39 |
goflag.Set("logtostderr", "true") |
| 40 |
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) |
| 41 |
pflag.Parse() |
| 42 |
|
| 43 |
// Gengo apps start with arguments. |
| 44 |
if err := gengo.Execute(getNameSystems(), getDefaultNameSystem(), getTargets, "", pflag.Args()); err != nil { |
| 45 |
klog.ErrorS(err, "fatal error") |
| 46 |
os.Exit(1) |
| 47 |
} |
| 48 |
klog.V(2).InfoS("completed successfully") |
| 49 |
} |
| 50 |
|
| 51 |
func trace(format string, args ...any) { |
| 52 |
if !strings.HasSuffix(format, "\n") { |
| 53 |
format += "\n" |
| 54 |
} |
| 55 |
fmt.Printf("DBG: "+format, args...) |
| 56 |
} |
| 57 |
|
| 58 |
// getNameSystems returns the name system used by the generators in this package. |
| 59 |
func getNameSystems() namer.NameSystems { |
| 60 |
return namer.NameSystems{ |
| 61 |
"raw": namer.NewRawNamer("", nil), |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// getDefaultNameSystem returns the default name system for ordering the types to be |
| 66 |
// processed by the generators in this package. |
| 67 |
func getDefaultNameSystem() string { |
| 68 |
return "raw" |
| 69 |
} |
| 70 |
|
| 71 |
// getTargets is called after the inputs have been loaded. It is expected to |
| 72 |
// examine the provided context and return a list of Packages which will be |
| 73 |
// executed further. |
| 74 |
func getTargets(c *generator.Context) []generator.Target { |
| 75 |
trace("getTargets") |
| 76 |
|
| 77 |
// Make sure we don't actually write a file. |
| 78 |
c.FileTypes = map[string]generator.FileType{ |
| 79 |
"null": nullFile{}, |
| 80 |
} |
| 81 |
|
| 82 |
targets := []generator.Target{} |
| 83 |
for _, input := range c.Inputs { |
| 84 |
klog.V(2).InfoS("processing", "pkg", input) |
| 85 |
|
| 86 |
pkg := c.Universe[input] |
| 87 |
|
| 88 |
targets = append(targets, &generator.SimpleTarget{ |
| 89 |
PkgName: pkg.Name, |
| 90 |
PkgPath: pkg.Path, |
| 91 |
PkgDir: pkg.Dir, |
| 92 |
|
| 93 |
// FilterFunc returns true if this Package cares about this type. |
| 94 |
// Each Generator has its own Filter method which will be checked |
| 95 |
// subsequently. This will be called for every type in every |
| 96 |
// loaded package, not just things in our inputs. |
| 97 |
FilterFunc: func(c *generator.Context, t *types.Type) bool { |
| 98 |
trace("FilterFunc{%s}: %s", pkg.Path, t.String()) |
| 99 |
// Only handle types that are under our input dirs. |
| 100 |
for _, input := range c.Inputs { |
| 101 |
if input == t.Name.Package { |
| 102 |
return true |
| 103 |
} |
| 104 |
} |
| 105 |
return false |
| 106 |
}, |
| 107 |
|
| 108 |
// GeneratorsFunc returns a list of Generators, each of which is |
| 109 |
// responsible for a single output file (though multiple generators |
| 110 |
// may write to the same one). |
| 111 |
GeneratorsFunc: func(c *generator.Context) (generators []generator.Generator) { |
| 112 |
trace("GeneratorsFunc{%s}", pkg.Path) |
| 113 |
return []generator.Generator{ |
| 114 |
&tracerGenerator{myPackage: pkg}, |
| 115 |
} |
| 116 |
}, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
return targets |
| 121 |
} |
| 122 |
|
| 123 |
// Our custom Generator type. |
| 124 |
type tracerGenerator struct { |
| 125 |
myPackage *types.Package |
| 126 |
} |
| 127 |
|
| 128 |
var _ generator.Generator = &tracerGenerator{} |
| 129 |
|
| 130 |
func (g *tracerGenerator) Name() string { return "gengo tracer" } |
| 131 |
func (g *tracerGenerator) Filename() string { return "never_written" } |
| 132 |
func (g *tracerGenerator) FileType() string { return "null" } |
| 133 |
|
| 134 |
// Filter returns true if this Generator cares about this type. |
| 135 |
// This will be called for every type which made it through this Package's |
| 136 |
// Filter method. |
| 137 |
func (g *tracerGenerator) Filter(_ *generator.Context, t *types.Type) bool { |
| 138 |
trace("tracerGenerator{%s}.Filter: %s", g.myPackage.Path, t.String()) |
| 139 |
// Only keep types in this package. |
| 140 |
return t.Name.Package == g.myPackage.Path |
| 141 |
} |
| 142 |
|
| 143 |
// Namers returns a set of NameSystems which will be merged with the namers |
| 144 |
// provided when executing this package. In case of a name collision, the |
| 145 |
// values produced here will win. |
| 146 |
func (g *tracerGenerator) Namers(*generator.Context) namer.NameSystems { |
| 147 |
trace("tracerGenerator{%s}.Namers", g.myPackage.Path) |
| 148 |
return nil |
| 149 |
} |
| 150 |
|
| 151 |
// PackageVars should return an array of variable lines, suitable to be written |
| 152 |
// inside a `var ( ... )` block. |
| 153 |
func (g *tracerGenerator) PackageVars(*generator.Context) []string { |
| 154 |
trace("tracerGenerator{%s}.PackageVars", g.myPackage.Path) |
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
// PackageVars should return an array of const lines, suitable to be written |
| 159 |
// inside a `const ( ... )` block. |
| 160 |
func (g *tracerGenerator) PackageConsts(*generator.Context) []string { |
| 161 |
trace("tracerGenerator{%s}.PackageConsts", g.myPackage.Path) |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
// Init should emit any per-generator code (init functions, etc.) Per-type |
| 166 |
// code can be emitted in GenerateType. |
| 167 |
func (g *tracerGenerator) Init(*generator.Context, io.Writer) error { |
| 168 |
trace("tracerGenerator{%s}.Init", g.myPackage.Path) |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
// GenerateType should emit code for the specified type. This will be called |
| 173 |
// for every type which made it through this Generator's Filter method. |
| 174 |
func (g *tracerGenerator) GenerateType(_ *generator.Context, t *types.Type, _ io.Writer) error { |
| 175 |
trace("tracerGenerator{%s}.GenerateType: %s", g.myPackage.Path, t.String()) |
| 176 |
return nil |
| 177 |
} |
| 178 |
|
| 179 |
// Imports should return an array of import lines, suitable to be written |
| 180 |
// inside an `import ( ... )` block. |
| 181 |
func (g *tracerGenerator) Imports(*generator.Context) []string { |
| 182 |
trace("tracerGenerator{%s}.Imports", g.myPackage.Path) |
| 183 |
return nil |
| 184 |
} |
| 185 |
|
| 186 |
// Finalize should emit any final per-generator code. |
| 187 |
func (g *tracerGenerator) Finalize(*generator.Context, io.Writer) error { |
| 188 |
trace("tracerGenerator{%s}.Finalize", g.myPackage.Path) |
| 189 |
return nil |
| 190 |
} |
| 191 |
|
| 192 |
// nullFile represents a file that does not exist and should not be written to. |
| 193 |
type nullFile struct{} |
| 194 |
|
| 195 |
var _ generator.FileType = nullFile{} |
| 196 |
|
| 197 |
func (nullFile) AssembleFile(*generator.File, string) error { return nil } |