codegen: Refactor core jennies for reusability, add version-picking metajennies (#58995)
* Add each-major jenny, refactor TS jenny for it * Introduce LatestJenny, refactor GoTypesJenny for it
This commit is contained in:
+29
-19
@@ -57,7 +57,6 @@ func StructuredForGen(k kindsys.Structured) *DeclForGen {
|
||||
type DeclForGen struct {
|
||||
*kindsys.SomeDecl
|
||||
lin thema.Lineage
|
||||
sch thema.Lineage
|
||||
}
|
||||
|
||||
// Lineage returns the [thema.Lineage] for the underlying [kindsys.SomeDecl].
|
||||
@@ -65,10 +64,15 @@ func (decl *DeclForGen) Lineage() thema.Lineage {
|
||||
return decl.lin
|
||||
}
|
||||
|
||||
// Schema returns the [thema.Schema] that a jenny should operate against, for those
|
||||
// jennies that target a single schema.
|
||||
func (decl *DeclForGen) Schema() thema.Lineage {
|
||||
return decl.sch
|
||||
// ForLatestSchema returns a [SchemaForGen] for the latest schema in this
|
||||
// DeclForGen's lineage.
|
||||
func (decl *DeclForGen) ForLatestSchema() SchemaForGen {
|
||||
comm := decl.Meta.Common()
|
||||
return SchemaForGen{
|
||||
Name: comm.Name,
|
||||
Schema: decl.Lineage().Latest(),
|
||||
IsGroup: comm.LineageIsGroup,
|
||||
}
|
||||
}
|
||||
|
||||
// SlashHeaderMapper produces a FileMapper that injects a comment header onto
|
||||
@@ -82,22 +86,28 @@ func SlashHeaderMapper(maingen string) codejen.FileMapper {
|
||||
case ".json", ".yml", ".yaml":
|
||||
return f, nil
|
||||
default:
|
||||
b := new(bytes.Buffer)
|
||||
fmt.Fprintf(b, headerTmpl, filepath.ToSlash(maingen), f.FromString())
|
||||
fmt.Fprint(b, string(f.Data))
|
||||
f.Data = b.Bytes()
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tmpls.Lookup("gen_header.tmpl").Execute(buf, tvars_gen_header{
|
||||
MainGenerator: maingen,
|
||||
Using: f.From,
|
||||
}); err != nil {
|
||||
return codejen.File{}, fmt.Errorf("failed executing gen header template: %w", err)
|
||||
}
|
||||
fmt.Fprint(buf, string(f.Data))
|
||||
f.Data = buf.Bytes()
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
|
||||
var headerTmpl = `// THIS FILE IS GENERATED. EDITING IS FUTILE.
|
||||
//
|
||||
// Generated by:
|
||||
// %s
|
||||
// Using jennies:
|
||||
// %s
|
||||
//
|
||||
// Run 'make gen-cue' from repository root to regenerate.
|
||||
|
||||
`
|
||||
// SchemaForGen is an intermediate values type for jennies that holds both a thema.Schema,
|
||||
// and values relevant to generating the schema that should properly, eventually, be in
|
||||
// thema itself.
|
||||
type SchemaForGen struct {
|
||||
// The PascalCase name of the schematized type.
|
||||
Name string
|
||||
// The schema to be rendered for the type itself.
|
||||
Schema thema.Schema
|
||||
// Whether the schema is grouped. See https://github.com/grafana/thema/issues/62
|
||||
IsGroup bool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user