From ad4591971eb9df3444a78c42d14da8d0e1a25220 Mon Sep 17 00:00:00 2001 From: jinzhu <1992053348@qq.com> Date: Sat, 5 Nov 2022 21:09:16 +0800 Subject: [PATCH] =?UTF-8?q?add:=20win=E4=B8=8B=E4=B8=8D=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=AE=89=E8=A3=85protoc=EF=BC=8C=E9=A1=B9=E7=9B=AE=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E7=9B=B4=E6=8E=A5=E6=94=BE=E5=85=A5protoc.exe?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 +++ console/commands/protoc.go | 45 +++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 4b505bc..11c437c 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,6 @@ export PATH := $(GOBIN):$(PATH) build: go build -ldflags="-w -s" -o $(GOBIN)/toolset ./ +build-win: + go build -ldflags="-w -s" -o $(GOBIN)/toolset.exe ./ + diff --git a/console/commands/protoc.go b/console/commands/protoc.go index 90a7f14..5d67fd9 100644 --- a/console/commands/protoc.go +++ b/console/commands/protoc.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" ) @@ -48,11 +49,6 @@ var show = false func (ProtocCommand) Execute(input command.Input) { show = input.GetOption("debug") == "true" root := getRootPath() - _, err := exec.LookPath("protoc") - if err != nil { - log.Printf("'protoc' 未安装; brew install protobuf") - return - } out := input.GetOption("go_out") out = strings.Replace(out, "@root", root, 1) outTemp, _ := filepath.Abs(out + "/../temp") @@ -79,7 +75,7 @@ func (ProtocCommand) Execute(input command.Input) { cods = append(cods, "--go_out="+outTemp) cods = append(cods, info.Path) - Cmd("protoc", cods) + ProtocCmd(cods) } } @@ -266,6 +262,43 @@ func getDocTag(doc string) ([]tag, map[string]tag) { return got, mapt } +func ProtocCmd(params []string) { + var commandName string + root := getRootPath() + switch runtime.GOOS { + case "darwin": + commandName = root + "/bin/protoc-mac" + case "windows": + commandName = root + "/bin/protoc-win.exe" + default: + commandName = root + "/bin/protoc-linux" + } + + _, err := exec.LookPath(commandName) + if err != nil { + commandName = "protoc" + _, err = exec.LookPath("protoc") + if err != nil { + log.Println("'protoc' 未安装; https://github.com/protocolbuffers/protobuf/releases") + return + } + } + + // 打印真实命令 + if show { + PrintCmd(commandName, params) + } + + cmd := exec.Command(commandName, params...) + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = os.Stderr + err = cmd.Run() + if err != nil { + log.Fatalln(err) + } +} + func Cmd(commandName string, params []string) { // 打印真实命令 if show {