mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* 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>
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package codegen
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"go/format"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
|
"golang.org/x/tools/imports"
|
|
)
|
|
|
|
type genGoFile struct {
|
|
path string
|
|
walker astutil.ApplyFunc
|
|
in []byte
|
|
}
|
|
|
|
func postprocessGoFile(cfg genGoFile) ([]byte, error) {
|
|
fname := filepath.Base(cfg.path)
|
|
buf := new(bytes.Buffer)
|
|
fset := token.NewFileSet()
|
|
gf, err := parser.ParseFile(fset, fname, string(cfg.in), parser.ParseComments)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing generated file: %w", err)
|
|
}
|
|
|
|
if cfg.walker != nil {
|
|
astutil.Apply(gf, cfg.walker, nil)
|
|
|
|
err = format.Node(buf, fset, gf)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error formatting Go AST: %w", err)
|
|
}
|
|
} else {
|
|
buf = bytes.NewBuffer(cfg.in)
|
|
}
|
|
|
|
byt, err := imports.Process(fname, buf.Bytes(), nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("goimports processing failed: %w", err)
|
|
}
|
|
|
|
// Compare imports before and after; warn about performance if some were added
|
|
gfa, _ := parser.ParseFile(fset, fname, string(byt), parser.ParseComments)
|
|
imap := make(map[string]bool)
|
|
for _, im := range gf.Imports {
|
|
imap[im.Path.Value] = true
|
|
}
|
|
var added []string
|
|
for _, im := range gfa.Imports {
|
|
if !imap[im.Path.Value] {
|
|
added = append(added, im.Path.Value)
|
|
}
|
|
}
|
|
|
|
if len(added) != 0 {
|
|
// TODO improve the guidance in this error if/when we better abstract over imports to generate
|
|
fmt.Fprintf(os.Stderr, "The following imports were added by goimports while generating %s: \n\t%s\nRelying on goimports to find imports significantly slows down code generation. Consider adding these to the relevant template.\n", cfg.path, strings.Join(added, "\n\t"))
|
|
}
|
|
|
|
return byt, nil
|
|
}
|