Schemas: Reduce duplicated jenny code (#84061)

* Remove jenny_ts_resources and use jenny_ts_types for all cases

* Unify TS generated files into one jenny

* Add missing imports to versioned files

* Update Parca plugin

* Fix loki

* Use LokiQuery

* Fix pyroscope tests

* Fix prettier

* 😒 fix default pyroscope value name

* Set the LokiQuery

* Update Elasticsearch and TestData

* Missed files from testdata

* Order imports
This commit is contained in:
Selene
2024-03-11 12:51:44 +01:00
committed by GitHub
parent bd9f9c9eb0
commit 275ccf994b
97 changed files with 320 additions and 429 deletions
@@ -1,101 +0,0 @@
package codegen
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/grafana/codejen"
tsast "github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/grafana/pkg/build"
corecodegen "github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/plugins/pfs"
"github.com/grafana/kindsys"
"github.com/grafana/thema"
)
func PluginTSEachMajor(rt *thema.Runtime) codejen.OneToMany[*pfs.PluginDecl] {
latestMajorsOrX := corecodegen.LatestMajorsOrXJenny(filepath.Join("packages", "grafana-schema", "src", "raw", "composable"), false, corecodegen.TSTypesJenny{})
return &pleJenny{
inner: kinds2pd(rt, latestMajorsOrX),
}
}
type pleJenny struct {
inner codejen.OneToMany[*pfs.PluginDecl]
}
func (*pleJenny) JennyName() string {
return "PluginEachMajorJenny"
}
func (j *pleJenny) Generate(decl *pfs.PluginDecl) (codejen.Files, error) {
if !decl.HasSchema() {
return nil, nil
}
jf, err := j.inner.Generate(decl)
if err != nil {
return nil, err
}
version := "export const pluginVersion = \"%s\";"
if decl.PluginMeta.Version != nil {
version = fmt.Sprintf(version, *decl.PluginMeta.Version)
} else {
version = fmt.Sprintf(version, getGrafanaVersion())
}
files := make(codejen.Files, len(jf))
for i, file := range jf {
tsf := &tsast.File{}
for _, im := range decl.Imports {
if tsim, err := cuectx.ConvertImport(im); err != nil {
return nil, err
} else if tsim.From.Value != "" {
tsf.Imports = append(tsf.Imports, tsim)
}
}
tsf.Nodes = append(tsf.Nodes, tsast.Raw{
Data: version,
})
tsf.Nodes = append(tsf.Nodes, tsast.Raw{
Data: string(file.Data),
})
data := []byte(tsf.String())
data = data[:len(data)-1] // remove the additional line break added by the inner jenny
files[i] = *codejen.NewFile(file.RelativePath, data, append(file.From, j)...)
}
return files, nil
}
func kinds2pd(rt *thema.Runtime, j codejen.OneToMany[kindsys.Kind]) codejen.OneToMany[*pfs.PluginDecl] {
return codejen.AdaptOneToMany(j, func(pd *pfs.PluginDecl) kindsys.Kind {
kd, err := kindsys.BindComposable(rt, pd.KindDecl)
if err != nil {
return nil
}
return kd
})
}
func getGrafanaVersion() string {
dir, err := os.Getwd()
if err != nil {
return ""
}
pkg, err := build.OpenPackageJSON(path.Join(dir, "../../../"))
if err != nil {
return ""
}
return pkg.Version
}
+74 -12
View File
@@ -2,19 +2,25 @@ package codegen
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/grafana/codejen"
tsast "github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/grafana/pkg/build"
"github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/plugins/pfs"
)
func PluginTSTypesJenny(root string, inner codejen.OneToOne[*pfs.PluginDecl]) codejen.OneToOne[*pfs.PluginDecl] {
var versionedPluginPath = filepath.Join("packages", "grafana-schema", "src", "raw", "composable")
func PluginTSTypesJenny(root string) codejen.OneToMany[*pfs.PluginDecl] {
return &ptsJenny{
root: root,
inner: inner,
inner: adaptToPipeline(codegen.TSTypesJenny{}),
}
}
@@ -24,21 +30,23 @@ type ptsJenny struct {
}
func (j *ptsJenny) JennyName() string {
return "PluginTSTypesJenny"
return "PluginTsTypesJenny"
}
func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (codejen.Files, error) {
if !decl.HasSchema() {
return nil, nil
}
tsf := &tsast.File{}
genFile := &tsast.File{}
versionedFile := &tsast.File{}
for _, im := range decl.Imports {
if tsim, err := cuectx.ConvertImport(im); err != nil {
return nil, err
} else if tsim.From.Value != "" {
tsf.Imports = append(tsf.Imports, tsim)
genFile.Imports = append(genFile.Imports, tsim)
versionedFile.Imports = append(versionedFile.Imports, tsim)
}
}
@@ -47,13 +55,67 @@ func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
return nil, err
}
tsf.Nodes = append(tsf.Nodes, tsast.Raw{
Data: string(jf.Data),
})
rawData := tsast.Raw{Data: string(jf.Data)}
rawVersion := tsast.Raw{
Data: getPluginVersion(decl.PluginMeta.Version),
}
path := filepath.Join(j.root, decl.PluginPath, fmt.Sprintf("%s.gen.ts", strings.ToLower(decl.SchemaInterface.Name)))
data := []byte(tsf.String())
genFile.Nodes = append(genFile.Nodes, rawData)
genPath := filepath.Join(j.root, decl.PluginPath, fmt.Sprintf("%s.gen.ts", strings.ToLower(decl.SchemaInterface.Name)))
data := []byte(genFile.String())
data = data[:len(data)-1] // remove the additional line break added by the inner jenny
return codejen.NewFile(path, data, append(jf.From, j)...), nil
files := make(codejen.Files, 2)
files[0] = *codejen.NewFile(genPath, data, append(jf.From, j)...)
versionedFile.Nodes = append(versionedFile.Nodes, rawVersion, rawData)
versionedData := []byte(versionedFile.String())
versionedData = versionedData[:len(versionedData)-1]
pluginFolder := strings.ReplaceAll(strings.ToLower(decl.PluginMeta.Name), " ", "")
versionedPath := filepath.Join(versionedPluginPath, pluginFolder, strings.ToLower(decl.SchemaInterface.Name), "x", jf.RelativePath)
files[1] = *codejen.NewFile(versionedPath, versionedData, append(jf.From, j)...)
return files, nil
}
func getPluginVersion(pluginVersion *string) string {
version := "export const pluginVersion = \"%s\";"
if pluginVersion != nil {
version = fmt.Sprintf(version, *pluginVersion)
} else {
version = fmt.Sprintf(version, getGrafanaVersion())
}
return version
}
func adaptToPipeline(j codejen.OneToOne[codegen.SchemaForGen]) codejen.OneToOne[*pfs.PluginDecl] {
return codejen.AdaptOneToOne(j, func(pd *pfs.PluginDecl) codegen.SchemaForGen {
name := strings.ReplaceAll(pd.PluginMeta.Name, " ", "")
if pd.SchemaInterface.Name == "DataQuery" {
name = name + "DataQuery"
}
return codegen.SchemaForGen{
Name: name,
Schema: pd.Lineage.Latest(),
IsGroup: pd.SchemaInterface.IsGroup,
}
})
}
func getGrafanaVersion() string {
dir, err := os.Getwd()
if err != nil {
return ""
}
pkg, err := build.OpenPackageJSON(path.Join(dir, "../../../"))
if err != nil {
return ""
}
return pkg.Version
}