2024-03-13 11:05:21 -05:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/codejen"
|
2024-03-21 05:11:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/pfs"
|
2024-03-13 11:05:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var registryPath = filepath.Join("pkg", "registry", "schemas")
|
|
|
|
|
|
|
|
var renamedPlugins = map[string]string{
|
|
|
|
"cloud-monitoring": "googlecloudmonitoring",
|
|
|
|
"grafana-pyroscope-datasource": "grafanapyroscope",
|
|
|
|
"annolist": "annotationslist",
|
|
|
|
"grafanatestdatadatasource": "testdata",
|
|
|
|
"dashlist": "dashboardlist",
|
|
|
|
}
|
|
|
|
|
|
|
|
type PluginRegistryJenny struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jenny *PluginRegistryJenny) JennyName() string {
|
|
|
|
return "PluginRegistryJenny"
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:11:29 -05:00
|
|
|
func (jenny *PluginRegistryJenny) Generate(decls ...*pfs.PluginDecl) (*codejen.File, error) {
|
|
|
|
if len(decls) == 0 {
|
2024-03-13 11:05:21 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2024-03-21 05:11:29 -05:00
|
|
|
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)
|
2024-03-13 11:05:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to find schema name: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
schemas[i] = Schema{
|
|
|
|
Name: name,
|
2024-03-21 05:11:29 -05:00
|
|
|
Filename: variant,
|
|
|
|
FilePath: "./" + filepath.Join("public", "app", "plugins", decl.PluginPath, variant),
|
2024-03-13 11:05:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := tmpls.Lookup("composable_registry.tmpl").Execute(buf, tmpl_vars_plugin_registry{
|
|
|
|
Schemas: schemas,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed executing kind registry template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := format.Source(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return codejen.NewFile(filepath.Join(registryPath, "composable_kind.go"), b, jenny), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSchemaName(path string) (string, error) {
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return "", fmt.Errorf("path should contain more than 2 elements")
|
|
|
|
}
|
2024-03-21 05:11:29 -05:00
|
|
|
folderName := parts[len(parts)-1]
|
2024-03-13 11:05:21 -05:00
|
|
|
if renamed, ok := renamedPlugins[folderName]; ok {
|
|
|
|
folderName = renamed
|
|
|
|
}
|
|
|
|
folderName = strings.ReplaceAll(folderName, "-", "")
|
|
|
|
return strings.ToLower(folderName), nil
|
|
|
|
}
|