2022-11-22 22:00:29 +08:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/grafana/codejen"
|
2023-03-16 00:04:28 +08:00
|
|
|
"github.com/grafana/kindsys"
|
2022-11-22 22:00:29 +08:00
|
|
|
)
|
|
|
|
|
2023-04-28 04:32:38 +08: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
|
2024-03-11 19:51:44 +08:00
|
|
|
func LatestMajorsOrXJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) OneToMany {
|
2022-11-22 22:00:29 +08:00
|
|
|
if inner == nil {
|
|
|
|
panic("inner jenny must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lmox{
|
2024-03-11 19:51:44 +08:00
|
|
|
parentdir: parentdir,
|
|
|
|
inner: inner,
|
2022-11-22 22:00:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lmox struct {
|
2024-03-11 19:51:44 +08:00
|
|
|
parentdir string
|
|
|
|
inner codejen.OneToOne[SchemaForGen]
|
2022-11-22 22:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *lmox) JennyName() string {
|
|
|
|
return "LatestMajorsOrXJenny"
|
|
|
|
}
|
|
|
|
|
2023-02-01 08:40:15 +08:00
|
|
|
func (j *lmox) Generate(kind kindsys.Kind) (codejen.Files, error) {
|
2023-05-30 20:56:18 +08:00
|
|
|
// TODO remove this once codejen catches nils https://github.com/grafana/codejen/issues/5
|
|
|
|
if kind == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 08:40:15 +08:00
|
|
|
comm := kind.Props().Common()
|
2022-11-22 22:00:29 +08:00
|
|
|
sfg := SchemaForGen{
|
|
|
|
Name: comm.Name,
|
2024-03-11 19:51:44 +08:00
|
|
|
IsGroup: true,
|
|
|
|
Schema: kind.Lineage().Latest(),
|
2022-11-22 22:00:29 +08:00
|
|
|
}
|
|
|
|
|
2024-03-11 19:51:44 +08:00
|
|
|
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(), kind.Props().Common().Name, err)
|
2023-04-28 04:32:38 +08:00
|
|
|
}
|
2024-03-11 19:51:44 +08:00
|
|
|
if f == nil || !f.Exists() {
|
|
|
|
return nil, nil
|
2022-11-22 22:00:29 +08:00
|
|
|
}
|
|
|
|
|
2024-03-11 19:51:44 +08:00
|
|
|
f.RelativePath = filepath.Join(j.parentdir, comm.MachineName, "x", f.RelativePath)
|
|
|
|
f.From = append(f.From, j)
|
|
|
|
return codejen.Files{*f}, nil
|
2022-11-22 22:00:29 +08:00
|
|
|
}
|