Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include build version on the generate code. #155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
g.p("")
}

g.p("// Code generated by MockGen. DO NOT EDIT.")
g.p("// Code generated by MockGen %s. DO NOT EDIT.", genVersion())
if *writeSourceComment {
if g.filename != "" {
g.p("// Source: %v", g.filename)
Expand Down Expand Up @@ -431,6 +431,20 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
return nil
}

// genVersion returns the version to be used in the generated code.
func genVersion() string {
if version != "" {
return version
}

v, ok := moduleVersion()
if !ok {
return "unknown"
}

return v
}

// The name of the mock type to use for the given interface identifier.
func (g *generator) mockName(typeName string) string {
if mockName, ok := g.mockNames[typeName]; ok {
Expand Down
13 changes: 11 additions & 2 deletions mockgen/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ import (
"runtime/debug"
)

func moduleVersion() (string, bool) {
bi, exists := debug.ReadBuildInfo()
if !exists {
return "", false
}

return bi.Main.Version, true
}

func printModuleVersion() {
if bi, exists := debug.ReadBuildInfo(); exists {
fmt.Println(bi.Main.Version)
if mv, exists := moduleVersion(); exists {
fmt.Println(mv)
} else {
log.Printf("No version information found. Make sure to use " +
"GO111MODULE=on when running 'go get' in order to use specific " +
Expand Down