Skip to content

Commit

Permalink
Merge pull request #401 from gucio321/codegen-use-gofumpt
Browse files Browse the repository at this point in the history
Codegen: compile-in gofumpt
  • Loading branch information
gucio321 authored Dec 22, 2024
2 parents 5cb4796 + ec6f920 commit 523f067
Show file tree
Hide file tree
Showing 14 changed files with 907 additions and 848 deletions.
6 changes: 3 additions & 3 deletions ImGuiColorTextEdit/funcs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ all: generate
## setup: prepare some dependencies
.PHONY: setup
setup:
go install mvdan.cc/gofumpt@latest
cd cmd/codegen; \
go build -o ../../codegen .

Expand All @@ -26,14 +25,6 @@ define generate
cat templates/cflags.go |sed -e "s/^package.*/package $(2)/g" > $(2)/cflags.go
cd $(2); \
../codegen -preset ../cmd/codegen/cimgui-go-preset.json -p $(1) -pkg $(2) -i ../$(3) -d ../$(4) -e ../$(5) -t ../$(6) $(7)
go run mvdan.cc/gofumpt@latest -w $(2)/enums.go
go run mvdan.cc/gofumpt@latest -w $(2)/funcs.go
go run mvdan.cc/gofumpt@latest -w $(2)/typedefs.go
go run mvdan.cc/gofumpt@latest -w $(2)/callbacks.go
go run golang.org/x/tools/cmd/goimports@latest -w $(2)/funcs.go
go run golang.org/x/tools/cmd/goimports@latest -w $(2)/enums.go
go run golang.org/x/tools/cmd/goimports@latest -w $(2)/typedefs.go
go run golang.org/x/tools/cmd/goimports@latest -w $(2)/callbacks.go
endef

define imgui
Expand Down
31 changes: 31 additions & 0 deletions cmd/codegen/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/kpango/glg"
"golang.org/x/tools/imports"
"mvdan.cc/gofumpt/format"
)

// FormatGo takes an output GO string and formats it using gofumpt and goimports returning the formatted string.
func FormatGo(s string, ctx *Context) string {
var err error
sBytes := []byte(s)
sBytes, err = format.Source(sBytes, format.Options{
ModulePath: ctx.preset.PackagePath,
ExtraRules: true,
})
if err != nil {
glg.Fatalf("Unable to format go code: %v", err)
}

sBytes, err = imports.Process("", sBytes, &imports.Options{
Fragment: false,
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: false,
})

return string(sBytes)
}
2 changes: 1 addition & 1 deletion cmd/codegen/gengo_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ import "unsafe"

fmt.Fprintf(result, g.goSb.String())

if err := os.WriteFile("callbacks.go", []byte(result.String()), 0644); err != nil {
if err := os.WriteFile("callbacks.go", []byte(FormatGo(result.String(), g.ctx)), 0644); err != nil {
return fmt.Errorf("cannot write typedefs.go: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/codegen/gengo_enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateGoEnums(prefix string, enums []EnumDef, ctx *Context) ([]CIdentifie

defer enumFile.Close()

_, _ = enumFile.WriteString(sb.String())
_, _ = enumFile.WriteString(FormatGo(sb.String(), ctx))

return enumNames, nil
}
2 changes: 1 addition & 1 deletion cmd/codegen/gengo_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GenerateGoFuncs(

defer goFile.Close()

_, err = goFile.WriteString(generator.sb.String())
_, err = goFile.WriteString(FormatGo(generator.sb.String(), context))
if err != nil {
return fmt.Errorf("failed to write content of GO file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/codegen/gengo_typedefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (g *typedefsGenerator) saveToDisk() error {
}
#endif`)

if err := os.WriteFile("typedefs.go", []byte(g.GoSb.String()), 0644); err != nil {
if err := os.WriteFile("typedefs.go", []byte(FormatGo(g.GoSb.String(), g.ctx)), 0644); err != nil {
return fmt.Errorf("cannot write typedefs.go: %w", err)
}

Expand Down
13 changes: 11 additions & 2 deletions cmd/codegen/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
module github.com/AllenDang/cimgui-go/cmd/codegen

go 1.20
go 1.22.0

require github.com/kpango/glg v1.6.15
toolchain go1.23.1

require (
github.com/kpango/glg v1.6.15
mvdan.cc/gofumpt v0.7.0
)

require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kpango/fastime v1.1.9 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/tools v0.28.0 // indirect
)
30 changes: 29 additions & 1 deletion cmd/codegen/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84=
github.com/kpango/fastime v1.1.9/go.mod h1:vyD7FnUn08zxY4b/QFBZVG+9EWMYsNl+QF0uE46urD4=
github.com/kpango/glg v1.6.15 h1:nw0xSxpSyrDIWHeb3dvnE08PW+SCbK+aYFETT75IeLA=
github.com/kpango/glg v1.6.15/go.mod h1:cmsc7Yeu8AS3wHLmN7bhwENXOpxfq+QoqxCIk2FneRk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
mvdan.cc/gofumpt v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo=
70 changes: 35 additions & 35 deletions imgui/callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 523f067

Please sign in to comment.