2022-12-02 01:22:28 -06:00
|
|
|
package pfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
|
2023-03-15 11:04:28 -05:00
|
|
|
"github.com/grafana/kindsys"
|
2022-12-02 01:22:28 -06:00
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 03:41:35 -06:00
|
|
|
// TODO convert this to be the new parser for Tree
|
2022-12-02 01:22:28 -06:00
|
|
|
func (psr *declParser) Parse(root fs.FS) ([]*PluginDecl, error) {
|
2023-01-20 03:41:35 -06:00
|
|
|
// TODO remove hardcoded tree structure assumption, work from root of provided fs
|
2022-12-02 01:22:28 -06:00
|
|
|
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 {
|
2023-02-02 14:06:55 -06:00
|
|
|
path := filepath.ToSlash(filepath.Dir(plugin))
|
2022-12-02 01:22:28 -06:00
|
|
|
base := filepath.Base(path)
|
|
|
|
if skip, ok := psr.skip[base]; ok && skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-24 03:47:25 -05:00
|
|
|
dir, _ := fs.Sub(root, path)
|
2023-01-20 03:41:35 -06:00
|
|
|
pp, err := ParsePluginFS(dir, psr.rt)
|
2022-12-02 01:22:28 -06:00
|
|
|
if err != nil {
|
2023-01-20 03:41:35 -06:00
|
|
|
return nil, fmt.Errorf("parsing plugin failed for %s: %s", dir, err)
|
2022-12-02 01:22:28 -06:00
|
|
|
}
|
|
|
|
|
2023-01-20 03:41:35 -06:00
|
|
|
if len(pp.ComposableKinds) == 0 {
|
|
|
|
decls = append(decls, EmptyPluginDecl(path, pp.Properties))
|
2022-12-02 01:22:28 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-20 03:41:35 -06:00
|
|
|
for slotName, kind := range pp.ComposableKinds {
|
2023-01-06 11:37:32 -06:00
|
|
|
slot, err := kindsys.FindSchemaInterface(slotName)
|
2022-12-02 01:22:28 -06:00
|
|
|
if err != nil {
|
2023-01-20 03:41:35 -06:00
|
|
|
return nil, fmt.Errorf("parsing plugin failed for %s: %s", dir, err)
|
2022-12-02 01:22:28 -06:00
|
|
|
}
|
|
|
|
decls = append(decls, &PluginDecl{
|
2023-01-06 11:37:32 -06:00
|
|
|
SchemaInterface: &slot,
|
2023-01-20 03:41:35 -06:00
|
|
|
Lineage: kind.Lineage(),
|
|
|
|
Imports: pp.CUEImports,
|
|
|
|
PluginMeta: pp.Properties,
|
2023-01-06 11:37:32 -06:00
|
|
|
PluginPath: path,
|
2023-01-31 03:50:08 -06:00
|
|
|
KindDecl: kind.Def(),
|
2022-12-02 01:22:28 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(decls, func(i, j int) bool {
|
|
|
|
return decls[i].PluginPath < decls[j].PluginPath
|
|
|
|
})
|
|
|
|
|
|
|
|
return decls, nil
|
|
|
|
}
|