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
This commit is contained in:
Joan López de la Franca Beltran 2022-12-12 15:47:19 +01:00 committed by GitHub
parent 8e77a42741
commit 61cd9eeb24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 166 additions and 2 deletions

View File

@ -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

View File

@ -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"

97
kinds/report.go Normal file
View File

@ -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)
}

54
kinds/report.json Normal file
View File

@ -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": []
}

View File

@ -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 {

View File

@ -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.