/
jenny_eachmajorcomposable.go
80 lines (68 loc) · 2.15 KB
/
jenny_eachmajorcomposable.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
74
75
76
77
78
79
80
package codegen
import (
"fmt"
"path/filepath"
"strings"
"github.com/grafana/codejen"
"github.com/grafana/kindsys"
)
// TODO remove this once there's a standard jenny for this...somewhere in core
func ComposableLatestMajorsOrXJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) codejen.OneToMany[kindsys.Composable] {
if inner == nil {
panic("inner jenny must not be nil")
}
return &clmox{
parentdir: parentdir,
inner: inner,
}
}
type clmox struct {
parentdir string
inner codejen.OneToOne[SchemaForGen]
}
func (j *clmox) JennyName() string {
return "ComposableLatestMajorsOrXJenny"
}
func (j *clmox) Generate(k kindsys.Composable) (codejen.Files, error) {
si, err := kindsys.FindSchemaInterface(k.Def().Properties.SchemaInterface)
if err != nil {
return nil, err
}
sfg := SchemaForGen{
Name: k.Lineage().Name(),
IsGroup: si.IsGroup(),
}
// TODO adapt this once we figure out consistent naming
// nam := fmt.Sprintf("%s-%s", strings.ToLower(decl.Info.Meta().Id), strings.ToLower(sfg.Name))
nam := strings.ToLower(sfg.Name)
do := func(sfg SchemaForGen, infix string) (codejen.Files, error) {
f, err := j.inner.Generate(sfg)
if err != nil {
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), nam, err)
}
if f == nil || !f.Exists() {
return nil, nil
}
f.RelativePath = filepath.Join(j.parentdir, strings.ToLower(strings.TrimSuffix(sfg.Name, si.Name())), strings.ToLower(si.Name()), infix, strings.ToLower(f.RelativePath))
f.From = append(f.From, j)
return codejen.Files{*f}, nil
}
// TODO uncomment this latter half once plugins are fully converted to new system
// if comm.Maturity.Less(kindsys.MaturityStable) {
sfg.Schema = k.Lineage().Latest()
return do(sfg, "x")
// }
// var fl codejen.Files
// for sch := decl.Lineage.First(); sch != nil; sch = sch.Successor() {
// sfg.Schema = sch.LatestInMajor()
// files, err := do(sfg, fmt.Sprintf("v%v", sch.Version()[0]))
// if err != nil {
// return nil, err
// }
// fl = append(fl, files...)
// }
// if fl.Validate() != nil {
// return nil, fl.Validate()
// }
// return fl, nil
}