Files
summercms/modules/bonfire/version.go
Jakub Zych 9641a1be59 refactor(bonfire): read build info through a package variable
BuildVersion calls readBuildInfo (debug.ReadBuildInfo) so tests can pin
the tagged, replaced and go.work cases. No behaviour change.
2026-10-21 19:14:55 +02:00

77 lines
2.2 KiB
Go

package bonfire
import (
"runtime/debug"
)
// frameworkModule is the module path of the SummerCMS framework.
const frameworkModule = "git.golem15.com/golem15/summercms"
// readBuildInfo reads the binary's build information; tests replace it.
var readBuildInfo = debug.ReadBuildInfo
// Version is the version line of a binary: the application's own version and
// the framework version it was built against.
type Version struct {
// App is the application module version ("devel" for an untagged
// local build).
App string
// Framework is the framework module version; it equals App when the
// binary is the framework's own summer tool.
Framework string
// Local is true when the framework comes from a replace directive
// (a local checkout) rather than the module proxy.
Local bool
}
// BuildVersion reads the versions from the binary's build information
// (runtime/debug.ReadBuildInfo): App is the main module version and Framework
// the version of the git.golem15.com/golem15/summercms dependency, or App
// when the main module is the framework itself. "(devel)" reads as "devel".
func BuildVersion() Version {
info, ok := readBuildInfo()
if !ok {
return Version{App: "devel", Framework: "devel"}
}
v := Version{App: cleanVersion(info.Main.Version)}
if info.Main.Path == frameworkModule {
v.Framework = v.App
return v
}
v.Framework = "devel"
for _, dep := range info.Deps {
if dep.Path != frameworkModule {
continue
}
v.Framework = cleanVersion(dep.Version)
v.Local = dep.Replace != nil
break
}
return v
}
func cleanVersion(s string) string {
if s == "" || s == "(devel)" {
return "devel"
}
return s
}
// Header is the first line of the command list: "SummerCMS acme v1.4.0
// (framework v0.3.2)", with ", local" added inside the parentheses when the
// framework is a local checkout, and "SummerCMS summer v0.3.2" when App and
// Framework are the same build.
func (v Version) Header(name string) string {
app := cleanVersion(v.App)
line := "SummerCMS " + name + " " + app
fw := v.Framework
if fw == "" || (fw == v.App && !v.Local) {
return line
}
fw = "framework " + cleanVersion(fw)
if v.Local {
fw += ", local"
}
return line + " (" + fw + ")"
}