grafana/pkg/codegen/tmpl.go
Marcus Andersson 7f92f1df00
Schema: Refactor plugin code generation (#58901)
* wip

* wip

* almost there..

* wip - change so it can run.

* treelist is working.

* support CODEGEN_VERIFY env variable

* use log.fatal

* comment out old PluginTreeList code generation

* cleanup

* rename corelist package files

* fix makefile

* move pkg/codegen/pluggen.go to pkg/plugins/codegen

* copy and refactor files to pkg/plugins/codegen

* use pkg/plugins/codegen instead of pkg/codegen for core plugins code gen

* remove unneeded files

* remove unused code to resolve linting errors

* adapters first hack

* added flattener

* add back ignore build tags to go generate file

* cleaned up the code a bit.

* seems to work, needs to do some refactoring of the GoTypesJenns and TSTypesJenny.

* one more step, going to get upstream changes in this branch.

* working but need to run import tmpl in jenny_schemapath to have the proper imports.

* added header to generated files.

* added missing jenny.

* preventing plugins with multiple decls/schemas to insert multiple lines in corelist.

* fixed so we use Slot type from kindsys to detect if its group.

* adding a go jenny that only runs if the plugin has a backend.

* added version object to generated ts.

* generating the ts types with the same output as prior to this refactoring.

* removed code that is replaced by the jenny pattern.

* removed the go code that isn't used anymore.

* removed some more unused code and renamed pluggen to util_ts

* fixed linting issue.

* removed unused vars.

* use a jenny list postprocessor for header injection

* moved decl and decl_parser to pfs.

* removed the pre-pended header in the gotypes jenny since it is done in the postprocess.

* moved decl to pfs.

* removed unused template.

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
2022-12-02 08:22:28 +01:00

61 lines
1.3 KiB
Go

package codegen
import (
"bytes"
"embed"
"text/template"
"time"
"github.com/grafana/codejen"
)
// All the parsed templates in the tmpl subdirectory
var tmpls *template.Template
func init() {
base := template.New("codegen").Funcs(template.FuncMap{
"now": time.Now,
})
tmpls = template.Must(base.ParseFS(tmplFS, "tmpl/*.tmpl"))
}
//go:embed tmpl/*.tmpl
var tmplFS embed.FS
// The following group of types, beginning with tvars_*, all contain the set
// of variables expected by the corresponding named template file under tmpl/
type (
tvars_autogen_header struct {
GeneratorPath string
LineagePath string
LineageCUEPath string
GenLicense bool
}
tvars_gen_header struct {
MainGenerator string
Using []codejen.NamedJenny
From string
}
tvars_kind_registry struct {
// Header tvars_autogen_header
NumRaw, NumStructured int
PackageName string
KindPackagePrefix string
Kinds []*DeclForGen
}
tvars_coremodel_imports struct {
PackageName string
}
)
type HeaderVars = tvars_autogen_header
// GenGrafanaHeader creates standard header elements for generated Grafana files.
func GenGrafanaHeader(vars HeaderVars) string {
buf := new(bytes.Buffer)
if err := tmpls.Lookup("autogen_header.tmpl").Execute(buf, vars); err != nil {
panic(err)
}
return buf.String()
}