grafana/pkg/codegen/jenny_core_registry.go
Selene 473898e47c
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
2024-03-21 11:11:29 +01:00

57 lines
1.2 KiB
Go

package codegen
import (
"bytes"
"fmt"
"go/format"
"path/filepath"
"strings"
"github.com/grafana/codejen"
)
var registryPath = filepath.Join("pkg", "registry", "schemas")
// CoreRegistryJenny generates a registry with all core kinds.
type CoreRegistryJenny struct {
}
func (jenny *CoreRegistryJenny) JennyName() string {
return "CoreRegistryJenny"
}
func (jenny *CoreRegistryJenny) Generate(cueFiles ...SchemaForGen) (codejen.Files, error) {
schemas := make([]Schema, len(cueFiles))
for i, v := range cueFiles {
name, err := getSchemaName(v.Name)
if err != nil {
return nil, err
}
schemas[i] = Schema{
Name: name,
FilePath: v.FilePath,
}
}
buf := new(bytes.Buffer)
if err := tmpls.Lookup("core_registry.tmpl").Execute(buf, tvars_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
}
file := codejen.NewFile(filepath.Join(registryPath, "core_kind.go"), b, jenny)
return codejen.Files{*file}, nil
}
func getSchemaName(pkg string) (string, error) {
pkg = strings.Replace(pkg, "-", "_", -1)
return strings.ToLower(pkg), nil
}