Skip to content

Commit

Permalink
Update vprotogen (v2fly#366)
Browse files Browse the repository at this point in the history
* Refine vprotogen
* Regenerate pb.go files
  • Loading branch information
Loyalsoldier authored Oct 29, 2020
1 parent c4e47e4 commit f17063a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 175 deletions.
2 changes: 1 addition & 1 deletion app/log/command/config_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/proxyman/command/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/router/command/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/stats/command/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 8 additions & 82 deletions infra/vprotogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ func main() {
}

GOBIN := common.GetGOBIN()
binPath := os.Getenv("PATH")
pathSlice := []string{binPath, GOBIN, pwd}
binPath = strings.Join(pathSlice, string(os.PathListSeparator))
os.Setenv("PATH", binPath)

EXE := ""
if runtime.GOOS == "windows" {
EXE = ".exe"
}
protoc := "protoc" + EXE

if path, err := exec.LookPath(protoc); err != nil {
fmt.Println("Make sure that you have `" + protoc + "` in your system or current path, please visit https://github.com/protocolbuffers/protobuf/releases")
fmt.Println("Make sure that you have `" + protoc + "` in your system path or current path. To download it, please visit https://github.com/protocolbuffers/protobuf/releases")
os.Exit(1)
} else {
protoc = path
Expand Down Expand Up @@ -61,14 +66,13 @@ func main() {
for _, relProtoFile := range files {
var args []string
if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
args = []string{"--gofast_out", pwd, "--gofast_opt", "paths=source_relative", "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
} else {
args = []string{"--go_out", pwd, "--go-grpc_out", pwd, "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go" + EXE, "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc" + EXE}
args = []string{"--go_out", pwd, "--go_opt", "paths=source_relative", "--go-grpc_out", pwd, "--go-grpc_opt", "paths=source_relative", "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go" + EXE, "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc" + EXE}
}
args = append(args, relProtoFile)
cmd := exec.Command(protoc, args...)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
output, cmdErr := cmd.CombinedOutput()
if len(output) > 0 {
fmt.Println(string(output))
Expand All @@ -79,82 +83,4 @@ func main() {
}
}
}

moduleName, gmnErr := common.GetModuleName(pwd)
if gmnErr != nil {
fmt.Println(gmnErr)
os.Exit(1)
}
modulePath := filepath.Join(strings.Split(moduleName, "/")...)

pbGoFilesMap := make(map[string][]string)
walkErr2 := filepath.Walk(modulePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}

if info.IsDir() {
return nil
}

dir := filepath.Dir(path)
filename := filepath.Base(path)
if strings.HasSuffix(filename, ".pb.go") {
pbGoFilesMap[dir] = append(pbGoFilesMap[dir], path)
}

return nil
})
if walkErr2 != nil {
fmt.Println(walkErr2)
os.Exit(1)
}

var err error
for _, srcPbGoFiles := range pbGoFilesMap {
for _, srcPbGoFile := range srcPbGoFiles {
var dstPbGoFile string
dstPbGoFile, err = filepath.Rel(modulePath, srcPbGoFile)
if err != nil {
fmt.Println(err)
continue
}
err = os.Link(srcPbGoFile, dstPbGoFile)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("'%s' does not exist\n", srcPbGoFile)
continue
}
if os.IsPermission(err) {
fmt.Println(err)
continue
}
if os.IsExist(err) {
err = os.Remove(dstPbGoFile)
if err != nil {
fmt.Printf("Failed to delete file '%s'\n", dstPbGoFile)
continue
}
err = os.Rename(srcPbGoFile, dstPbGoFile)
if err != nil {
fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile)
}
continue
}
}
err = os.Rename(srcPbGoFile, dstPbGoFile)
if err != nil {
fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile)
}
continue
}
}

if err == nil {
err = os.RemoveAll(strings.Split(modulePath, "/")[0])
if err != nil {
fmt.Println(err)
}
}
}
Loading

0 comments on commit f17063a

Please sign in to comment.