mirror of https://github.com/grafana/grafana
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
pull/83223/head^2
parent
bd9f9c9eb0
commit
275ccf994b
@ -1,85 +0,0 @@ |
||||
package codegen |
||||
|
||||
import ( |
||||
"github.com/grafana/codejen" |
||||
"github.com/grafana/cuetsy" |
||||
"github.com/grafana/cuetsy/ts" |
||||
"github.com/grafana/cuetsy/ts/ast" |
||||
"github.com/grafana/grafana/pkg/cuectx" |
||||
"github.com/grafana/thema/encoding/typescript" |
||||
) |
||||
|
||||
// TSResourceJenny is a [OneToOne] that produces TypeScript types and
|
||||
// defaults for a Thema schema.
|
||||
//
|
||||
// Thema's generic TS jenny will be able to replace this one once
|
||||
// https://github.com/grafana/thema/issues/89 is complete.
|
||||
type TSResourceJenny struct{} |
||||
|
||||
var _ codejen.OneToOne[SchemaForGen] = &TSResourceJenny{} |
||||
|
||||
func (j TSResourceJenny) JennyName() string { |
||||
return "TSResourceJenny" |
||||
} |
||||
|
||||
func (j TSResourceJenny) Generate(sfg SchemaForGen) (*codejen.File, error) { |
||||
// TODO allow using name instead of machine name in thema generator
|
||||
f, err := typescript.GenerateTypes(sfg.Schema, &typescript.TypeConfig{ |
||||
RootName: sfg.Name, |
||||
Group: sfg.IsGroup, |
||||
CuetsyConfig: &cuetsy.Config{ |
||||
Export: true, |
||||
ImportMapper: cuectx.MapCUEImportToTS, |
||||
}, |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
renameSpecNode(sfg.Name, f) |
||||
|
||||
return codejen.NewFile(sfg.Schema.Lineage().Name()+"_types.gen.ts", []byte(f.String()), j), nil |
||||
} |
||||
|
||||
func renameSpecNode(name string, tf *ast.File) { |
||||
specidx, specdefidx := -1, -1 |
||||
for idx, def := range tf.Nodes { |
||||
// Peer through export keywords
|
||||
if ex, is := def.(ast.ExportKeyword); is { |
||||
def = ex.Decl |
||||
} |
||||
|
||||
switch x := def.(type) { |
||||
case ast.TypeDecl: |
||||
if x.Name.Name == "spec" { |
||||
specidx = idx |
||||
x.Name.Name = name |
||||
tf.Nodes[idx] = x |
||||
} |
||||
case ast.VarDecl: |
||||
// Before:
|
||||
// export const defaultspec: Partial<spec> = {
|
||||
// After:
|
||||
/// export const defaultPlaylist: Partial<Playlist> = {
|
||||
if x.Names.Idents[0].Name == "defaultspec" { |
||||
specdefidx = idx |
||||
x.Names.Idents[0].Name = "default" + name |
||||
tt := x.Type.(ast.TypeTransformExpr) |
||||
tt.Expr = ts.Ident(name) |
||||
x.Type = tt |
||||
tf.Nodes[idx] = x |
||||
} |
||||
} |
||||
} |
||||
|
||||
if specidx != -1 { |
||||
decl := tf.Nodes[specidx] |
||||
tf.Nodes = append(append(tf.Nodes[:specidx], tf.Nodes[specidx+1:]...), decl) |
||||
} |
||||
if specdefidx != -1 { |
||||
if specdefidx > specidx { |
||||
specdefidx-- |
||||
} |
||||
decl := tf.Nodes[specdefidx] |
||||
tf.Nodes = append(append(tf.Nodes[:specdefidx], tf.Nodes[specdefidx+1:]...), decl) |
||||
} |
||||
} |
@ -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 |
||||
} |
Loading…
Reference in new issue