From 4ba4cad7ae8657b8cf5f41b268319e40d24831e2 Mon Sep 17 00:00:00 2001 From: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Sun, 4 Oct 2020 09:55:21 +0800 Subject: [PATCH 1/3] Refine func GetModuleName --- common/common.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/common.go b/common/common.go index be06b3a39..4156a7b17 100644 --- a/common/common.go +++ b/common/common.go @@ -125,26 +125,26 @@ func GetGOPATH() string { } // GetModuleName returns the value of module in `go.mod` file. -func GetModuleName(path string) (string, error) { - gomodPath := filepath.Join(path, "go.mod") +func GetModuleName(pathToProjectRoot string) (string, error) { + gomodPath := filepath.Join(pathToProjectRoot, "go.mod") gomodBytes, err := ioutil.ReadFile(gomodPath) if err != nil { return "", err } gomodContent := string(gomodBytes) - moduleIdx := strings.Index(gomodContent, "module") + 6 + moduleIdx := strings.Index(gomodContent, "module ") newLineIdx := strings.Index(gomodContent, "\n") var moduleName string if moduleIdx >= 0 { if newLineIdx >= 0 { - moduleName = strings.TrimSpace(gomodContent[moduleIdx:newLineIdx]) + moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx]) moduleName = strings.TrimSuffix(moduleName, "\r") } else { - moduleName = strings.TrimSpace(gomodContent[moduleIdx:]) + moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:]) } } else { - return "", fmt.Errorf("can not get the value of `module` in path `%s`", gomodPath) + return "", fmt.Errorf("can not get module path in `%s`", gomodPath) } return moduleName, nil } From edefca79321ba72d8a99e4e7ac6d4c09e05d8274 Mon Sep 17 00:00:00 2001 From: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Sun, 4 Oct 2020 09:56:45 +0800 Subject: [PATCH 2/3] Refine & make proto.go work outside GOPATH --- proto.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/proto.go b/proto.go index d6e4b646a..46a849f85 100644 --- a/proto.go +++ b/proto.go @@ -1,7 +1,6 @@ package core -//go:generate go install google.golang.org/protobuf/proto -//go:generate go install google.golang.org/protobuf/cmd/protoc-gen-go -//go:generate go install google.golang.org/grpc/cmd/protoc-gen-go-grpc -//go:generate go install github.com/gogo/protobuf/protoc-gen-gofast -//go:generate go run v2ray.com/core/infra/vprotogen +//go:generate go install -v google.golang.org/protobuf/cmd/protoc-gen-go +//go:generate go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc +//go:generate go install -v github.com/gogo/protobuf/protoc-gen-gofast +//go:generate go run ./infra/vprotogen/main.go From 9b249f912b2cb691ff702a8aaf189e6835ef4753 Mon Sep 17 00:00:00 2001 From: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Sun, 4 Oct 2020 10:05:00 +0800 Subject: [PATCH 3/3] Make variables of vprotogen easier to find --- infra/vprotogen/main.go | 13 +++---------- proto.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/infra/vprotogen/main.go b/infra/vprotogen/main.go index dea42500f..98091bf8a 100644 --- a/infra/vprotogen/main.go +++ b/infra/vprotogen/main.go @@ -8,17 +8,10 @@ import ( "runtime" "strings" + "v2ray.com/core" "v2ray.com/core/common" ) -var protoFilesUsingProtocGenGoFast = map[string]bool{"proxy/vless/encoding/addons.proto": true} - -var protocMap = map[string]string{ - "windows": filepath.Join(".dev", "protoc", "windows", "protoc.exe"), - "darwin": filepath.Join(".dev", "protoc", "macos", "protoc"), - "linux": filepath.Join(".dev", "protoc", "linux", "protoc"), -} - func main() { pwd, wdErr := os.Getwd() if wdErr != nil { @@ -27,7 +20,7 @@ func main() { } GOBIN := common.GetGOBIN() - protoc := protocMap[runtime.GOOS] + protoc := core.ProtocMap[runtime.GOOS] protoFilesMap := make(map[string][]string) walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error { @@ -56,7 +49,7 @@ func main() { for _, files := range protoFilesMap { for _, relProtoFile := range files { var args []string - if protoFilesUsingProtocGenGoFast[relProtoFile] { + if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] { args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast"} } else { args = []string{"--go_out", pwd, "--go-grpc_out", pwd, "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go", "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc"} diff --git a/proto.go b/proto.go index 46a849f85..b490c136e 100644 --- a/proto.go +++ b/proto.go @@ -4,3 +4,16 @@ package core //go:generate go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc //go:generate go install -v github.com/gogo/protobuf/protoc-gen-gofast //go:generate go run ./infra/vprotogen/main.go + +import "path/filepath" + +// ProtoFilesUsingProtocGenGoFast is the map of Proto files +// that use `protoc-gen-gofast` to generate pb.go files +var ProtoFilesUsingProtocGenGoFast = map[string]bool{"proxy/vless/encoding/addons.proto": true} + +// ProtocMap is the map of paths to `protoc` binary excutable files of specific platform +var ProtocMap = map[string]string{ + "windows": filepath.Join(".dev", "protoc", "windows", "protoc.exe"), + "darwin": filepath.Join(".dev", "protoc", "macos", "protoc"), + "linux": filepath.Join(".dev", "protoc", "linux", "protoc"), +}