Skip to content

Commit

Permalink
feat: add versions for generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Oct 29, 2024
1 parent 979c303 commit 1547e75
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 27 deletions.
23 changes: 19 additions & 4 deletions cmd/kod/internal/generate_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path"
"path/filepath"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -644,6 +645,11 @@ func (g *generator) componentRef(comp *component) string {
func (g *generator) generateImports(p printFn) {
p(`// Code generated by "kod generate". DO NOT EDIT.`)
p("//go:build !ignoreKodGen")
debugInfo, ok := debug.ReadBuildInfo()
if ok {
p(`// versions:`)
p("// - %s\t%s", debugInfo.Path, debugInfo.Main.Version)
}
p("")
p("package %s", g.pkg.Name)
p("")
Expand All @@ -667,6 +673,9 @@ func (g *generator) generateFullMethodNames(p printFn) {
p(`const (`)
for _, comp := range g.components {
for _, m := range comp.methods() {
if g.getFirstArgTypeString(m) != "context.Context" {
continue
}
p(`// %s is the full name of the method [%s.%s].`, comp.fullMethodNameVar(m.Name()), comp.implName(), m.Name())
p(`%s = %q`, comp.fullMethodNameVar(m.Name()), comp.fullFullMethodName(m.Name()))
}
Expand Down Expand Up @@ -790,10 +799,7 @@ func (g *generator) generateLocalStubs(p printFn) {

mt := m.Type().(*types.Signature)

firstArgTypeString := ""
if mt.Params().Len() > 0 {
firstArgTypeString = g.tset.genTypeString(mt.Params().At(0).Type())
}
firstArgTypeString := g.getFirstArgTypeString(m)

p(``)
p(`// %s wraps the method [%s.%s].`, m.Name(), comp.implName(), m.Name())
Expand Down Expand Up @@ -844,6 +850,15 @@ func (g *generator) generateLocalStubs(p printFn) {
}
}

func (g *generator) getFirstArgTypeString(m *types.Func) string {
mt := m.Type().(*types.Signature)

if mt.Params().Len() > 0 {
return g.tset.genTypeString(mt.Params().At(0).Type())
}
return ""
}

func (g *generator) setReturnsList(sig *types.Signature) string {
var returns strings.Builder
for i := 0; i < sig.Results().Len(); i++ {
Expand Down
1 change: 1 addition & 0 deletions cmd/kod/internal/mock_watcher_test.go

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

14 changes: 12 additions & 2 deletions cmd/kod/internal/struct2interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -202,9 +203,17 @@ func makeInterfaceHead(pkgName string, imports []string) []string {
output := []string{
"// Code generated by \"kod generate\"; DO NOT EDIT.",
"",
}
debugInfo, ok := debug.ReadBuildInfo()
if ok {
output = append(output, "// versions:")
output = append(output, fmt.Sprintf("// - kod\t%s", debugInfo.Main.Version))
}

output = append(output, []string{
"package " + pkgName,
"import (",
}
}...)
output = append(output, imports...)
output = append(output,
")",
Expand Down Expand Up @@ -265,7 +274,8 @@ func createFile(c *cobra.Command, objs map[string]*makeInterfaceFile) error {
}

if commandExists("mockgen") {
cmd := exec.Command("mockgen", "-source", fileName, "-destination", filepath.Join(obj.DirPath, "kod_gen_mock.go"), "-package", pkgName, "-typed")
cmd := exec.Command("mockgen", "-source", fileName, "-destination", filepath.Join(obj.DirPath, "kod_gen_mock.go"),
"-package", pkgName, "-typed", "-build_constraint", "!ignoreKodGen")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

Expand Down
3 changes: 3 additions & 0 deletions examples/helloworld/kod_gen.go

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

2 changes: 2 additions & 0 deletions examples/helloworld/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion examples/helloworld/kod_gen_mock.go

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

13 changes: 3 additions & 10 deletions tests/case1/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/case1/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/case1/kod_gen_mock.go

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

3 changes: 3 additions & 0 deletions tests/case2/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/case2/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/case2/kod_gen_mock.go

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

3 changes: 3 additions & 0 deletions tests/case3/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/case3/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/case3/kod_gen_mock.go

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

3 changes: 3 additions & 0 deletions tests/case4/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/case4/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/case4/kod_gen_mock.go

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

3 changes: 3 additions & 0 deletions tests/case5/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/case5/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/case5/kod_gen_mock.go

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

7 changes: 3 additions & 4 deletions tests/graphcase/kod_gen.go

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

2 changes: 2 additions & 0 deletions tests/graphcase/kod_gen_interface.go

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

4 changes: 3 additions & 1 deletion tests/graphcase/kod_gen_mock.go

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

0 comments on commit 1547e75

Please sign in to comment.