Skip to content

Commit 523f067

Browse files
authored
Merge pull request #401 from gucio321/codegen-use-gofumpt
Codegen: compile-in gofumpt
2 parents 5cb4796 + ec6f920 commit 523f067

File tree

14 files changed

+907
-848
lines changed

14 files changed

+907
-848
lines changed

ImGuiColorTextEdit/funcs.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ all: generate
88
## setup: prepare some dependencies
99
.PHONY: setup
1010
setup:
11-
go install mvdan.cc/gofumpt@latest
1211
cd cmd/codegen; \
1312
go build -o ../../codegen .
1413

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

3930
define imgui

cmd/codegen/format.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"github.com/kpango/glg"
5+
"golang.org/x/tools/imports"
6+
"mvdan.cc/gofumpt/format"
7+
)
8+
9+
// FormatGo takes an output GO string and formats it using gofumpt and goimports returning the formatted string.
10+
func FormatGo(s string, ctx *Context) string {
11+
var err error
12+
sBytes := []byte(s)
13+
sBytes, err = format.Source(sBytes, format.Options{
14+
ModulePath: ctx.preset.PackagePath,
15+
ExtraRules: true,
16+
})
17+
if err != nil {
18+
glg.Fatalf("Unable to format go code: %v", err)
19+
}
20+
21+
sBytes, err = imports.Process("", sBytes, &imports.Options{
22+
Fragment: false,
23+
AllErrors: true,
24+
Comments: true,
25+
TabIndent: true,
26+
TabWidth: 8,
27+
FormatOnly: false,
28+
})
29+
30+
return string(sBytes)
31+
}

cmd/codegen/gengo_callbacks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ import "unsafe"
470470

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

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

cmd/codegen/gengo_enums.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func generateGoEnums(prefix string, enums []EnumDef, ctx *Context) ([]CIdentifie
4242

4343
defer enumFile.Close()
4444

45-
_, _ = enumFile.WriteString(sb.String())
45+
_, _ = enumFile.WriteString(FormatGo(sb.String(), ctx))
4646

4747
return enumNames, nil
4848
}

cmd/codegen/gengo_funcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func GenerateGoFuncs(
9191

9292
defer goFile.Close()
9393

94-
_, err = goFile.WriteString(generator.sb.String())
94+
_, err = goFile.WriteString(FormatGo(generator.sb.String(), context))
9595
if err != nil {
9696
return fmt.Errorf("failed to write content of GO file: %w", err)
9797
}

cmd/codegen/gengo_typedefs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func (g *typedefsGenerator) saveToDisk() error {
422422
}
423423
#endif`)
424424

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

cmd/codegen/go.mod

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
module github.com/AllenDang/cimgui-go/cmd/codegen
22

3-
go 1.20
3+
go 1.22.0
44

5-
require github.com/kpango/glg v1.6.15
5+
toolchain go1.23.1
6+
7+
require (
8+
github.com/kpango/glg v1.6.15
9+
mvdan.cc/gofumpt v0.7.0
10+
)
611

712
require (
813
github.com/goccy/go-json v0.10.2 // indirect
14+
github.com/google/go-cmp v0.6.0 // indirect
915
github.com/kpango/fastime v1.1.9 // indirect
16+
golang.org/x/mod v0.22.0 // indirect
17+
golang.org/x/sync v0.10.0 // indirect
18+
golang.org/x/tools v0.28.0 // indirect
1019
)

cmd/codegen/go.sum

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
2+
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
13
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
24
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
37
github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84=
48
github.com/kpango/fastime v1.1.9/go.mod h1:vyD7FnUn08zxY4b/QFBZVG+9EWMYsNl+QF0uE46urD4=
59
github.com/kpango/glg v1.6.15 h1:nw0xSxpSyrDIWHeb3dvnE08PW+SCbK+aYFETT75IeLA=
610
github.com/kpango/glg v1.6.15/go.mod h1:cmsc7Yeu8AS3wHLmN7bhwENXOpxfq+QoqxCIk2FneRk=
11+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
12+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
13+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
14+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
15+
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
16+
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
717
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
18+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
819
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
20+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
921
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
22+
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
1023
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
11-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
24+
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
25+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
26+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
27+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
28+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
29+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
30+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
31+
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
32+
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
33+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
34+
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
35+
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
36+
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
37+
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
38+
mvdan.cc/gofumpt v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
39+
mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo=

imgui/callbacks.go

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)