2 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個贊
我能夠按照Go Modules 工具指南以及用于protoc二進(jìn)制文件的一點(diǎn) Makefile-Fu完成 protoc 的供應(yīng)商工具構(gòu)建(以及Twirp等插件) 。
完整的工作示例可以在 Aspiration Labs pyggpot repo中找到。以下是基本細(xì)節(jié)。值得注意的是:為某些工具找到正確的導(dǎo)入路徑非常繁瑣,但最終還是成功的。
對于protoc
其本身,我在 Makefile 中提供二進(jìn)制版本并將其設(shè)置到目錄中tools/bin
:
TOOLS_DIR := ./tools
TOOLS_BIN := $(TOOLS_DIR)/bin
# protoc
PROTOC_VERSION := 3.7.1
PROTOC_PLATFORM := osx-x86_64
PROTOC_RELEASES_PATH := https://github.com/protocolbuffers/protobuf/releases/download
PROTOC_ZIP := protoc-$(PROTOC_VERSION)-$(PROTOC_PLATFORM).zip
PROTOC_DOWNLOAD := $(PROTOC_RELEASES_PATH)/v$(PROTOC_VERSION)/$(PROTOC_ZIP)
PROTOC := $(TOOLS_BIN)/protoc
# protoc
$(PROTOC): $(TOOLS_DIR)/$(PROTOC_ZIP)
? ? unzip -o -d "$(TOOLS_DIR)" $< && touch $@? # avoid Prerequisite is newer than target `tools/bin/protoc'.
$(TOOLS_DIR)/$(PROTOC_ZIP):
? ? curl --location $(PROTOC_DOWNLOAD) --output $@
該PROTOC_PLATFORM
字符串可以通過諸如操作系統(tǒng)檢測 makefile之類的東西來自動化。
繼續(xù)構(gòu)建 go 工具。創(chuàng)建一個tools.go
類似的東西
// +build tools
package tools
import (
? ? // protocol buffer compiler plugins
? ? _ "github.com/golang/protobuf/protoc-gen-go"
? ? _ "github.com/twitchtv/twirp/protoc-gen-twirp"
? ? _ "github.com/twitchtv/twirp/protoc-gen-twirp_python"
? ? _ "github.com/thechriswalker/protoc-gen-twirp_js"
)
注意:該// +build tools標(biāo)簽將避免go build在最終構(gòu)建中過度構(gòu)建工具導(dǎo)入。
最后,一些代碼來構(gòu)建你的 go 工具:
# go installed tools.go
GO_TOOLS := github.com/golang/protobuf/protoc-gen-go \
? ? ? ? ? ? github.com/twitchtv/twirp/protoc-gen-twirp \
? ? ? ? ? ? github.com/twitchtv/twirp/protoc-gen-twirp_python \
? ? ? ? ? ? github.com/thechriswalker/protoc-gen-twirp_js \
# tools
GO_TOOLS_BIN := $(addprefix $(TOOLS_BIN), $(notdir $(GO_TOOLS)))
GO_TOOLS_VENDOR := $(addprefix vendor/, $(GO_TOOLS))
setup_tools: $(GO_TOOLS_BIN)
$(GO_TOOLS_BIN): $(GO_TOOLS_VENDOR)
? ? GOBIN="$(PWD)/$(TOOLS_BIN)" go install -mod=vendor $(GO_TOOLS)
最后,make setup運(yùn)行g(shù)o mod vendor和處理上述目標(biāo)的目標(biāo)。
setup: setup_vendor $(TOOLS_DIR) $(PROTOC) setup_tools
# vendor
setup_vendor:
? ? go mod vendor
$(TOOLS_DIR):
? ? mkdir -v -p $@

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個贊
Go 模塊可與 .go 文件的導(dǎo)入一起使用。如果他們找到導(dǎo)入,他們將自動下載滿足您要求的最新版本。
此時,為什么 go.mod 中沒有列出 protoc-gen-go 工具版本(或 git hash)?
這是因?yàn)榫?Go 模塊而言,protoc-gen-go 只是一個外部工具。您不導(dǎo)入golang/protobuf/tree/master/protoc-gen-go
而是導(dǎo)入它生成的代碼。
當(dāng) Joe 克隆應(yīng)用程序存儲庫時,他如何獲取并編譯 Ryan 使用的相同版本的 protoc-gen-go?
使用:
GIT_TAG="v1.2.0" # change as needed
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go
在用戶的每臺計(jì)算機(jī)上安裝特定版本??赡軙帉懸粋€構(gòu)建腳本來自動化該過程。
protoc 如何知道在我的 ./bin 目錄中使用 protoc-gen-go 可執(zhí)行文件生成器?
來自 github 文檔:除非設(shè)置了 $GOBIN,否則編譯器插件 protoc-gen-go 將安裝在 $GOPATH/bin 中。它必須位于您的 $PATH 中,協(xié)議編譯器 protoc 才能找到它。
- 2 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)