1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00

Merge pull request #259 from Loyalsoldier/refine-code

Refine code
This commit is contained in:
Loyalsoldier 2020-10-04 10:27:28 +08:00 committed by GitHub
commit d30b5fb501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 21 deletions

View File

@ -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
}

View File

@ -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"}

View File

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