Browse Source

Merge pull request #259 from Loyalsoldier/refine-code

Refine code
Loyalsoldier 5 years ago
parent
commit
d30b5fb501
3 changed files with 26 additions and 21 deletions
  1. 6 6
      common/common.go
  2. 3 10
      infra/vprotogen/main.go
  3. 17 5
      proto.go

+ 6 - 6
common/common.go

@@ -125,26 +125,26 @@ func GetGOPATH() string {
 }
 }
 
 
 // GetModuleName returns the value of module in `go.mod` file.
 // 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)
 	gomodBytes, err := ioutil.ReadFile(gomodPath)
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
 	gomodContent := string(gomodBytes)
 	gomodContent := string(gomodBytes)
-	moduleIdx := strings.Index(gomodContent, "module") + 6
+	moduleIdx := strings.Index(gomodContent, "module ")
 	newLineIdx := strings.Index(gomodContent, "\n")
 	newLineIdx := strings.Index(gomodContent, "\n")
 
 
 	var moduleName string
 	var moduleName string
 	if moduleIdx >= 0 {
 	if moduleIdx >= 0 {
 		if newLineIdx >= 0 {
 		if newLineIdx >= 0 {
-			moduleName = strings.TrimSpace(gomodContent[moduleIdx:newLineIdx])
+			moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
 			moduleName = strings.TrimSuffix(moduleName, "\r")
 			moduleName = strings.TrimSuffix(moduleName, "\r")
 		} else {
 		} else {
-			moduleName = strings.TrimSpace(gomodContent[moduleIdx:])
+			moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
 		}
 		}
 	} else {
 	} 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
 	return moduleName, nil
 }
 }

+ 3 - 10
infra/vprotogen/main.go

@@ -8,17 +8,10 @@ import (
 	"runtime"
 	"runtime"
 	"strings"
 	"strings"
 
 
+	"v2ray.com/core"
 	"v2ray.com/core/common"
 	"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() {
 func main() {
 	pwd, wdErr := os.Getwd()
 	pwd, wdErr := os.Getwd()
 	if wdErr != nil {
 	if wdErr != nil {
@@ -27,7 +20,7 @@ func main() {
 	}
 	}
 
 
 	GOBIN := common.GetGOBIN()
 	GOBIN := common.GetGOBIN()
-	protoc := protocMap[runtime.GOOS]
+	protoc := core.ProtocMap[runtime.GOOS]
 
 
 	protoFilesMap := make(map[string][]string)
 	protoFilesMap := make(map[string][]string)
 	walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
 	walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
@@ -56,7 +49,7 @@ func main() {
 	for _, files := range protoFilesMap {
 	for _, files := range protoFilesMap {
 		for _, relProtoFile := range files {
 		for _, relProtoFile := range files {
 			var args []string
 			var args []string
-			if protoFilesUsingProtocGenGoFast[relProtoFile] {
+			if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
 				args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast"}
 				args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast"}
 			} else {
 			} 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"}
 				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"}

+ 17 - 5
proto.go

@@ -1,7 +1,19 @@
 package core
 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
+
+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"),
+}