Skip to content

Commit

Permalink
add: win下不方便安装protoc,项目可以直接放入protoc.exe文件
Browse files Browse the repository at this point in the history
  • Loading branch information
ctfang committed Nov 5, 2022
1 parent 91e411c commit ad45919
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./

45 changes: 39 additions & 6 deletions console/commands/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ad45919

Please sign in to comment.