@@ -23,7 +23,6 @@ import (
2323 "go/format"
2424 "go/types"
2525 "io"
26- "os"
2726 "path/filepath"
2827 "sort"
2928 "strings"
@@ -106,6 +105,15 @@ func groupAndPackageVersion(pkg string) string {
106105 return parts [len (parts )- 2 ] + "/" + parts [len (parts )- 1 ]
107106}
108107
108+ func updatePackagePathForOutput (universe Universe , pkg * loader.Package ) {
109+ if filepath .Dir (pkg .CompiledGoFiles [0 ]) == universe .baseFilePath {
110+ pkg .CompiledGoFiles [0 ] = universe .baseFilePath + "/" + universe .importPathSuffix + "/"
111+ } else {
112+ desiredPath := universe .baseFilePath + "/" + universe .importPathSuffix + "/" + groupAndPackageVersion (pkg .PkgPath ) + "/"
113+ pkg .CompiledGoFiles [0 ] = desiredPath
114+ }
115+ }
116+
109117func (d Generator ) Generate (ctx * genall.GenerationContext ) error {
110118 var headerText string
111119
@@ -159,27 +167,20 @@ func (d Generator) Generate(ctx *genall.GenerationContext) error {
159167 }
160168
161169 universe := Universe {
162- eligibleTypes : eligibleTypes ,
170+ eligibleTypes : eligibleTypes ,
171+ baseImportPath : "sigs.k8s.io/controller-tools/pkg/applyconfigurations/testdata" ,
172+ importPathSuffix : "ac" ,
173+ baseFilePath : filepath .Dir (crdRoot .CompiledGoFiles [0 ]),
163174 }
164175
165- basePath := filepath .Dir (crdRoot .CompiledGoFiles [0 ])
166-
167176 for _ , pkg := range visited {
168177 outContents := objGenCtx .generateForPackage (universe , pkg )
169178 if outContents == nil {
170179 continue
171180 }
172181
173- if pkg != ctx .Roots [0 ] {
174- desiredPath := basePath + "/ac/" + groupAndPackageVersion (pkg .PkgPath ) + "/"
175- if _ , err := os .Stat (desiredPath ); os .IsNotExist (err ) {
176- os .MkdirAll (desiredPath , os .ModePerm )
177- }
178- pkg .CompiledGoFiles [0 ] = desiredPath
179- } else {
180- pkg .CompiledGoFiles [0 ] = basePath + "/ac/"
181- os .MkdirAll (pkg .CompiledGoFiles [0 ], os .ModePerm )
182- }
182+ // TODO|jefftree: Changing the import path affects deepcopy as well. Is this the correct approach?
183+ updatePackagePathForOutput (universe , pkg )
183184 writeOut (ctx , pkg , outContents )
184185
185186 }
@@ -235,7 +236,10 @@ func (ctx *ObjectGenCtx) generateEligibleTypes(root *loader.Package) []types.Typ
235236}
236237
237238type Universe struct {
238- eligibleTypes []types.Type
239+ eligibleTypes []types.Type
240+ baseImportPath string
241+ importPathSuffix string
242+ baseFilePath string
239243}
240244
241245// generateForPackage generates apply configuration implementations for
@@ -286,7 +290,6 @@ func (ctx *ObjectGenCtx) generateForPackage(universe Universe, root *loader.Pack
286290 copyCtx .GenerateTypesFor (& universe , root , info )
287291 copyCtx .GenerateStructConstructor (root , info )
288292 }
289- // copyCtx.GenerateListMapAlias(root, info)
290293
291294 outBytes := outContent .Bytes ()
292295 if len (outBytes ) > 0 {
@@ -336,7 +339,7 @@ func writeTypes(pkg *loader.Package, out io.Writer, byType map[string][]byte) {
336339// writeFormatted outputs the given code, after gofmt-ing it. If we couldn't gofmt,
337340// we write the unformatted code for debugging purposes.
338341func writeOut (ctx * genall.GenerationContext , root * loader.Package , outBytes []byte ) {
339- outputFile , err := ctx .Open (root , root . Name + "_zz_generated .applyconfigurations.go" )
342+ outputFile , err := ctx .Open (root , "zz_generated .applyconfigurations.go" )
340343 if err != nil {
341344 root .AddError (err )
342345 return
0 commit comments