| 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 gengo is a code-generation framework. |
| 18 |
package gengo |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"strconv" |
| 26 |
"strings" |
| 27 |
"time" |
| 28 |
|
| 29 |
"k8s.io/gengo/v2/generator" |
| 30 |
"k8s.io/gengo/v2/namer" |
| 31 |
"k8s.io/gengo/v2/parser" |
| 32 |
) |
| 33 |
|
| 34 |
// StdBuildTag is a suggested build-tag which tools can use both as an argument |
| 35 |
// to GoBoilerplate and to Execute. |
| 36 |
const StdBuildTag = "ignore_autogenerated" |
| 37 |
|
| 38 |
// StdGeneratedBy is a suggested "generated by" line which tools can use as an |
| 39 |
// argument to GoBoilerplate. |
| 40 |
const StdGeneratedBy = "// Code generated by GENERATOR_NAME. DO NOT EDIT." |
| 41 |
|
| 42 |
// GoBoilerplate returns the Go file header: |
| 43 |
// - an optional build tag (negative, set it to ignore generated code) |
| 44 |
// - an optional boilerplate file |
| 45 |
// - an optional "generated by" comment |
| 46 |
func GoBoilerplate(headerFile, buildTag, generatedBy string) ([]byte, error) { |
| 47 |
buf := bytes.Buffer{} |
| 48 |
|
| 49 |
if buildTag != "" { |
| 50 |
buf.WriteString( |
| 51 |
fmt.Sprintf("//go:build !%s\n// +build !%s\n\n", buildTag, buildTag)) |
| 52 |
} |
| 53 |
|
| 54 |
if headerFile != "" { |
| 55 |
b, err := os.ReadFile(headerFile) |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
b = bytes.ReplaceAll(b, []byte("YEAR"), []byte(strconv.Itoa(time.Now().UTC().Year()))) |
| 60 |
buf.Write(b) |
| 61 |
buf.WriteByte('\n') |
| 62 |
} |
| 63 |
|
| 64 |
if generatedBy != "" { |
| 65 |
generatorName := filepath.Base(os.Args[0]) |
| 66 |
// Strip the extension from the name to normalize output between *nix and Windows. |
| 67 |
generatorName = generatorName[:len(generatorName)-len(filepath.Ext(generatorName))] |
| 68 |
generatedByComment := strings.ReplaceAll(generatedBy, "GENERATOR_NAME", generatorName) |
| 69 |
buf.WriteString(fmt.Sprintf("%s\n\n", generatedByComment)) |
| 70 |
} |
| 71 |
|
| 72 |
return buf.Bytes(), nil |
| 73 |
} |
| 74 |
|
| 75 |
// Execute implements most of a tool's main loop. |
| 76 |
func Execute(nameSystems namer.NameSystems, defaultSystem string, getTargets func(*generator.Context) []generator.Target, buildTag string, patterns []string) error { |
| 77 |
var buildTags []string |
| 78 |
if buildTag != "" { |
| 79 |
buildTags = append(buildTags, buildTag) |
| 80 |
} |
| 81 |
|
| 82 |
p := parser.NewWithOptions(parser.Options{BuildTags: buildTags}) |
| 83 |
if err := p.LoadPackages(patterns...); err != nil { |
| 84 |
return fmt.Errorf("failed making a parser: %v", err) |
| 85 |
} |
| 86 |
|
| 87 |
c, err := generator.NewContext(p, nameSystems, defaultSystem) |
| 88 |
if err != nil { |
| 89 |
return fmt.Errorf("failed making a context: %v", err) |
| 90 |
} |
| 91 |
|
| 92 |
targets := getTargets(c) |
| 93 |
if err := c.ExecuteTargets(targets); err != nil { |
| 94 |
return fmt.Errorf("failed executing generator: %v", err) |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |