execute.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package gengo is a code-generation framework.
  14. package gengo
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "k8s.io/gengo/v2/generator"
  24. "k8s.io/gengo/v2/namer"
  25. "k8s.io/gengo/v2/parser"
  26. )
  27. // StdBuildTag is a suggested build-tag which tools can use both as an argument
  28. // to GoBoilerplate and to Execute.
  29. const StdBuildTag = "ignore_autogenerated"
  30. // StdGeneratedBy is a suggested "generated by" line which tools can use as an
  31. // argument to GoBoilerplate.
  32. const StdGeneratedBy = "// Code generated by GENERATOR_NAME. DO NOT EDIT."
  33. // GoBoilerplate returns the Go file header:
  34. // - an optional build tag (negative, set it to ignore generated code)
  35. // - an optional boilerplate file
  36. // - an optional "generated by" comment
  37. func GoBoilerplate(headerFile, buildTag, generatedBy string) ([]byte, error) {
  38. buf := bytes.Buffer{}
  39. if buildTag != "" {
  40. buf.WriteString(
  41. fmt.Sprintf("//go:build !%s\n// +build !%s\n\n", buildTag, buildTag))
  42. }
  43. if headerFile != "" {
  44. b, err := os.ReadFile(headerFile)
  45. if err != nil {
  46. return nil, err
  47. }
  48. b = bytes.ReplaceAll(b, []byte("YEAR"), []byte(strconv.Itoa(time.Now().UTC().Year())))
  49. buf.Write(b)
  50. buf.WriteByte('\n')
  51. }
  52. if generatedBy != "" {
  53. generatorName := filepath.Base(os.Args[0])
  54. // Strip the extension from the name to normalize output between *nix and Windows.
  55. generatorName = generatorName[:len(generatorName)-len(filepath.Ext(generatorName))]
  56. generatedByComment := strings.ReplaceAll(generatedBy, "GENERATOR_NAME", generatorName)
  57. buf.WriteString(fmt.Sprintf("%s\n\n", generatedByComment))
  58. }
  59. return buf.Bytes(), nil
  60. }
  61. // Execute implements most of a tool's main loop.
  62. func Execute(nameSystems namer.NameSystems, defaultSystem string, getTargets func(*generator.Context) []generator.Target, buildTag string, patterns []string) error {
  63. var buildTags []string
  64. if buildTag != "" {
  65. buildTags = append(buildTags, buildTag)
  66. }
  67. p := parser.NewWithOptions(parser.Options{BuildTags: buildTags})
  68. if err := p.LoadPackages(patterns...); err != nil {
  69. return fmt.Errorf("failed making a parser: %v", err)
  70. }
  71. c, err := generator.NewContext(p, nameSystems, defaultSystem)
  72. if err != nil {
  73. return fmt.Errorf("failed making a context: %v", err)
  74. }
  75. targets := getTargets(c)
  76. if err := c.ExecuteTargets(targets); err != nil {
  77. return fmt.Errorf("failed executing generator: %v", err)
  78. }
  79. return nil
  80. }