plugindef: Move pluginmeta out of coremodels as standalone thema lineage (#56765)

* Get pluginmeta mostly moved over to pkg/plugins/plugindef

* Remove dead func

* Fix up pfs, use sync.Once in plugindef

* Update to latest thema

* Chase Endec->Codec conversion in Thema

* Comments on slash header gen; use ToSlash

* Also generate JSON schema for plugindef

* Generate JSON Schema as well

* Fix slot loading from kindsys cue decls

* Remove unused vars

* skip generating plugin.schema.json for now

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
sam boyer
2022-11-15 08:48:31 -05:00
committed by GitHub
parent ff1afbb699
commit 78f0340031
29 changed files with 458 additions and 451 deletions

View File

@ -237,7 +237,7 @@ type tplVars struct {
}
func (cd *CoremodelDeclaration) GenerateTypescriptCoremodel() (*tsast.File, error) {
schv := thema.SchemaP(cd.Lineage, thema.LatestVersion(cd.Lineage)).UnwrapCUE()
schv := cd.Lineage.Latest().Underlying()
tf, err := cuetsy.GenerateAST(schv, cuetsy.Config{
Export: true,

View File

@ -3,6 +3,7 @@ package codegen
import (
"bytes"
"fmt"
"path/filepath"
"github.com/grafana/codejen"
"github.com/grafana/grafana/pkg/kindsys"
@ -39,12 +40,22 @@ func (decl *DeclForGen) Lineage() thema.Lineage {
return decl.lin
}
// SlashHeaderMapper produces a FileMapper that injects a comment header onto
// a [codejen.File] indicating the main generator that produced it (via the provided
// maingen, which should be a path) and the jenny or jennies that constructed the
// file.
func SlashHeaderMapper(maingen string) codejen.FileMapper {
return func(f codejen.File) (codejen.File, error) {
b := new(bytes.Buffer)
fmt.Fprintf(b, headerTmpl, maingen, f.FromString())
fmt.Fprint(b, string(f.Data))
f.Data = b.Bytes()
// Never inject on certain filetypes, it's never valid
switch filepath.Ext(f.RelativePath) {
case ".json", ".yml", ".yaml":
return f, nil
default:
b := new(bytes.Buffer)
fmt.Fprintf(b, headerTmpl, filepath.ToSlash(maingen), f.FromString())
fmt.Fprint(b, string(f.Data))
f.Data = b.Bytes()
}
return f, nil
}
}

View File

@ -66,11 +66,10 @@ func (gen *genTSVeneerIndex) Generate(decls []*DeclForGen) (*codejen.File, error
func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(decl *DeclForGen, tf *ast.File) ([]ast.Decl, error) {
lin := decl.Lineage()
sch := thema.SchemaP(lin, thema.LatestVersion(lin))
comm := decl.Meta.Common()
// Check the root, then walk the tree
rootv := sch.UnwrapCUE()
rootv := lin.Latest().Underlying()
var raw, custom, rawD, customD ast.Idents

View File

@ -15,7 +15,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/grafana/cuetsy"
tsast "github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/grafana/pkg/framework/coremodel"
"github.com/grafana/grafana/pkg/kindsys"
"github.com/grafana/grafana/pkg/plugins/pfs"
"github.com/grafana/thema"
"github.com/grafana/thema/encoding/openapi"
@ -146,7 +146,7 @@ func (pt *PluginTree) GenerateTypeScriptAST() (*tsast.File, error) {
// whether the slot is a grouped lineage:
// https://github.com/grafana/thema/issues/62
if isGroupLineage(slotname) {
tsf, err := cuetsy.GenerateAST(sch.UnwrapCUE(), cuetsy.Config{
tsf, err := cuetsy.GenerateAST(sch.Underlying(), cuetsy.Config{
Export: true,
})
if err != nil {
@ -154,7 +154,7 @@ func (pt *PluginTree) GenerateTypeScriptAST() (*tsast.File, error) {
}
f.Nodes = append(f.Nodes, tsf.Nodes...)
} else {
pair, err := cuetsy.GenerateSingleAST(strings.Title(lin.Name()), sch.UnwrapCUE(), cuetsy.TypeInterface)
pair, err := cuetsy.GenerateSingleAST(strings.Title(lin.Name()), sch.Underlying(), cuetsy.TypeInterface)
if err != nil {
return nil, fmt.Errorf("error translating %s lineage to TypeScript: %w", slotname, err)
}
@ -169,7 +169,7 @@ func (pt *PluginTree) GenerateTypeScriptAST() (*tsast.File, error) {
}
func isGroupLineage(slotname string) bool {
sl, has := coremodel.AllSlots()[slotname]
sl, has := kindsys.AllSlots(nil)[slotname]
if !has {
panic("unknown slotname name: " + slotname)
}

View File

@ -15,7 +15,7 @@ const rootrel string = "kinds/structured/{{ .Meta.MachineName }}"
// TODO standard generated docs
type Kind struct {
lin thema.ConvergentLineage[*{{ .Meta.Name }}]
jendec vmux.Endec
jcodec vmux.Codec
valmux vmux.ValueMux[*{{ .Meta.Name }}]
decl kindsys.Decl[kindsys.CoreStructuredMeta]
}
@ -47,9 +47,9 @@ func NewKind(rt *thema.Runtime, opts ...thema.BindOption) (*Kind, error) {
return nil, err
}
k.jendec = vmux.NewJSONEndec("{{ .Meta.MachineName }}.json")
k.jcodec = vmux.NewJSONCodec("{{ .Meta.MachineName }}.json")
k.lin = tsch.ConvergentLineage()
k.valmux = vmux.NewValueMux(k.lin.TypedSchema(), k.jendec)
k.valmux = vmux.NewValueMux(k.lin.TypedSchema(), k.jcodec)
return k, nil
}

View File

@ -69,8 +69,8 @@ func postprocessGoFile(cfg genGoFile) ([]byte, error) {
}
type prefixmod struct {
str string
base string
prefix string
replace string
rxp *regexp.Regexp
rxpsuff *regexp.Regexp
}
@ -80,7 +80,22 @@ type prefixmod struct {
// comments in a generated Go file.
func PrefixDropper(prefix string) astutil.ApplyFunc {
return (&prefixmod{
str: prefix,
prefix: prefix,
rxpsuff: regexp.MustCompile(fmt.Sprintf(`%s([a-zA-Z_]+)`, prefix)),
rxp: regexp.MustCompile(fmt.Sprintf(`%s([\s.,;-])`, prefix)),
}).applyfunc
}
// PrefixReplacer returns an astutil.ApplyFunc that removes the provided prefix
// string when it appears as a leading sequence in type names, var names, and
// comments in a generated Go file.
//
// When an exact match for prefix is found, the provided replace string
// is substituted.
func PrefixReplacer(prefix, replace string) astutil.ApplyFunc {
return (&prefixmod{
prefix: prefix,
replace: replace,
rxpsuff: regexp.MustCompile(fmt.Sprintf(`%s([a-zA-Z_]+)`, prefix)),
rxp: regexp.MustCompile(fmt.Sprintf(`%s([\s.,;-])`, prefix)),
}).applyfunc
@ -113,8 +128,8 @@ func (d prefixmod) applyfunc(c *astutil.Cursor) bool {
case *ast.CommentGroup:
for _, c := range x.List {
c.Text = d.rxpsuff.ReplaceAllString(c.Text, "$1")
if d.base != "" {
c.Text = d.rxp.ReplaceAllString(c.Text, d.base+"$1")
if d.replace != "" {
c.Text = d.rxp.ReplaceAllString(c.Text, d.replace+"$1")
}
}
}
@ -142,9 +157,9 @@ func (d prefixmod) handleExpr(e ast.Expr) {
}
func (d prefixmod) do(n *ast.Ident) {
if n.Name != d.str {
n.Name = strings.TrimPrefix(n.Name, d.str)
} else if d.base != "" {
n.Name = d.base
if n.Name != d.prefix {
n.Name = strings.TrimPrefix(n.Name, d.prefix)
} else if d.replace != "" {
n.Name = d.replace
}
}