Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Use explicit root directory so that generator can be run from anywhere #145

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/pipeline/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand All @@ -42,14 +43,15 @@ type ControllerGenerator struct {
Group string
ControllerGroupDir string
ModulePath string
LicenseHeaderPath string
}

// Generate writes controller setup functions.
func (cg *ControllerGenerator) Generate(cfg *config.Resource, typesPkgPath string) (pkgPath string, err error) {
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{}{
Expand Down
8 changes: 5 additions & 3 deletions pkg/pipeline/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,6 +52,7 @@ type CRDGenerator struct {
LocalDirectoryPath string
Group string
ProviderShortName string
LicenseHeaderPath string

pkg *types.Package
}
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/pipeline/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -39,14 +40,15 @@ 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
// list of version packages.
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 {
Expand Down
19 changes: 7 additions & 12 deletions pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package pipeline

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
Expand All @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions pkg/pipeline/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ 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,
}
}

// 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 {
Expand All @@ -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")
}
8 changes: 5 additions & 3 deletions pkg/pipeline/terraformed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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
}
Expand All @@ -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{
Expand Down
28 changes: 12 additions & 16 deletions pkg/pipeline/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
)
}
Expand All @@ -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
}