2022-11-15 07:48:31 -06:00
|
|
|
//go:build ignore
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"cuelang.org/go/cue/cuecontext"
|
2023-01-17 04:58:08 -06:00
|
|
|
"github.com/dave/dst"
|
2022-11-15 07:48:31 -06:00
|
|
|
"github.com/grafana/codejen"
|
|
|
|
"github.com/grafana/grafana/pkg/codegen"
|
|
|
|
"github.com/grafana/grafana/pkg/cuectx"
|
|
|
|
"github.com/grafana/thema"
|
|
|
|
"github.com/grafana/thema/encoding/gocode"
|
|
|
|
"github.com/grafana/thema/encoding/jsonschema"
|
|
|
|
)
|
|
|
|
|
|
|
|
var dirPlugindef = filepath.Join("pkg", "plugins", "plugindef")
|
|
|
|
|
|
|
|
// main generator for plugindef. plugindef isn't a kind, so it has its own
|
|
|
|
// one-off main generator.
|
|
|
|
func main() {
|
|
|
|
v := elsedie(cuectx.BuildGrafanaInstance(nil, dirPlugindef, "", nil))("could not load plugindef cue package")
|
|
|
|
|
|
|
|
lin := elsedie(thema.BindLineage(v, cuectx.GrafanaThemaRuntime()))("plugindef lineage is invalid")
|
|
|
|
|
|
|
|
jl := &codejen.JennyList[thema.Lineage]{}
|
|
|
|
jl.AppendOneToOne(&jennytypego{}, &jennybindgo{})
|
|
|
|
jl.AddPostprocessors(codegen.SlashHeaderMapper(filepath.Join(dirPlugindef, "gen.go")))
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "could not get working directory: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-02-02 14:06:55 -06:00
|
|
|
|
|
|
|
groot := filepath.Clean(filepath.Join(cwd, "../../.."))
|
2022-11-15 07:48:31 -06:00
|
|
|
|
2022-11-16 14:08:01 -06:00
|
|
|
jfs := elsedie(jl.GenerateFS(lin))("plugindef jenny pipeline failed")
|
2022-11-15 07:48:31 -06:00
|
|
|
if _, set := os.LookupEnv("CODEGEN_VERIFY"); set {
|
|
|
|
if err := jfs.Verify(context.Background(), groot); err != nil {
|
|
|
|
die(fmt.Errorf("generated code is out of sync with inputs:\n%s\nrun `make gen-cue` to regenerate", err))
|
|
|
|
}
|
|
|
|
} else if err := jfs.Write(context.Background(), groot); err != nil {
|
|
|
|
die(fmt.Errorf("error while writing generated code to disk:\n%s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// one-off jenny for plugindef go types
|
|
|
|
type jennytypego struct{}
|
|
|
|
|
|
|
|
func (j *jennytypego) JennyName() string {
|
|
|
|
return "PluginGoTypes"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jennytypego) Generate(lin thema.Lineage) (*codejen.File, error) {
|
2023-01-25 14:29:10 -06:00
|
|
|
f, err := codegen.GoTypesJenny{}.Generate(codegen.SchemaForGen{
|
2022-12-22 06:20:02 -06:00
|
|
|
Name: "PluginDef",
|
|
|
|
Schema: lin.Latest(),
|
|
|
|
IsGroup: false,
|
2022-11-15 07:48:31 -06:00
|
|
|
})
|
2022-12-22 06:20:02 -06:00
|
|
|
if f != nil {
|
|
|
|
f.RelativePath = filepath.Join(dirPlugindef, f.RelativePath)
|
2022-11-15 07:48:31 -06:00
|
|
|
}
|
2022-12-22 06:20:02 -06:00
|
|
|
return f, err
|
2022-11-15 07:48:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// one-off jenny for plugindef go bindings
|
|
|
|
type jennybindgo struct{}
|
|
|
|
|
|
|
|
func (j *jennybindgo) JennyName() string {
|
|
|
|
return "PluginGoBindings"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jennybindgo) Generate(lin thema.Lineage) (*codejen.File, error) {
|
|
|
|
b, err := gocode.GenerateLineageBinding(lin, &gocode.BindingConfig{
|
|
|
|
TitleName: "PluginDef",
|
2023-01-17 04:58:08 -06:00
|
|
|
Assignee: dst.NewIdent("*PluginDef"),
|
2022-11-15 07:48:31 -06:00
|
|
|
PrivateFactory: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return codejen.NewFile(filepath.Join(dirPlugindef, "plugindef_bindings_gen.go"), b, j), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// one-off jenny for plugindef json schema generator
|
|
|
|
type jennyjschema struct{}
|
|
|
|
|
|
|
|
func (j *jennyjschema) JennyName() string {
|
|
|
|
return "PluginJSONSchema"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jennyjschema) Generate(lin thema.Lineage) (*codejen.File, error) {
|
|
|
|
f, err := jsonschema.GenerateSchema(lin.Latest())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, _ := cuecontext.New().BuildFile(f).MarshalJSON()
|
|
|
|
nb := new(bytes.Buffer)
|
|
|
|
die(json.Indent(nb, b, "", " "))
|
|
|
|
return codejen.NewFile(filepath.FromSlash("docs/sources/developers/plugins/plugin.schema.json"), nb.Bytes(), j), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func elsedie[T any](t T, err error) func(msg string) T {
|
|
|
|
if err != nil {
|
|
|
|
return func(msg string) T {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err)
|
|
|
|
os.Exit(1)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(msg string) T {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func die(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err, "\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|