From 61cd9eeb24dd26717af7c22230e5a44b94471294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= <5459617+joanlopez@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:47:19 +0100 Subject: [PATCH] Kinds: Generate JSON report (#59742) * Kinds: Generate JSON report * Kinds: Add Validate/Write support for JSON report * Kinds: Report format * Kinds: Report new line * Kinds: Use kindsys.SomeKindMeta to generate the report * Move kinds report generation to the end * Re-structure kinds report JSON output * Make prettier to ignore kinds json report * Minor on kinds report generation * Fix toggles_gen test --- .prettierignore | 3 ++ Makefile | 1 + kinds/report.go | 97 +++++++++++++++++++++++++++++++++++++++++++++ kinds/report.json | 54 +++++++++++++++++++++++++ pkg/kindsys/kind.go | 4 ++ pkg/kindsys/load.go | 9 ++++- 6 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 kinds/report.go create mode 100644 kinds/report.json diff --git a/.prettierignore b/.prettierignore index a54ca37d5e4..cc9c39938ad 100644 --- a/.prettierignore +++ b/.prettierignore @@ -30,3 +30,6 @@ theme.dark.generated.json public/api-spec.json public/api-merged.json public/openapi3.json + +# Generated Kinds report +kinds/report.json diff --git a/Makefile b/Makefile index 5c3cf3f5256..aea67a0639e 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ gen-cue: ## Do all CUE/Thema code generation go generate ./kinds/gen.go go generate ./pkg/framework/coremodel go generate ./public/app/plugins/gen.go + go generate ./kinds/report.go gen-go: $(WIRE) gen-cue @echo "generate go files" diff --git a/kinds/report.go b/kinds/report.go new file mode 100644 index 00000000000..993c31331e6 --- /dev/null +++ b/kinds/report.go @@ -0,0 +1,97 @@ +//go:build ignore +// +build ignore + +//go:generate go run report.go + +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + + "github.com/grafana/codejen" + "github.com/grafana/grafana/pkg/kindsys" + "github.com/grafana/grafana/pkg/registry/corekind" +) + +const reportFileName = "report.json" + +func main() { + report := buildKindStateReport() + reportJSON := elsedie(json.MarshalIndent(report, "", " "))("error generating json output") + + path := filepath.Join(kindsys.DeclParentPath, reportFileName) + file := codejen.NewFile(path, reportJSON, reportJenny{}) + filesystem := elsedie(file.ToFS())("error building in-memory file system") + + cwd := elsedie(os.Getwd())("error getting working directory") + groot := filepath.Dir(cwd) + + if _, set := os.LookupEnv("CODEGEN_VERIFY"); set { + if err := filesystem.Verify(context.Background(), groot); err != nil { + die(fmt.Errorf("generated code is out of sync with inputs:\n%s\nrun `make gen-cue` to regenerate", err)) + } + } else if err := filesystem.Write(context.Background(), groot); err != nil { + die(fmt.Errorf("error while writing generated code to disk:\n%s", err)) + } +} + +type KindStateReport struct { + Core []kindsys.CoreStructuredProperties `json:"core"` + Raw []kindsys.RawProperties `json:"raw"` + Composable []kindsys.ComposableProperties `json:"composable"` +} + +func emptyKindStateReport() KindStateReport { + return KindStateReport{ + Core: make([]kindsys.CoreStructuredProperties, 0), + Raw: make([]kindsys.RawProperties, 0), + Composable: make([]kindsys.ComposableProperties, 0), + } +} + +func buildKindStateReport() KindStateReport { + r := emptyKindStateReport() + b := corekind.NewBase(nil) + + for _, k := range b.All() { + switch props := k.Props().(type) { + case kindsys.CoreStructuredProperties: + r.Core = append(r.Core, props) + case kindsys.RawProperties: + r.Raw = append(r.Raw, props) + case kindsys.ComposableProperties: + r.Composable = append(r.Composable, props) + } + } + + return r +} + +type reportJenny struct{} + +func (reportJenny) JennyName() string { + return "ReportJenny" +} + +func elsedie[T any](t T, err error) func(msg string) T { + if err != nil { + return func(msg string) T { + fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err) + os.Exit(1) + return t + } + } + + return func(msg string) T { + return t + } +} + +func die(err error) { + fmt.Fprint(os.Stderr, err, "\n") + os.Exit(1) +} diff --git a/kinds/report.json b/kinds/report.json new file mode 100644 index 00000000000..9dd5449d52f --- /dev/null +++ b/kinds/report.json @@ -0,0 +1,54 @@ +{ + "core": [ + { + "name": "Dashboard", + "pluralName": "Dashboards", + "machineName": "dashboard", + "pluralMachineName": "dashboards", + "lineageIsGroup": false, + "maturity": "merged", + "currentVersion": [ + 0, + 0 + ] + }, + { + "name": "Playlist", + "pluralName": "Playlists", + "machineName": "playlist", + "pluralMachineName": "playlists", + "lineageIsGroup": false, + "maturity": "merged", + "currentVersion": [ + 0, + 0 + ] + }, + { + "name": "Team", + "pluralName": "Teams", + "machineName": "team", + "pluralMachineName": "teams", + "lineageIsGroup": false, + "maturity": "merged", + "currentVersion": [ + 0, + 0 + ] + } + ], + "raw": [ + { + "name": "SVG", + "pluralName": "SVGs", + "machineName": "svg", + "pluralMachineName": "svgs", + "lineageIsGroup": false, + "maturity": "merged", + "extensions": [ + "svg" + ] + } + ], + "composable": [] +} \ No newline at end of file diff --git a/pkg/kindsys/kind.go b/pkg/kindsys/kind.go index 2add53e98ad..a64a66c0f3a 100644 --- a/pkg/kindsys/kind.go +++ b/pkg/kindsys/kind.go @@ -41,6 +41,10 @@ func (m Maturity) Less(om Maturity) bool { return maturityIdx(m) < maturityIdx(om) } +func (m Maturity) String() string { + return string(m) +} + // Interface describes a Grafana kind object: a Go representation of the definition of // one of Grafana's categories of kinds. type Interface interface { diff --git a/pkg/kindsys/load.go b/pkg/kindsys/load.go index 710cb425b49..f250354758d 100644 --- a/pkg/kindsys/load.go +++ b/pkg/kindsys/load.go @@ -12,14 +12,19 @@ import ( "github.com/grafana/thema" ) +// DeclParentPath is the path, relative to the repository root, where +// each child directory is expected to contain directories with .cue files, +// declaring one kind. +var DeclParentPath = "kinds" + // CoreStructuredDeclParentPath is the path, relative to the repository root, where // each child directory is expected to contain .cue files declaring one // CoreStructured kind. -var CoreStructuredDeclParentPath = filepath.Join("kinds", "structured") +var CoreStructuredDeclParentPath = filepath.Join(DeclParentPath, "structured") // RawDeclParentPath is the path, relative to the repository root, where each child // directory is expected to contain .cue files declaring one Raw kind. -var RawDeclParentPath = filepath.Join("kinds", "raw") +var RawDeclParentPath = filepath.Join(DeclParentPath, "raw") // GoCoreKindParentPath is the path, relative to the repository root, to the directory // containing one directory per kind, full of generated Go kind output: types and bindings.