forked from grafana/grafana
/
decl_parser.go
73 lines (62 loc) · 1.68 KB
/
decl_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package pfs
import (
"fmt"
"io/fs"
"path/filepath"
"sort"
"github.com/grafana/kindsys"
"github.com/grafana/thema"
)
type declParser struct {
rt *thema.Runtime
skip map[string]bool
}
func NewDeclParser(rt *thema.Runtime, skip map[string]bool) *declParser {
return &declParser{
rt: rt,
skip: skip,
}
}
// TODO convert this to be the new parser for Tree
func (psr *declParser) Parse(root fs.FS) ([]*PluginDecl, error) {
// TODO remove hardcoded tree structure assumption, work from root of provided fs
plugins, err := fs.Glob(root, "**/**/plugin.json")
if err != nil {
return nil, fmt.Errorf("error finding plugin dirs: %w", err)
}
decls := make([]*PluginDecl, 0)
for _, plugin := range plugins {
path := filepath.ToSlash(filepath.Dir(plugin))
base := filepath.Base(path)
if skip, ok := psr.skip[base]; ok && skip {
continue
}
dir, _ := fs.Sub(root, path)
pp, err := ParsePluginFS(dir, psr.rt)
if err != nil {
return nil, fmt.Errorf("parsing plugin failed for %s: %s", dir, err)
}
if len(pp.ComposableKinds) == 0 {
decls = append(decls, EmptyPluginDecl(path, pp.Properties))
continue
}
for slotName, kind := range pp.ComposableKinds {
slot, err := kindsys.FindSchemaInterface(slotName)
if err != nil {
return nil, fmt.Errorf("parsing plugin failed for %s: %s", dir, err)
}
decls = append(decls, &PluginDecl{
SchemaInterface: &slot,
Lineage: kind.Lineage(),
Imports: pp.CUEImports,
PluginMeta: pp.Properties,
PluginPath: path,
KindDecl: kind.Def(),
})
}
}
sort.Slice(decls, func(i, j int) bool {
return decls[i].PluginPath < decls[j].PluginPath
})
return decls, nil
}