| 1 |
/* |
| 2 |
Copyright 2015 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 |
package generator |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"io/ioutil" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"strings" |
| 27 |
|
| 28 |
"golang.org/x/tools/imports" |
| 29 |
"k8s.io/gengo/namer" |
| 30 |
"k8s.io/gengo/types" |
| 31 |
|
| 32 |
"k8s.io/klog/v2" |
| 33 |
) |
| 34 |
|
| 35 |
func errs2strings(errors []error) []string { |
| 36 |
strs := make([]string, len(errors)) |
| 37 |
for i := range errors { |
| 38 |
strs[i] = errors[i].Error() |
| 39 |
} |
| 40 |
return strs |
| 41 |
} |
| 42 |
|
| 43 |
// ExecutePackages runs the generators for every package in 'packages'. 'outDir' |
| 44 |
// is the base directory in which to place all the generated packages; it |
| 45 |
// should be a physical path on disk, not an import path. e.g.: |
| 46 |
// /path/to/home/path/to/gopath/src/ |
| 47 |
// Each package has its import path already, this will be appended to 'outDir'. |
| 48 |
func (c *Context) ExecutePackages(outDir string, packages Packages) error { |
| 49 |
var errors []error |
| 50 |
for _, p := range packages { |
| 51 |
if err := c.ExecutePackage(outDir, p); err != nil { |
| 52 |
errors = append(errors, err) |
| 53 |
} |
| 54 |
} |
| 55 |
if len(errors) > 0 { |
| 56 |
return fmt.Errorf("some packages had errors:\n%v\n", strings.Join(errs2strings(errors), "\n")) |
| 57 |
} |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
type DefaultFileType struct { |
| 62 |
Format func([]byte) ([]byte, error) |
| 63 |
Assemble func(io.Writer, *File) |
| 64 |
} |
| 65 |
|
| 66 |
func (ft DefaultFileType) AssembleFile(f *File, pathname string) error { |
| 67 |
klog.V(5).Infof("Assembling file %q", pathname) |
| 68 |
destFile, err := os.Create(pathname) |
| 69 |
if err != nil { |
| 70 |
return err |
| 71 |
} |
| 72 |
defer destFile.Close() |
| 73 |
|
| 74 |
b := &bytes.Buffer{} |
| 75 |
et := NewErrorTracker(b) |
| 76 |
ft.Assemble(et, f) |
| 77 |
if et.Error() != nil { |
| 78 |
return et.Error() |
| 79 |
} |
| 80 |
if formatted, err := ft.Format(b.Bytes()); err != nil { |
| 81 |
err = fmt.Errorf("unable to format file %q (%v).", pathname, err) |
| 82 |
// Write the file anyway, so they can see what's going wrong and fix the generator. |
| 83 |
if _, err2 := destFile.Write(b.Bytes()); err2 != nil { |
| 84 |
return err2 |
| 85 |
} |
| 86 |
return err |
| 87 |
} else { |
| 88 |
_, err = destFile.Write(formatted) |
| 89 |
return err |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (ft DefaultFileType) VerifyFile(f *File, pathname string) error { |
| 94 |
klog.V(5).Infof("Verifying file %q", pathname) |
| 95 |
friendlyName := filepath.Join(f.PackageName, f.Name) |
| 96 |
b := &bytes.Buffer{} |
| 97 |
et := NewErrorTracker(b) |
| 98 |
ft.Assemble(et, f) |
| 99 |
if et.Error() != nil { |
| 100 |
return et.Error() |
| 101 |
} |
| 102 |
formatted, err := ft.Format(b.Bytes()) |
| 103 |
if err != nil { |
| 104 |
return fmt.Errorf("unable to format the output for %q: %v", friendlyName, err) |
| 105 |
} |
| 106 |
existing, err := ioutil.ReadFile(pathname) |
| 107 |
if err != nil { |
| 108 |
return fmt.Errorf("unable to read file %q for comparison: %v", friendlyName, err) |
| 109 |
} |
| 110 |
if bytes.Compare(formatted, existing) == 0 { |
| 111 |
return nil |
| 112 |
} |
| 113 |
// Be nice and find the first place where they differ |
| 114 |
i := 0 |
| 115 |
for i < len(formatted) && i < len(existing) && formatted[i] == existing[i] { |
| 116 |
i++ |
| 117 |
} |
| 118 |
eDiff, fDiff := existing[i:], formatted[i:] |
| 119 |
if len(eDiff) > 100 { |
| 120 |
eDiff = eDiff[:100] |
| 121 |
} |
| 122 |
if len(fDiff) > 100 { |
| 123 |
fDiff = fDiff[:100] |
| 124 |
} |
| 125 |
return fmt.Errorf("output for %q differs; first existing/expected diff: \n %q\n %q", friendlyName, string(eDiff), string(fDiff)) |
| 126 |
} |
| 127 |
|
| 128 |
func assembleGolangFile(w io.Writer, f *File) { |
| 129 |
w.Write(f.Header) |
| 130 |
fmt.Fprintf(w, "package %v\n\n", f.PackageName) |
| 131 |
|
| 132 |
if len(f.Imports) > 0 { |
| 133 |
fmt.Fprint(w, "import (\n") |
| 134 |
for i := range f.Imports { |
| 135 |
if strings.Contains(i, "\"") { |
| 136 |
// they included quotes, or are using the |
| 137 |
// `name "path/to/pkg"` format. |
| 138 |
fmt.Fprintf(w, "\t%s\n", i) |
| 139 |
} else { |
| 140 |
fmt.Fprintf(w, "\t%q\n", i) |
| 141 |
} |
| 142 |
} |
| 143 |
fmt.Fprint(w, ")\n\n") |
| 144 |
} |
| 145 |
|
| 146 |
if f.Vars.Len() > 0 { |
| 147 |
fmt.Fprint(w, "var (\n") |
| 148 |
w.Write(f.Vars.Bytes()) |
| 149 |
fmt.Fprint(w, ")\n\n") |
| 150 |
} |
| 151 |
|
| 152 |
if f.Consts.Len() > 0 { |
| 153 |
fmt.Fprint(w, "const (\n") |
| 154 |
w.Write(f.Consts.Bytes()) |
| 155 |
fmt.Fprint(w, ")\n\n") |
| 156 |
} |
| 157 |
|
| 158 |
w.Write(f.Body.Bytes()) |
| 159 |
} |
| 160 |
|
| 161 |
func importsWrapper(src []byte) ([]byte, error) { |
| 162 |
return imports.Process("", src, nil) |
| 163 |
} |
| 164 |
|
| 165 |
func NewGolangFile() *DefaultFileType { |
| 166 |
return &DefaultFileType{ |
| 167 |
Format: importsWrapper, |
| 168 |
Assemble: assembleGolangFile, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// format should be one line only, and not end with \n. |
| 173 |
func addIndentHeaderComment(b *bytes.Buffer, format string, args ...interface{}) { |
| 174 |
if b.Len() > 0 { |
| 175 |
fmt.Fprintf(b, "\n// "+format+"\n", args...) |
| 176 |
} else { |
| 177 |
fmt.Fprintf(b, "// "+format+"\n", args...) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func (c *Context) filteredBy(f func(*Context, *types.Type) bool) *Context { |
| 182 |
c2 := *c |
| 183 |
c2.Order = []*types.Type{} |
| 184 |
for _, t := range c.Order { |
| 185 |
if f(c, t) { |
| 186 |
c2.Order = append(c2.Order, t) |
| 187 |
} |
| 188 |
} |
| 189 |
return &c2 |
| 190 |
} |
| 191 |
|
| 192 |
// make a new context; inheret c.Namers, but add on 'namers'. In case of a name |
| 193 |
// collision, the namer in 'namers' wins. |
| 194 |
func (c *Context) addNameSystems(namers namer.NameSystems) *Context { |
| 195 |
if namers == nil { |
| 196 |
return c |
| 197 |
} |
| 198 |
c2 := *c |
| 199 |
// Copy the existing name systems so we don't corrupt a parent context |
| 200 |
c2.Namers = namer.NameSystems{} |
| 201 |
for k, v := range c.Namers { |
| 202 |
c2.Namers[k] = v |
| 203 |
} |
| 204 |
|
| 205 |
for name, namer := range namers { |
| 206 |
c2.Namers[name] = namer |
| 207 |
} |
| 208 |
return &c2 |
| 209 |
} |
| 210 |
|
| 211 |
// ExecutePackage executes a single package. 'outDir' is the base directory in |
| 212 |
// which to place the package; it should be a physical path on disk, not an |
| 213 |
// import path. e.g.: '/path/to/home/path/to/gopath/src/' The package knows its |
| 214 |
// import path already, this will be appended to 'outDir'. |
| 215 |
func (c *Context) ExecutePackage(outDir string, p Package) error { |
| 216 |
path := filepath.Join(outDir, p.Path()) |
| 217 |
|
| 218 |
// When working outside of GOPATH, we typically won't want to generate the |
| 219 |
// full path for a package. For example, if our current project's root/base |
| 220 |
// package is github.com/foo/bar, outDir=., p.Path()=github.com/foo/bar/generated, |
| 221 |
// then we really want to be writing files to ./generated, not ./github.com/foo/bar/generated. |
| 222 |
// The following will trim a path prefix (github.com/foo/bar) from p.Path() to arrive at |
| 223 |
// a relative path that works with projects not in GOPATH. |
| 224 |
if c.TrimPathPrefix != "" { |
| 225 |
separator := string(filepath.Separator) |
| 226 |
if !strings.HasSuffix(c.TrimPathPrefix, separator) { |
| 227 |
c.TrimPathPrefix += separator |
| 228 |
} |
| 229 |
|
| 230 |
path = strings.TrimPrefix(path, c.TrimPathPrefix) |
| 231 |
} |
| 232 |
klog.V(5).Infof("Processing package %q, disk location %q", p.Name(), path) |
| 233 |
// Filter out any types the *package* doesn't care about. |
| 234 |
packageContext := c.filteredBy(p.Filter) |
| 235 |
os.MkdirAll(path, 0755) |
| 236 |
files := map[string]*File{} |
| 237 |
for _, g := range p.Generators(packageContext) { |
| 238 |
// Filter out types the *generator* doesn't care about. |
| 239 |
genContext := packageContext.filteredBy(g.Filter) |
| 240 |
// Now add any extra name systems defined by this generator |
| 241 |
genContext = genContext.addNameSystems(g.Namers(genContext)) |
| 242 |
|
| 243 |
fileType := g.FileType() |
| 244 |
if len(fileType) == 0 { |
| 245 |
return fmt.Errorf("generator %q must specify a file type", g.Name()) |
| 246 |
} |
| 247 |
f := files[g.Filename()] |
| 248 |
if f == nil { |
| 249 |
// This is the first generator to reference this file, so start it. |
| 250 |
f = &File{ |
| 251 |
Name: g.Filename(), |
| 252 |
FileType: fileType, |
| 253 |
PackageName: p.Name(), |
| 254 |
PackagePath: p.Path(), |
| 255 |
PackageSourcePath: p.SourcePath(), |
| 256 |
Header: p.Header(g.Filename()), |
| 257 |
Imports: map[string]struct{}{}, |
| 258 |
} |
| 259 |
files[f.Name] = f |
| 260 |
} else { |
| 261 |
if f.FileType != g.FileType() { |
| 262 |
return fmt.Errorf("file %q already has type %q, but generator %q wants to use type %q", f.Name, f.FileType, g.Name(), g.FileType()) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if vars := g.PackageVars(genContext); len(vars) > 0 { |
| 267 |
addIndentHeaderComment(&f.Vars, "Package-wide variables from generator %q.", g.Name()) |
| 268 |
for _, v := range vars { |
| 269 |
if _, err := fmt.Fprintf(&f.Vars, "%s\n", v); err != nil { |
| 270 |
return err |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
if consts := g.PackageConsts(genContext); len(consts) > 0 { |
| 275 |
addIndentHeaderComment(&f.Consts, "Package-wide consts from generator %q.", g.Name()) |
| 276 |
for _, v := range consts { |
| 277 |
if _, err := fmt.Fprintf(&f.Consts, "%s\n", v); err != nil { |
| 278 |
return err |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
if err := genContext.executeBody(&f.Body, g); err != nil { |
| 283 |
return err |
| 284 |
} |
| 285 |
if imports := g.Imports(genContext); len(imports) > 0 { |
| 286 |
for _, i := range imports { |
| 287 |
f.Imports[i] = struct{}{} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
var errors []error |
| 293 |
for _, f := range files { |
| 294 |
finalPath := filepath.Join(path, f.Name) |
| 295 |
assembler, ok := c.FileTypes[f.FileType] |
| 296 |
if !ok { |
| 297 |
return fmt.Errorf("the file type %q registered for file %q does not exist in the context", f.FileType, f.Name) |
| 298 |
} |
| 299 |
var err error |
| 300 |
if c.Verify { |
| 301 |
err = assembler.VerifyFile(f, finalPath) |
| 302 |
} else { |
| 303 |
err = assembler.AssembleFile(f, finalPath) |
| 304 |
} |
| 305 |
if err != nil { |
| 306 |
errors = append(errors, err) |
| 307 |
} |
| 308 |
} |
| 309 |
if len(errors) > 0 { |
| 310 |
return fmt.Errorf("errors in package %q:\n%v\n", p.Path(), strings.Join(errs2strings(errors), "\n")) |
| 311 |
} |
| 312 |
return nil |
| 313 |
} |
| 314 |
|
| 315 |
func (c *Context) executeBody(w io.Writer, generator Generator) error { |
| 316 |
et := NewErrorTracker(w) |
| 317 |
if err := generator.Init(c, et); err != nil { |
| 318 |
return err |
| 319 |
} |
| 320 |
for _, t := range c.Order { |
| 321 |
if err := generator.GenerateType(c, t, et); err != nil { |
| 322 |
return err |
| 323 |
} |
| 324 |
} |
| 325 |
if err := generator.Finalize(c, et); err != nil { |
| 326 |
return err |
| 327 |
} |
| 328 |
return et.Error() |
| 329 |
} |