github.com/galaxyobe/gen@v0.0.0-20220910125335-392fa8f0990f/cmd/getter-gen/generators/package.go (about) 1 /* 2 Copyright 2022 Galaxyobe. 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 generators 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "strings" 23 24 "k8s.io/gengo/args" 25 "k8s.io/gengo/examples/set-gen/sets" 26 "k8s.io/gengo/generator" 27 "k8s.io/gengo/namer" 28 "k8s.io/gengo/types" 29 "k8s.io/klog/v2" 30 31 "github.com/galaxyobe/gen/pkg/custom_args" 32 "github.com/galaxyobe/gen/pkg/util" 33 ) 34 35 // NameSystems returns the name system used by the generators in this package. 36 func NameSystems() namer.NameSystems { 37 return namer.NameSystems{ 38 "public": namer.NewPublicNamer(0), 39 "private": namer.NewPrivateNamer(0), 40 "raw": namer.NewRawNamer("", nil), 41 } 42 } 43 44 // DefaultNameSystem returns the default name system for ordering the types to be 45 // processed by the generators in this package. 46 func DefaultNameSystem() string { 47 return "public" 48 } 49 50 func Packages(context *generator.Context, arguments *args.GeneratorArgs) generator.Packages { 51 boilerplate, err := arguments.LoadGoBoilerplate() 52 if err != nil { 53 klog.Fatalf("Failed loading boilerplate: %v", err) 54 } 55 56 inputs := sets.NewString(context.Inputs...) 57 packages := generator.Packages{} 58 header := append([]byte(fmt.Sprintf("//go:build !%s\n// +build !%s\n\n", arguments.GeneratedBuildTag, arguments.GeneratedBuildTag)), boilerplate...) 59 60 customArgs := custom_args.GetCustomArgs(arguments) 61 if customArgs.BoundingDirs == nil { 62 customArgs.BoundingDirs = context.Inputs 63 } 64 65 var boundingDirs []string 66 for i := range customArgs.BoundingDirs { 67 // Strip any trailing slashes - they are not exactly "correct" but 68 // this is friendlier. 69 boundingDirs = append(boundingDirs, strings.TrimRight(customArgs.BoundingDirs[i], "/")) 70 } 71 72 // new parse build to replace context universe for basic int8/uint8 types 73 build, err := customArgs.NewBuilder() 74 if err != nil { 75 klog.Fatalf("Failed making a parser: %v", err) 76 } 77 // replace universe 78 err = build.ReplaceUniverse(context.Universe) 79 if err != nil { 80 klog.Fatalf("Find types error: %v", err) 81 } 82 // reorder 83 util.ReContextOrder(context, DefaultNameSystem()) 84 85 for i := range inputs { 86 klog.V(5).Infof("Considering pkg %q", i) 87 pkg := context.Universe[i] 88 if pkg == nil { 89 // If the input had no Go files, for example. 90 continue 91 } 92 pkgEnabled, genTypes := NewGenTypes(pkg) 93 if !pkgEnabled { 94 continue 95 } 96 klog.V(3).Infof("Package %q needs generation", pkg.Path) 97 path := pkg.Path 98 if strings.HasPrefix(pkg.SourcePath, arguments.OutputBase) { 99 expandedPath := strings.TrimPrefix(pkg.SourcePath, arguments.OutputBase) 100 if strings.Contains(expandedPath, "/vendor/") { 101 path = expandedPath 102 } 103 } 104 if customArgs.TrimPackagePath != "" { 105 path = strings.ReplaceAll(path, customArgs.TrimPackagePath, "") 106 separator := string(filepath.Separator) 107 if path != "" && strings.HasPrefix(path, separator) { 108 path = path[1:] 109 } 110 } 111 112 packages = append(packages, 113 &generator.DefaultPackage{ 114 PackageName: pkg.Name, 115 PackagePath: path, 116 HeaderText: header, 117 GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { 118 return []generator.Generator{ 119 NewGenGetter( 120 build, 121 arguments.OutputFileBaseName, 122 pkg.Path, 123 boundingDirs, 124 genTypes, 125 pkg.SourcePath, 126 ), 127 } 128 }, 129 FilterFunc: func(c *generator.Context, t *types.Type) bool { 130 return t.Name.Package == pkg.Path 131 }, 132 }) 133 } 134 return packages 135 }