entc/gen: fix module version extraction when running entc as a package

This commit is contained in:
Ariel Mashraki
2021-10-28 14:51:20 +03:00
committed by Ariel Mashraki
parent 59069b7449
commit 51b63c17c0

View File

@@ -650,11 +650,22 @@ func (g *Graph) templates() (*Template, []GraphTemplate) {
return templates, external
}
// ModuleInfo returns the entc binary module version.
// ModuleInfo returns the entgo.io/ent version.
func (Config) ModuleInfo() (m debug.Module) {
const pkg = "entgo.io/ent"
info, ok := debug.ReadBuildInfo()
if ok {
m = info.Main
if !ok {
return
}
// Was running as a CLI (ent/cmd/ent).
if info.Main.Path == pkg {
return info.Main
}
// Or, as a main package (ent/entc).
for _, dep := range info.Deps {
if dep.Path == pkg {
return *dep
}
}
return
}