Skip to content
Merged
14 changes: 4 additions & 10 deletions cmd/grafana-app-sdk/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ func generateCmdFunc(cmd *cobra.Command, _ []string) error {

//nolint:funlen,goconst
func generateKindsCue(parser *cuekind.Parser, cfg *config.Config) (codejen.Files, error) {
// Slightly hacky multiple generators as an intermediary while we move to a better system.
// Both still source from a Manifest, but generatorForKinds supplies []Kind to jennies, vs AppManifest
generatorForKinds, err := codegen.NewGenerator(parser.KindParser())
if err != nil {
return nil, err
}
generatorForManifest, err := codegen.NewGenerator(parser.ManifestParser())
if err != nil {
return nil, err
Expand All @@ -264,14 +258,14 @@ func generateKindsCue(parser *cuekind.Parser, cfg *config.Config) (codejen.Files
}

// Resource
resourceFiles, err := generatorForKinds.Generate(cuekind.ResourceGenerator(goModule, goModGenPath, cfg.GroupKinds()), cfg.ManifestSelectors...)
resourceFiles, err := generatorForManifest.Generate(cuekind.ResourceGenerator(goModule, goModGenPath, cfg.GroupKinds()), cfg.ManifestSelectors...)
if err != nil {
return nil, err
}
for i, f := range resourceFiles {
resourceFiles[i].RelativePath = filepath.Join(cfg.Codegen.GoGenPath, f.RelativePath)
}
tsResourceFiles, err := generatorForKinds.Generate(cuekind.TypeScriptResourceGenerator(), cfg.ManifestSelectors...)
tsResourceFiles, err := generatorForManifest.Generate(cuekind.TypeScriptResourceGenerator(), cfg.ManifestSelectors...)
if err != nil {
return nil, err
}
Expand All @@ -287,7 +281,7 @@ func generateKindsCue(parser *cuekind.Parser, cfg *config.Config) (codejen.Files
if cfg.Definitions.Encoding == "yaml" {
encFunc = yaml.Marshal
}
crdFiles, err = generatorForKinds.Generate(cuekind.CRDGenerator(encFunc, cfg.Definitions.Encoding), cfg.ManifestSelectors...)
crdFiles, err = generatorForManifest.Generate(cuekind.CRDGenerator(encFunc, cfg.Definitions.Encoding), cfg.ManifestSelectors...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -345,7 +339,7 @@ func postGenerateFilesCue(parser *cuekind.Parser, cfg *config.Config) (codejen.F
if err != nil {
return nil, err
}
generator, err := codegen.NewGenerator[codegen.Kind](parser.KindParser())
generator, err := codegen.NewGenerator[codegen.AppManifest](parser.ManifestParser())
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/grafana-app-sdk/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ func projectAddComponent(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
generator, err = codegen.NewGenerator(parser.KindParser())
manifestParser = parser.ManifestParser()
generator, err = codegen.NewGenerator(manifestParser)
if err != nil {
return err
}
manifestParser = parser.ManifestParser()
default:
return fmt.Errorf("unknown kind format '%s'", format)
}
Expand All @@ -622,7 +622,7 @@ func projectAddComponent(cmd *cobra.Command, args []string) error {
case "backend":
switch format {
case FormatCUE:
err = addComponentBackend(path, generator.(*codegen.Generator[codegen.Kind]), cfg.ManifestSelectors, manifest.Properties().Group, cfg.Kinds.Grouping == config.KindGroupingGroup)
err = addComponentBackend(path, generator.(*codegen.Generator[codegen.AppManifest]), cfg.ManifestSelectors, manifest.Properties().Group, cfg.Kinds.Grouping == config.KindGroupingGroup)
default:
return fmt.Errorf("unknown kind format '%s'", format)
}
Expand All @@ -639,7 +639,7 @@ func projectAddComponent(cmd *cobra.Command, args []string) error {
case "operator":
switch format {
case FormatCUE:
err = addComponentOperator(path, generator.(*codegen.Generator[codegen.Kind]), cfg.ManifestSelectors, cfg.Kinds.Grouping == config.KindGroupingGroup, !overwrite)
err = addComponentOperator(path, generator.(*codegen.Generator[codegen.AppManifest]), cfg.ManifestSelectors, cfg.Kinds.Grouping == config.KindGroupingGroup, !overwrite)
default:
return fmt.Errorf("unknown kind format '%s'", format)
}
Expand All @@ -659,7 +659,7 @@ func projectAddComponent(cmd *cobra.Command, args []string) error {
}

type anyGenerator interface {
*codegen.Generator[codegen.Kind]
*codegen.Generator[codegen.AppManifest]
}

//nolint:revive
Expand All @@ -681,7 +681,7 @@ func addComponentOperator[G anyGenerator](projectRootPath string, generator G, s

var files codejen.Files
switch cast := any(generator).(type) {
case *codegen.Generator[codegen.Kind]:
case *codegen.Generator[codegen.AppManifest]:
files, err = cast.Generate(cuekind.OperatorGenerator(repo, "pkg/generated", groupKinds), selectors...)
if err != nil {
return err
Expand Down Expand Up @@ -776,7 +776,7 @@ func projectAddPluginAPI[G anyGenerator](generator G, repo, generatedAPIModelsPa
var files codejen.Files
var err error
switch cast := any(generator).(type) {
case *codegen.Generator[codegen.Kind]:
case *codegen.Generator[codegen.AppManifest]:
files, err = cast.Generate(cuekind.BackendPluginGenerator(repo, generatedAPIModelsPath, groupKinds), selectors...)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/grafana-app-sdk/project_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func projectLocalEnvGenerate(cmd *cobra.Command, _ []string) error {
if err != nil {
return nil, err
}
generator, err := codegen.NewGenerator(parser.KindParser())
generator, err := codegen.NewGenerator(parser.ManifestParser())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/grafana-app-sdk/templates/Makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ endif
.PHONY: build/operator
build/operator:
ifeq ("$(wildcard cmd/operator/Dockerfile)","cmd/operator/Dockerfile")
docker build -t $(OPERATOR_DOCKERIMAGE) -f cmd/operator/Dockerfile .
docker build -t $(OPERATOR_DOCKERIMAGE) -f cmd/operator/Dockerfile .
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by that I noticed when doing new project testing--this was four spaces instead of a tab.

else
@echo "No cmd/operator/Dockerfile, skipping operator build"
endif
Expand Down
41 changes: 17 additions & 24 deletions codegen/cuekind/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

// CRDGenerator returns a Generator which will create a CRD file
func CRDGenerator(outputEncoder jennies.CRDOutputEncoder, outputExtension string) *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer(namerFunc)
func CRDGenerator(outputEncoder jennies.CRDOutputEncoder, outputExtension string) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(jennies.CRDGenerator(outputEncoder, outputExtension))
return g
}
Expand All @@ -27,8 +27,8 @@ func CRDGenerator(outputEncoder jennies.CRDOutputEncoder, outputExtension string
// If `groupKinds` is true, kinds within the same group will exist in the same package.
// When combined with `versioned`, each version package will contain all kinds in the group
// which have a schema for that version.
func ResourceGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer(namerFunc)
func ResourceGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
&jennies.GoTypes{
Depth: 1,
Expand Down Expand Up @@ -63,21 +63,21 @@ func ResourceGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *c
}

// BackendPluginGenerator returns a Generator which will produce boilerplate backend plugin code
func BackendPluginGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.Kind] {
func BackendPluginGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
pluginSecurePkgFiles, _ := templates.GetBackendPluginSecurePackageFiles()

g := codejen.JennyListWithNamer(namerFunc)
g := codejen.JennyListWithNamer(namerFuncManifest)
g.Append(
jennies.RouterHandlerCodeGenerator(projectRepo, generatedAPIPath, !groupKinds),
jennies.StaticManyToOneGenerator[codegen.Kind](codejen.File{
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/data.go",
Data: pluginSecurePkgFiles["data.go"],
}),
jennies.StaticManyToOneGenerator[codegen.Kind](codejen.File{
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/middleware.go",
Data: pluginSecurePkgFiles["middleware.go"],
}),
jennies.StaticManyToOneGenerator[codegen.Kind](codejen.File{
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/retriever.go",
Data: pluginSecurePkgFiles["retriever.go"],
}),
Expand All @@ -88,8 +88,8 @@ func BackendPluginGenerator(projectRepo, generatedAPIPath string, groupKinds boo
}

// TypeScriptResourceGenerator returns a Generator which generates TypeScript resource code.
func TypeScriptResourceGenerator() *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer(namerFunc)
func TypeScriptResourceGenerator() *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(&jennies.TypeScriptTypes{
Depth: 1,
}, &jennies.TypeScriptResourceTypes{})
Expand All @@ -98,8 +98,8 @@ func TypeScriptResourceGenerator() *codejen.JennyList[codegen.Kind] {

// OperatorGenerator returns a Generator which will build out watcher boilerplate for each resource,
// and a main func to run an operator for the watchers.
func OperatorGenerator(projectRepo, codegenPath string, groupKinds bool) *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer[codegen.Kind](namerFunc)
func OperatorGenerator(projectRepo, codegenPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
&jennies.OperatorKubeConfigJenny{},
jennies.OperatorMainJenny(projectRepo, codegenPath, !groupKinds),
Expand All @@ -108,12 +108,12 @@ func OperatorGenerator(projectRepo, codegenPath string, groupKinds bool) *codeje
return g
}

func AppGenerator(projectRepo, codegenPath string, manifestGoFilePath string, groupKinds bool) *codejen.JennyList[codegen.Kind] {
func AppGenerator(projectRepo, codegenPath string, manifestGoFilePath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
parts := strings.Split(projectRepo, "/")
if len(parts) == 0 {
parts = []string{""}
}
g := codejen.JennyListWithNamer[codegen.Kind](namerFunc)
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
jennies.WatcherJenny(projectRepo, codegenPath, !groupKinds),
&jennies.AppGenerator{
Expand All @@ -127,8 +127,8 @@ func AppGenerator(projectRepo, codegenPath string, manifestGoFilePath string, gr
return g
}

func PostResourceGenerationGenerator(projectRepo, goGenPath string, groupKinds bool) *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer[codegen.Kind](namerFunc)
func PostResourceGenerationGenerator(projectRepo, goGenPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(&jennies.OpenAPI{
GoModName: projectRepo,
GoGenPath: goGenPath,
Expand Down Expand Up @@ -184,13 +184,6 @@ func ManifestGoGenerator(pkg string, includeSchemas bool, projectRepo, goGenPath
return g
}

func namerFunc(k codegen.Kind) string {
if k == nil {
return "nil"
}
return k.Properties().Kind
}

func namerFuncManifest(m codegen.AppManifest) string {
if m == nil {
return "nil"
Expand Down
8 changes: 4 additions & 4 deletions codegen/cuekind/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCRDGenerator(t *testing.T) {

parser, err := NewParser(testingCue(t), true, false)
require.NoError(t, err)
kinds, err := parser.KindParser().Parse("customManifest", "testManifest")
kinds, err := parser.ManifestParser().Parse("customManifest", "testManifest")
require.NoError(t, err)

t.Run("JSON", func(t *testing.T) {
Expand Down Expand Up @@ -51,9 +51,9 @@ func TestResourceGenerator(t *testing.T) {

parser, err := NewParser(testingCue(t), true, false)
require.NoError(t, err)
kinds, err := parser.KindParser().Parse("customManifest")
kinds, err := parser.ManifestParser().Parse("customManifest")
require.NoError(t, err)
sameGroupKinds, err := parser.KindParser().Parse("testManifest")
sameGroupKinds, err := parser.ManifestParser().Parse("testManifest")
require.NoError(t, err)

t.Run("group by kind", func(t *testing.T) {
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTypeScriptResourceGenerator(t *testing.T) {
require.NoError(t, err)

t.Run("versioned", func(t *testing.T) {
kinds, err := parser.KindParser().Parse("customManifest")
kinds, err := parser.ManifestParser().Parse("customManifest")
require.NoError(t, err)
files, err := TypeScriptResourceGenerator().Generate(kinds...)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions codegen/cuekind/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (p *Parser) ManifestParser() codegen.Parser[codegen.AppManifest] {
// KindParser returns a Parser that returns a list of codegen.Kind.
// If useManifest is true, it will load kinds from a manifest provided by the selector(s) in Parse (or DefaultManifestSelector if no selectors are present),
// rather than loading the selector(s) as kinds.
// Deprecated: Use ManifestParser instead, Kind is deprecated
//
//nolint:revive
func (p *Parser) KindParser() codegen.Parser[codegen.Kind] {
Expand Down
34 changes: 25 additions & 9 deletions codegen/jennies/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package jennies
import (
"bytes"
"go/format"
"slices"
"strings"

"github.com/grafana/codejen"

Expand All @@ -22,7 +24,7 @@ func (*AppGenerator) JennyName() string {
return "App"
}

func (a *AppGenerator) Generate(kinds ...codegen.Kind) (*codejen.File, error) {
func (a *AppGenerator) Generate(appManifest codegen.AppManifest) (*codejen.File, error) {
tmd := templates.AppMetadata{
Repo: a.ProjectRepo,
ProjectName: a.ProjectName,
Expand All @@ -34,16 +36,30 @@ func (a *AppGenerator) Generate(kinds ...codegen.Kind) (*codejen.File, error) {
ManifestPackagePath: a.ManifestPackagePath,
}

for _, kind := range kinds {
vers := make([]string, len(kind.Versions()))
for i, ver := range kind.Versions() {
vers[i] = ver.Version
appMetadataByKind := make(map[string]templates.AppMetadataKind)

for version, kind := range codegen.VersionedKinds(appManifest) {
meta, ok := appMetadataByKind[kind.Kind]
if !ok {
meta = templates.AppMetadataKind{
KindProperties: versionedKindToKindProperties(kind, appManifest),
Versions: make([]string, 0),
}
}
meta.Versions = append(meta.Versions, version.Name())
if version.Name() == appManifest.Properties().PreferredVersion {
meta.KindProperties = versionedKindToKindProperties(kind, appManifest)
}
tmd.Resources = append(tmd.Resources, templates.AppMetadataKind{
KindProperties: kind.Properties(),
Versions: vers,
})
appMetadataByKind[kind.Kind] = meta
}

for _, meta := range appMetadataByKind {
tmd.Resources = append(tmd.Resources, meta)
}
// Sort for deterministic output
slices.SortFunc(tmd.Resources, func(a, b templates.AppMetadataKind) int {
return strings.Compare(a.Kind, b.Kind)
})

b := bytes.Buffer{}
err := templates.WriteAppGoFile(tmd, &b)
Expand Down
14 changes: 7 additions & 7 deletions codegen/jennies/backendplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/grafana/grafana-app-sdk/codegen/templates"
)

// BackendPluginMainGenerator returns a many-to-one jenny which generates the `main.go` file needed to run the backend plugin.
func BackendPluginMainGenerator(projectRepo, apiCodegenPath string, groupByKind bool) codejen.ManyToOne[codegen.Kind] {
// BackendPluginMainGenerator returns a one-to-one jenny which generates the `main.go` file needed to run the backend plugin.
func BackendPluginMainGenerator(projectRepo, apiCodegenPath string, groupByKind bool) codejen.OneToOne[codegen.AppManifest] {
return &backendPluginMainGenerator{
projectRepo: projectRepo,
apiCodegenPath: apiCodegenPath,
Expand All @@ -26,7 +26,7 @@ type backendPluginMainGenerator struct {
groupByKind bool
}

func (m *backendPluginMainGenerator) Generate(decls ...codegen.Kind) (*codejen.File, error) {
func (m *backendPluginMainGenerator) Generate(appManifest codegen.AppManifest) (*codejen.File, error) {
tmd := templates.BackendPluginRouterTemplateMetadata{
Repo: m.projectRepo,
APICodegenPath: m.apiCodegenPath,
Expand All @@ -35,10 +35,10 @@ func (m *backendPluginMainGenerator) Generate(decls ...codegen.Kind) (*codejen.F
KindsAreGrouped: !m.groupByKind,
}

for _, decl := range decls {
tmd.Resources = append(tmd.Resources, decl.Properties())
if decl.Properties().Group != "" {
tmd.PluginID = strings.Split(decl.Properties().Group, ".")[0]
for _, kind := range codegen.PreferredVersionKinds(appManifest) {
tmd.Resources = append(tmd.Resources, versionedKindToKindProperties(kind, appManifest))
if appManifest.Properties().FullGroup != "" {
tmd.PluginID = strings.Split(appManifest.Properties().FullGroup, ".")[0]
}
}

Expand Down
Loading
Loading