Codegen: format golang code from cue (#105352)
This commit is contained in:
+2
-1
@@ -18,6 +18,7 @@ import (
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/cuecontext"
|
||||
"cuelang.org/go/cue/load"
|
||||
|
||||
"github.com/grafana/codejen"
|
||||
"github.com/grafana/cuetsy"
|
||||
"github.com/grafana/grafana/pkg/codegen"
|
||||
@@ -54,7 +55,7 @@ func main() {
|
||||
)
|
||||
|
||||
header := codegen.SlashHeaderMapper("kinds/gen.go")
|
||||
coreKindsGen.AddPostprocessors(header)
|
||||
coreKindsGen.AddPostprocessors(header, codegen.GoFormat())
|
||||
|
||||
ctx := cuecontext.New()
|
||||
|
||||
|
||||
+1
-1
@@ -9,6 +9,7 @@ require (
|
||||
github.com/grafana/cog v0.0.28
|
||||
github.com/grafana/cuetsy v0.1.11
|
||||
github.com/matryer/is v1.4.1
|
||||
golang.org/x/tools v0.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -48,7 +49,6 @@ require (
|
||||
golang.org/x/net v0.39.0 // indirect
|
||||
golang.org/x/sync v0.13.0 // indirect
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
golang.org/x/tools v0.32.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"go/format"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/tools/imports"
|
||||
|
||||
"github.com/grafana/codejen"
|
||||
)
|
||||
|
||||
// GoFormat applies go format to each go file
|
||||
func GoFormat() codejen.FileMapper {
|
||||
return func(f codejen.File) (codejen.File, error) {
|
||||
if filepath.Ext(f.RelativePath) != ".go" {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
formatted, err := format.Source(f.Data)
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
|
||||
f.Data, err = imports.Process("", formatted, &imports.Options{
|
||||
Comments: true,
|
||||
})
|
||||
return f, err
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,11 @@ func main() {
|
||||
codegen.PluginTSTypesJenny("public/app/plugins"),
|
||||
)
|
||||
|
||||
pluginKindGen.AddPostprocessors(corecodegen.SlashHeaderMapper("public/app/plugins/gen.go"), splitSchiffer())
|
||||
pluginKindGen.AddPostprocessors(
|
||||
corecodegen.SlashHeaderMapper("public/app/plugins/gen.go"),
|
||||
corecodegen.GoFormat(),
|
||||
splitSchiffer(),
|
||||
)
|
||||
|
||||
declParser := pfs.NewDeclParser(skipPlugins)
|
||||
decls, err := declParser.Parse(os.DirFS(cwd))
|
||||
|
||||
Reference in New Issue
Block a user