Files
grafana/pkg/codegen/tmpl/plugin_lineage_file.tmpl
sam boyer ced53a8dc2 plugins: Introduce generated, static core plugin registry (#54118)
* Refactor towards template/codegen framework

* Add templates for plugin gen

* Add Go codegen for plugins; overhaul framework, too

* Add new codegen output; assorted framework fixes

* Regenerate after merge

* Remove accidental commit file, update templates

* Export the pfs.Tree loader from plugin types

* Print details from cuetsy errors

* Generate loaders for all plugins and list in registry

* Use pfs_gen.go over lineage_gen.go

* Un-un-ignore main file

* Introduce simple List static registry for plugins

* Last tweaks to codegen

* remove unused tvars

* Ensure loop-local instances for both vars

* Generate pfs parsing in-place in registry

* Stop generating pfs_gen.go

* Move Tree into pfs, rename subdir

* Change package name to match dir

* Ignore gocyclo on HTTPServer.getNavTree
2022-09-14 10:15:09 -04:00

59 lines
1.8 KiB
Cheetah

{{ template "autogen_header.tmpl" .Header -}}
package {{ .PackageName }}
import (
"embed"
"fmt"
"path/filepath"
"sync"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/plugins/pfs"
"github.com/grafana/thema"
)
var parseOnce sync.Once
var ptree *pfs.Tree
//go:embed plugin.{{ if .RootCUE }}cue{{ else }}json{{ end }}{{ if .HasModels }} models.cue{{ end }}
var plugFS embed.FS
// PluginTree returns the plugin tree representing the statically analyzable contents of the {{ .PluginID }} plugin.
func PluginTree(lib *thema.Library) *pfs.Tree {
var err error
if lib == nil {
parseOnce.Do(func() {
ptree, err = pfs.ParsePluginFS(plugFS, cuectx.ProvideThemaLibrary())
})
} else {
ptree, err = pfs.ParsePluginFS(plugFS, cuectx.ProvideThemaLibrary())
}
if err != nil {
// Even the most rudimentary testing in CI ensures this is unreachable
panic(fmt.Errorf("error parsing plugin fs tree: %w", err))
}
return ptree
}
{{ $pluginfo := . }}{{ range $slot := .SlotImpls }}
// {{ .SlotName }}Lineage returns the Thema lineage for the {{ $pluginfo.PluginID }} {{ $pluginfo.PluginType }} plugin's
// {{ .SlotName }} ["github.com/grafana/grafana/pkg/framework/coremodel".Slot] implementation.
func {{ .SlotName }}Lineage(lib *thema.Library, opts ...thema.BindOption) (thema.Lineage, error) {
t := PluginTree(lib)
lin, has := t.RootPlugin().SlotImplementations()["{{ .SlotName }}"]
if !has {
panic("unreachable: lineage for {{ .SlotName }} does not exist, but code is only generated for existing lineages")
}
return lin, nil
}
// The current schema version of the {{ .SlotName }} slot implementation.
//
// Code generation ensures that this is always the version number for the latest schema
// in the {{ .SlotName }} Thema lineage.
var currentVersion{{ .SlotName }} = thema.SV({{ .LatestMajv }}, {{ .LatestMinv }})
{{ end }}