From 35eb0a9164881aa508892a68977db20b0aaeda32 Mon Sep 17 00:00:00 2001 From: xuthus5 Date: Sat, 20 Apr 2024 01:37:15 +0800 Subject: [PATCH] feat: generate plugin version --- main.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index ec702a3..7b2167b 100644 --- a/main.go +++ b/main.go @@ -4,16 +4,28 @@ import ( "bytes" "flag" "fmt" - "os" - "path/filepath" - "strings" - "time" - "github.com/emicklei/proto" "gitter.top/common/protofmt" "google.golang.org/protobuf/compiler/protogen" + "os" + "path/filepath" + "runtime/debug" + "strings" ) +var ( + version string +) + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + version = "unknown" + return + } + version = info.Main.Version +} + type Coco struct { DisableGenerateRouter bool // 禁止路由信息生成 DisableGenerateMongoModel bool // 禁止mongo信息生成 @@ -109,7 +121,7 @@ func (c *Coco) generateRouterMap(plugin *protogen.Plugin) { g := plugin.NewGeneratedFile(filename, pbFile.GoImportPath) g.P("// Code generated by protoc-gen-coco. DO NOT EDIT.") g.P("// source: ", pbFile.GeneratedFilenamePrefix, ".proto") - g.P("// generate at: ", time.Now().Format("2006-01-02 15:04:05")) + g.P("// protoc-gen-coco: ", version) g.P() g.P("package ", pbFile.GoPackageName) g.P() @@ -224,7 +236,7 @@ func (c *Coco) generateMongoModel(plugin *protogen.Plugin) { g := plugin.NewGeneratedFile(filename, pbFile.GoImportPath) g.P("// Code generated by protoc-gen-coco. DO NOT EDIT.") g.P("// source: ", pbFile.GeneratedFilenamePrefix, ".proto") - g.P("// generate at: ", time.Now().Format("2006-01-02 15:04:05")) + g.P("// protoc-gen-coco: ", version) g.P() g.P("package ", pbFile.GoPackageName) g.P()