mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 03:12:59 +08:00
Core: Remove thema and kindsys dependencies (#84499)
* Move some thema code inside grafana * Use new codegen instead of thema for core kinds * Replace TS generator * Use new generator for go types * Remove thema from oapi generator * Remove thema from generators * Don't use kindsys/thema for core kinds * Remove kindsys/thema from plugins * Remove last thema related * Remove most of cuectx and move utils_ts into codegen. It also deletes wire dependency * Merge plugins generators * Delete thema dependency 🎉 * Fix CODEOWNERS * Fix package name * Fix TS output names * More path fixes * Fix mod codeowners * Use original plugin's name * Remove kindsys dependency 🎉 * Modify oapi schema and create an apply function to fix elasticsearch errors * cue.mod was deleted by mistake * Fix TS panels * sort imports * Fixing elasticsearch output * Downgrade oapi-codegen library * Update output ts files * More fixes * Restore old elasticsearch generated file and skip its generation. Remove core imports into plugins * More lint fixes * Add codeowners * restore embed.go file * Fix embed.go
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/codejen"
|
||||
"github.com/grafana/grafana/pkg/plugins/pfs"
|
||||
)
|
||||
|
||||
var registryPath = filepath.Join("pkg", "registry", "schemas")
|
||||
@ -27,21 +28,22 @@ func (jenny *PluginRegistryJenny) JennyName() string {
|
||||
return "PluginRegistryJenny"
|
||||
}
|
||||
|
||||
func (jenny *PluginRegistryJenny) Generate(files []string) (*codejen.File, error) {
|
||||
if len(files) == 0 {
|
||||
func (jenny *PluginRegistryJenny) Generate(decls ...*pfs.PluginDecl) (*codejen.File, error) {
|
||||
if len(decls) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
schemas := make([]Schema, len(files))
|
||||
for i, file := range files {
|
||||
name, err := getSchemaName(file)
|
||||
schemas := make([]Schema, len(decls))
|
||||
for i, decl := range decls {
|
||||
variant := fmt.Sprintf("%s.cue", strings.ToLower(decl.SchemaInterface.Name))
|
||||
name, err := getSchemaName(decl.PluginPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to find schema name: %s", err)
|
||||
}
|
||||
|
||||
schemas[i] = Schema{
|
||||
Name: name,
|
||||
Filename: filepath.Base(file),
|
||||
FilePath: file,
|
||||
Filename: variant,
|
||||
FilePath: "./" + filepath.Join("public", "app", "plugins", decl.PluginPath, variant),
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,7 +67,7 @@ func getSchemaName(path string) (string, error) {
|
||||
if len(parts) < 2 {
|
||||
return "", fmt.Errorf("path should contain more than 2 elements")
|
||||
}
|
||||
folderName := parts[len(parts)-2]
|
||||
folderName := parts[len(parts)-1]
|
||||
if renamed, ok := renamedPlugins[folderName]; ok {
|
||||
folderName = renamed
|
||||
}
|
||||
|
@ -6,12 +6,9 @@ import (
|
||||
"strings"
|
||||
|
||||
copenapi "cuelang.org/go/encoding/openapi"
|
||||
"github.com/dave/dst/dstutil"
|
||||
"github.com/grafana/codejen"
|
||||
corecodegen "github.com/grafana/grafana/pkg/codegen"
|
||||
"github.com/grafana/grafana/pkg/codegen/generators"
|
||||
"github.com/grafana/grafana/pkg/plugins/pfs"
|
||||
"github.com/grafana/thema/encoding/gocode"
|
||||
"github.com/grafana/thema/encoding/openapi"
|
||||
)
|
||||
|
||||
// TODO this is duplicative of other Go type jennies. Remove it in favor of a better-abstracted version in thema itself
|
||||
@ -30,22 +27,22 @@ func (j *pgoJenny) JennyName() string {
|
||||
}
|
||||
|
||||
func (j *pgoJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
|
||||
b := decl.PluginMeta.Backend
|
||||
if b == nil || !*b || !decl.HasSchema() {
|
||||
hasBackend := decl.PluginMeta.Backend
|
||||
// We skip elasticsearch since we have problems with the generated file.
|
||||
// This is temporal until we migrate to the new system.
|
||||
if hasBackend == nil || !*hasBackend || decl.PluginMeta.Id == "elasticsearch" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
slotname := strings.ToLower(decl.SchemaInterface.Name)
|
||||
byt, err := gocode.GenerateTypesOpenAPI(decl.Lineage.Latest(), &gocode.TypeConfigOpenAPI{
|
||||
Config: &openapi.Config{
|
||||
Group: decl.SchemaInterface.IsGroup,
|
||||
byt, err := generators.GenerateTypesGo(decl.CueFile, &generators.GoConfig{
|
||||
Config: &generators.OpenApiConfig{
|
||||
Config: &copenapi.Config{
|
||||
MaxCycleDepth: 10,
|
||||
},
|
||||
SplitSchema: true,
|
||||
IsGroup: decl.SchemaInterface.IsGroup,
|
||||
},
|
||||
PackageName: slotname,
|
||||
ApplyFuncs: []dstutil.ApplyFunc{corecodegen.PrefixDropper(decl.Lineage.Name())},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
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"
|
||||
)
|
||||
|
||||
@ -34,15 +33,11 @@ func (j *ptsJenny) JennyName() string {
|
||||
}
|
||||
|
||||
func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (codejen.Files, error) {
|
||||
if !decl.HasSchema() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
genFile := &tsast.File{}
|
||||
versionedFile := &tsast.File{}
|
||||
|
||||
for _, im := range decl.Imports {
|
||||
if tsim, err := cuectx.ConvertImport(im); err != nil {
|
||||
if tsim, err := codegen.ConvertImport(im); err != nil {
|
||||
return nil, err
|
||||
} else if tsim.From.Value != "" {
|
||||
genFile.Imports = append(genFile.Imports, tsim)
|
||||
@ -94,18 +89,46 @@ func getPluginVersion(pluginVersion *string) string {
|
||||
|
||||
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(),
|
||||
Name: derivePascalName(pd.PluginMeta.Id, pd.PluginMeta.Name) + pd.SchemaInterface.Name,
|
||||
CueFile: pd.CueFile,
|
||||
IsGroup: pd.SchemaInterface.IsGroup,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func derivePascalName(id string, name string) string {
|
||||
sani := func(s string) string {
|
||||
ret := strings.Title(strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z':
|
||||
return r
|
||||
case r >= 'A' && r <= 'Z':
|
||||
return r
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}, strings.Title(strings.Map(func(r rune) rune {
|
||||
switch r {
|
||||
case '-', '_':
|
||||
return ' '
|
||||
default:
|
||||
return r
|
||||
}
|
||||
}, s))))
|
||||
if len(ret) > 63 {
|
||||
return ret[:63]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
fromname := sani(name)
|
||||
if len(fromname) != 0 {
|
||||
return fromname
|
||||
}
|
||||
return sani(strings.Split(id, "-")[1])
|
||||
}
|
||||
|
||||
func getGrafanaVersion() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user