2022-11-22 08:00:29 -06:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/grafana/codejen"
|
2023-03-15 11:04:28 -05:00
|
|
|
"github.com/grafana/kindsys"
|
2022-11-22 08:00:29 -06:00
|
|
|
)
|
|
|
|
|
2023-04-27 15:32:38 -05:00
|
|
|
// LatestMajorsOrXJenny returns a jenny that repeats the input for the latest in each major version.
|
|
|
|
//
|
|
|
|
// TODO remove forceGroup option, it's a temporary hack to accommodate core kinds
|
|
|
|
func LatestMajorsOrXJenny(parentdir string, forceGroup bool, inner codejen.OneToOne[SchemaForGen]) OneToMany {
|
2022-11-22 08:00:29 -06:00
|
|
|
if inner == nil {
|
|
|
|
panic("inner jenny must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lmox{
|
2023-04-27 15:32:38 -05:00
|
|
|
parentdir: parentdir,
|
|
|
|
inner: inner,
|
|
|
|
forceGroup: forceGroup,
|
2022-11-22 08:00:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lmox struct {
|
2023-04-27 15:32:38 -05:00
|
|
|
parentdir string
|
|
|
|
inner codejen.OneToOne[SchemaForGen]
|
|
|
|
forceGroup bool
|
2022-11-22 08:00:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *lmox) JennyName() string {
|
|
|
|
return "LatestMajorsOrXJenny"
|
|
|
|
}
|
|
|
|
|
2023-01-31 18:40:15 -06:00
|
|
|
func (j *lmox) Generate(kind kindsys.Kind) (codejen.Files, error) {
|
2023-05-30 07:56:18 -05:00
|
|
|
// TODO remove this once codejen catches nils https://github.com/grafana/codejen/issues/5
|
|
|
|
if kind == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 18:40:15 -06:00
|
|
|
comm := kind.Props().Common()
|
2022-11-22 08:00:29 -06:00
|
|
|
sfg := SchemaForGen{
|
|
|
|
Name: comm.Name,
|
|
|
|
IsGroup: comm.LineageIsGroup,
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:32:38 -05:00
|
|
|
if j.forceGroup {
|
|
|
|
sfg.IsGroup = true
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:00:29 -06:00
|
|
|
do := func(sfg SchemaForGen, infix string) (codejen.Files, error) {
|
|
|
|
f, err := j.inner.Generate(sfg)
|
|
|
|
if err != nil {
|
2023-01-31 18:40:15 -06:00
|
|
|
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), kind.Props().Common().Name, err)
|
2022-11-22 08:00:29 -06:00
|
|
|
}
|
|
|
|
if f == nil || !f.Exists() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.RelativePath = filepath.Join(j.parentdir, comm.MachineName, infix, f.RelativePath)
|
|
|
|
f.From = append(f.From, j)
|
|
|
|
return codejen.Files{*f}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if comm.Maturity.Less(kindsys.MaturityStable) {
|
2023-01-31 18:40:15 -06:00
|
|
|
sfg.Schema = kind.Lineage().Latest()
|
2022-11-22 08:00:29 -06:00
|
|
|
return do(sfg, "x")
|
|
|
|
}
|
|
|
|
|
|
|
|
var fl codejen.Files
|
2023-03-09 11:24:03 -06:00
|
|
|
major := -1
|
2023-01-31 18:40:15 -06:00
|
|
|
for sch := kind.Lineage().First(); sch != nil; sch = sch.Successor() {
|
2023-03-09 11:24:03 -06:00
|
|
|
if int(sch.Version()[0]) == major {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
major = int(sch.Version()[0])
|
|
|
|
|
2022-11-22 08:00:29 -06:00
|
|
|
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
|
|
|
|
}
|