From e5bba1a0e2ab975ee59e35fb5bbc4dbd1b269e91 Mon Sep 17 00:00:00 2001 From: Muvaffak Onus Date: Tue, 16 Nov 2021 12:19:55 +0300 Subject: [PATCH] pipeline: calculate the path of the license header with root dir information so that generator can be run from anywhere. Signed-off-by: Muvaffak Onus --- pkg/pipeline/controller.go | 4 +++- pkg/pipeline/crd.go | 8 +++++--- pkg/pipeline/register.go | 4 +++- pkg/pipeline/run.go | 19 +++++++------------ pkg/pipeline/setup.go | 10 ++++++---- pkg/pipeline/terraformed.go | 8 +++++--- pkg/pipeline/version.go | 28 ++++++++++++---------------- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/pkg/pipeline/controller.go b/pkg/pipeline/controller.go index 697d3cce..03231568 100644 --- a/pkg/pipeline/controller.go +++ b/pkg/pipeline/controller.go @@ -34,6 +34,7 @@ func NewControllerGenerator(rootDir, modulePath, group string) *ControllerGenera Group: group, ControllerGroupDir: filepath.Join(rootDir, "internal", "controller", strings.Split(group, ".")[0]), ModulePath: modulePath, + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), } } @@ -42,6 +43,7 @@ type ControllerGenerator struct { Group string ControllerGroupDir string ModulePath string + LicenseHeaderPath string } // Generate writes controller setup functions. @@ -49,7 +51,7 @@ func (cg *ControllerGenerator) Generate(cfg *config.Resource, typesPkgPath strin controllerPkgPath := filepath.Join(cg.ModulePath, "internal", "controller", strings.ToLower(strings.Split(cg.Group, ".")[0]), strings.ToLower(cfg.Kind)) ctrlFile := wrapper.NewFile(controllerPkgPath, strings.ToLower(cfg.Kind), templates.ControllerTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), // todo + wrapper.WithHeaderPath(cg.LicenseHeaderPath), ) vars := map[string]interface{}{ diff --git a/pkg/pipeline/crd.go b/pkg/pipeline/crd.go index 9cec5cd3..7fa9fbdf 100644 --- a/pkg/pipeline/crd.go +++ b/pkg/pipeline/crd.go @@ -36,9 +36,10 @@ import ( const GenStatement = "// Code generated by terrajet. DO NOT EDIT." // NewCRDGenerator returns a new CRDGenerator. -func NewCRDGenerator(pkg *types.Package, localDirectoryPath, group, providerShortName string) *CRDGenerator { +func NewCRDGenerator(pkg *types.Package, rootDir, providerShortName, group, version string) *CRDGenerator { return &CRDGenerator{ - LocalDirectoryPath: localDirectoryPath, + LocalDirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version), + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), Group: group, ProviderShortName: providerShortName, pkg: pkg, @@ -51,6 +52,7 @@ type CRDGenerator struct { LocalDirectoryPath string Group string ProviderShortName string + LicenseHeaderPath string pkg *types.Package } @@ -59,7 +61,7 @@ type CRDGenerator struct { func (cg *CRDGenerator) Generate(cfg *config.Resource) error { file := wrapper.NewFile(cg.pkg.Path(), cg.pkg.Name(), templates.CRDTypesTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), // todo + wrapper.WithHeaderPath(cg.LicenseHeaderPath), ) for _, omit := range cfg.ExternalName.OmittedFields { delete(cfg.TerraformResource.Schema, omit) diff --git a/pkg/pipeline/register.go b/pkg/pipeline/register.go index 6f6c142c..c2410ab2 100644 --- a/pkg/pipeline/register.go +++ b/pkg/pipeline/register.go @@ -31,6 +31,7 @@ import ( func NewRegisterGenerator(rootDir, modulePath string) *RegisterGenerator { return &RegisterGenerator{ LocalDirectoryPath: filepath.Join(rootDir, "apis"), + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), ModulePath: modulePath, } } @@ -39,6 +40,7 @@ func NewRegisterGenerator(rootDir, modulePath string) *RegisterGenerator { type RegisterGenerator struct { LocalDirectoryPath string ModulePath string + LicenseHeaderPath string } // Generate writes the register file with the content produced using given @@ -46,7 +48,7 @@ type RegisterGenerator struct { func (rg *RegisterGenerator) Generate(versionPkgList []string) error { registerFile := wrapper.NewFile(filepath.Join(rg.ModulePath, "apis"), "apis", templates.RegisterTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), + wrapper.WithHeaderPath(rg.LicenseHeaderPath), ) aliases := make([]string, len(versionPkgList)) for i, pkgPath := range versionPkgList { diff --git a/pkg/pipeline/run.go b/pkg/pipeline/run.go index 1140a716..35aca2b3 100644 --- a/pkg/pipeline/run.go +++ b/pkg/pipeline/run.go @@ -18,7 +18,6 @@ package pipeline import ( "fmt" - "os" "os/exec" "path/filepath" "sort" @@ -30,14 +29,10 @@ import ( ) // Run runs the Terrajet code generation pipelines. -func Run(pc *config.Provider) { // nolint:gocyclo +func Run(pc *config.Provider, rootDir string) { // nolint:gocyclo // Note(turkenh): nolint reasoning - this is the main function of the code // generation pipeline. We didn't want to split it into multiple functions // for better readability considering the straightforward logic here. - wd, err := os.Getwd() - if err != nil { - panic(errors.Wrap(err, "cannot get working directory")) - } // Group resources based on their Group and API Versions. resourcesGroups := map[string]map[string]map[string]*config.Resource{} @@ -64,10 +59,10 @@ func Run(pc *config.Provider) { // nolint:gocyclo count := 0 for group, versions := range resourcesGroups { for version, resources := range versions { - versionGen := NewVersionGenerator(wd, pc.ModulePath, strings.ToLower(group)+pc.GroupSuffix, version) - crdGen := NewCRDGenerator(versionGen.Package(), versionGen.DirectoryPath(), strings.ToLower(group)+pc.GroupSuffix, pc.ShortName) - tfGen := NewTerraformedGenerator(versionGen.Package(), versionGen.DirectoryPath()) - ctrlGen := NewControllerGenerator(wd, pc.ModulePath, strings.ToLower(group)+pc.GroupSuffix) + versionGen := NewVersionGenerator(rootDir, pc.ModulePath, strings.ToLower(group)+pc.GroupSuffix, version) + crdGen := NewCRDGenerator(versionGen.Package(), rootDir, pc.ShortName, strings.ToLower(group)+pc.GroupSuffix, version) + tfGen := NewTerraformedGenerator(versionGen.Package(), rootDir, strings.ToLower(group)+pc.GroupSuffix, version) + ctrlGen := NewControllerGenerator(rootDir, pc.ModulePath, strings.ToLower(group)+pc.GroupSuffix) keys := make([]string, len(resources)) i := 0 @@ -99,10 +94,10 @@ func Run(pc *config.Provider) { // nolint:gocyclo } } - if err := NewRegisterGenerator(wd, pc.ModulePath).Generate(apiVersionPkgList); err != nil { + if err := NewRegisterGenerator(rootDir, pc.ModulePath).Generate(apiVersionPkgList); err != nil { panic(errors.Wrap(err, "cannot generate register file")) } - if err := NewSetupGenerator(wd, pc.ModulePath).Generate(controllerPkgList); err != nil { + if err := NewSetupGenerator(rootDir, pc.ModulePath).Generate(controllerPkgList); err != nil { panic(errors.Wrap(err, "cannot generate setup file")) } if out, err := exec.Command("bash", "-c", "goimports -w $(find apis -iname 'zz_*')").CombinedOutput(); err != nil { diff --git a/pkg/pipeline/setup.go b/pkg/pipeline/setup.go index e0db39e1..58df94c2 100644 --- a/pkg/pipeline/setup.go +++ b/pkg/pipeline/setup.go @@ -31,6 +31,7 @@ import ( func NewSetupGenerator(rootDir, modulePath string) *SetupGenerator { return &SetupGenerator{ LocalDirectoryPath: filepath.Join(rootDir, "internal", "controller"), + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), ModulePath: modulePath, } } @@ -38,15 +39,16 @@ func NewSetupGenerator(rootDir, modulePath string) *SetupGenerator { // SetupGenerator generates controller setup file. type SetupGenerator struct { LocalDirectoryPath string + LicenseHeaderPath string ModulePath string } // Generate writes the setup file with the content produced using given // list of version packages. -func (rg *SetupGenerator) Generate(versionPkgList []string) error { - setupFile := wrapper.NewFile(filepath.Join(rg.ModulePath, "apis"), "apis", templates.SetupTemplate, +func (sg *SetupGenerator) Generate(versionPkgList []string) error { + setupFile := wrapper.NewFile(filepath.Join(sg.ModulePath, "apis"), "apis", templates.SetupTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), + wrapper.WithHeaderPath(sg.LicenseHeaderPath), ) aliases := make([]string, len(versionPkgList)) for i, pkgPath := range versionPkgList { @@ -56,6 +58,6 @@ func (rg *SetupGenerator) Generate(versionPkgList []string) error { vars := map[string]interface{}{ "Aliases": aliases, } - filePath := filepath.Join(rg.LocalDirectoryPath, "zz_setup.go") + filePath := filepath.Join(sg.LocalDirectoryPath, "zz_setup.go") return errors.Wrap(setupFile.Write(filePath, vars, os.ModePerm), "cannot write setup file") } diff --git a/pkg/pipeline/terraformed.go b/pkg/pipeline/terraformed.go index 7c99f83b..df9acf50 100644 --- a/pkg/pipeline/terraformed.go +++ b/pkg/pipeline/terraformed.go @@ -31,9 +31,10 @@ import ( ) // NewTerraformedGenerator returns a new TerraformedGenerator. -func NewTerraformedGenerator(pkg *types.Package, localDirectoryPath string) *TerraformedGenerator { +func NewTerraformedGenerator(pkg *types.Package, rootDir, group, version string) *TerraformedGenerator { return &TerraformedGenerator{ - LocalDirectoryPath: localDirectoryPath, + LocalDirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version), + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), pkg: pkg, } } @@ -42,6 +43,7 @@ func NewTerraformedGenerator(pkg *types.Package, localDirectoryPath string) *Ter // interface on CRD structs. type TerraformedGenerator struct { LocalDirectoryPath string + LicenseHeaderPath string pkg *types.Package } @@ -50,7 +52,7 @@ type TerraformedGenerator struct { func (tg *TerraformedGenerator) Generate(cfg *config.Resource) error { trFile := wrapper.NewFile(tg.pkg.Path(), tg.pkg.Name(), templates.TerraformedTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), // todo + wrapper.WithHeaderPath(tg.LicenseHeaderPath), ) vars := map[string]interface{}{ "CRD": map[string]string{ diff --git a/pkg/pipeline/version.go b/pkg/pipeline/version.go index 6dd44c81..6d9c6897 100644 --- a/pkg/pipeline/version.go +++ b/pkg/pipeline/version.go @@ -31,22 +31,23 @@ import ( // NewVersionGenerator returns a new VersionGenerator. func NewVersionGenerator(rootDir, modulePath, group, version string) *VersionGenerator { pkgPath := filepath.Join(modulePath, "apis", strings.ToLower(strings.Split(group, ".")[0]), version) - directoryPath := filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version) return &VersionGenerator{ - directoryPath: directoryPath, - Group: group, - Version: version, - pkg: types.NewPackage(pkgPath, version), + Group: group, + Version: version, + DirectoryPath: filepath.Join(rootDir, "apis", strings.ToLower(strings.Split(group, ".")[0]), version), + LicenseHeaderPath: filepath.Join(rootDir, "hack", "boilerplate.go.txt"), + pkg: types.NewPackage(pkgPath, version), } } // VersionGenerator generates files for a version of a specific group. type VersionGenerator struct { - Group string - Version string + Group string + Version string + DirectoryPath string + LicenseHeaderPath string - directoryPath string - pkg *types.Package + pkg *types.Package } // Generate writes doc and group version info files to the disk. @@ -59,10 +60,10 @@ func (vg *VersionGenerator) Generate() error { } gviFile := wrapper.NewFile(vg.pkg.Path(), vg.Version, templates.GroupVersionInfoTemplate, wrapper.WithGenStatement(GenStatement), - wrapper.WithHeaderPath("hack/boilerplate.go.txt"), // todo + wrapper.WithHeaderPath(vg.LicenseHeaderPath), ) return errors.Wrap( - gviFile.Write(filepath.Join(vg.directoryPath, "zz_groupversion_info.go"), vars, os.ModePerm), + gviFile.Write(filepath.Join(vg.DirectoryPath, "zz_groupversion_info.go"), vars, os.ModePerm), "cannot write group version info file", ) } @@ -71,8 +72,3 @@ func (vg *VersionGenerator) Generate() error { func (vg *VersionGenerator) Package() *types.Package { return vg.pkg } - -// DirectoryPath returns the path to the directory of the version. -func (vg *VersionGenerator) DirectoryPath() string { - return vg.directoryPath -}