From 639d2b5e1da7cfafcb7f3dc659ae655f56cd0316 Mon Sep 17 00:00:00 2001 From: Gustavo Chain Date: Tue, 20 Feb 2024 10:03:09 +0100 Subject: [PATCH] Include build version on the generate code. --- mockgen/mockgen.go | 16 +++++++++++++++- mockgen/version.go | 13 +++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index cca4467..d0cd6b7 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -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) @@ -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 { diff --git a/mockgen/version.go b/mockgen/version.go index 6db160a..4711e9d 100644 --- a/mockgen/version.go +++ b/mockgen/version.go @@ -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 " +