github.com/crossplane/upjet@v1.3.0/pkg/pipeline/version.go (about) 1 // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package pipeline 6 7 import ( 8 "go/types" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/muvaf/typewriter/pkg/wrapper" 14 "github.com/pkg/errors" 15 16 "github.com/crossplane/upjet/pkg/pipeline/templates" 17 ) 18 19 // NewVersionGenerator returns a new VersionGenerator. 20 func NewVersionGenerator(rootDir, modulePath, group, version string) *VersionGenerator { 21 pkgPath := filepath.Join(modulePath, "apis", strings.ToLower(strings.Split(group, ".")[0]), version) 22 return &VersionGenerator{ 23 Group: group, 24 Version: version, 25 DirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version), 26 LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), 27 pkg: types.NewPackage(pkgPath, version), 28 } 29 } 30 31 // VersionGenerator generates files for a version of a specific group. 32 type VersionGenerator struct { 33 Group string 34 Version string 35 DirectoryPath string 36 LicenseHeaderPath string 37 38 pkg *types.Package 39 } 40 41 // Generate writes doc and group version info files to the disk. 42 func (vg *VersionGenerator) Generate() error { 43 vars := map[string]any{ 44 "CRD": map[string]string{ 45 "Version": vg.Version, 46 "Group": vg.Group, 47 }, 48 } 49 gviFile := wrapper.NewFile(vg.pkg.Path(), vg.Version, templates.GroupVersionInfoTemplate, 50 wrapper.WithGenStatement(GenStatement), 51 wrapper.WithHeaderPath(vg.LicenseHeaderPath), 52 ) 53 return errors.Wrap( 54 gviFile.Write(filepath.Join(vg.DirectoryPath, "zz_groupversion_info.go"), vars, os.ModePerm), 55 "cannot write group version info file", 56 ) 57 } 58 59 // Package returns the package of the version that will be generated. 60 func (vg *VersionGenerator) Package() *types.Package { 61 return vg.pkg 62 }