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
|
|
|
)
|
|
|
|
|
|
|
|
// LatestJenny returns a jenny that runs another jenny for only the latest
|
2023-01-31 03:50:08 -06:00
|
|
|
// schema in a DefForGen, and prefixes the resulting file with the provided
|
2022-11-22 08:00:29 -06:00
|
|
|
// parentdir (e.g. "pkg/kinds/") and with a directory based on the kind's
|
|
|
|
// machine name (e.g. "dashboard/").
|
|
|
|
func LatestJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) OneToOne {
|
|
|
|
if inner == nil {
|
|
|
|
panic("inner jenny must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &latestj{
|
|
|
|
parentdir: parentdir,
|
|
|
|
inner: inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type latestj struct {
|
|
|
|
parentdir string
|
|
|
|
inner codejen.OneToOne[SchemaForGen]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *latestj) JennyName() string {
|
|
|
|
return "LatestJenny"
|
|
|
|
}
|
|
|
|
|
2023-01-31 18:40:15 -06:00
|
|
|
func (j *latestj) Generate(kind kindsys.Kind) (*codejen.File, error) {
|
|
|
|
comm := kind.Props().Common()
|
2022-11-22 08:00:29 -06:00
|
|
|
sfg := SchemaForGen{
|
|
|
|
Name: comm.Name,
|
2023-01-31 18:40:15 -06:00
|
|
|
Schema: kind.Lineage().Latest(),
|
2022-11-22 08:00:29 -06:00
|
|
|
IsGroup: comm.LineageIsGroup,
|
|
|
|
}
|
|
|
|
|
|
|
|
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, f.RelativePath)
|
|
|
|
f.From = append(f.From, j)
|
|
|
|
return f, nil
|
|
|
|
}
|