github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/vprotogen/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 "strings" 10 11 "v2ray.com/core" 12 "v2ray.com/core/common" 13 ) 14 15 func main() { 16 pwd, wdErr := os.Getwd() 17 if wdErr != nil { 18 fmt.Println("Can not get current working directory.") 19 os.Exit(1) 20 } 21 22 GOBIN := common.GetGOBIN() 23 protoc := core.ProtocMap[runtime.GOOS] 24 25 protoFilesMap := make(map[string][]string) 26 walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error { 27 if err != nil { 28 fmt.Println(err) 29 return err 30 } 31 32 if info.IsDir() { 33 return nil 34 } 35 36 dir := filepath.Dir(path) 37 filename := filepath.Base(path) 38 if strings.HasSuffix(filename, ".proto") { 39 protoFilesMap[dir] = append(protoFilesMap[dir], path) 40 } 41 42 return nil 43 }) 44 if walkErr != nil { 45 fmt.Println(walkErr) 46 os.Exit(1) 47 } 48 49 for _, files := range protoFilesMap { 50 for _, relProtoFile := range files { 51 var args []string 52 if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] { 53 args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast"} 54 } else { 55 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"} 56 } 57 args = append(args, relProtoFile) 58 cmd := exec.Command(protoc, args...) 59 cmd.Env = append(cmd.Env, os.Environ()...) 60 cmd.Env = append(cmd.Env, "GOBIN="+GOBIN) 61 output, cmdErr := cmd.CombinedOutput() 62 if len(output) > 0 { 63 fmt.Println(string(output)) 64 } 65 if cmdErr != nil { 66 fmt.Println(cmdErr) 67 os.Exit(1) 68 } 69 } 70 } 71 72 moduleName, gmnErr := common.GetModuleName(pwd) 73 if gmnErr != nil { 74 fmt.Println(gmnErr) 75 os.Exit(1) 76 } 77 modulePath := filepath.Join(strings.Split(moduleName, "/")...) 78 79 pbGoFilesMap := make(map[string][]string) 80 walkErr2 := filepath.Walk(modulePath, func(path string, info os.FileInfo, err error) error { 81 if err != nil { 82 fmt.Println(err) 83 return err 84 } 85 86 if info.IsDir() { 87 return nil 88 } 89 90 dir := filepath.Dir(path) 91 filename := filepath.Base(path) 92 if strings.HasSuffix(filename, ".pb.go") { 93 pbGoFilesMap[dir] = append(pbGoFilesMap[dir], path) 94 } 95 96 return nil 97 }) 98 if walkErr2 != nil { 99 fmt.Println(walkErr2) 100 os.Exit(1) 101 } 102 103 var err error 104 for _, srcPbGoFiles := range pbGoFilesMap { 105 for _, srcPbGoFile := range srcPbGoFiles { 106 var dstPbGoFile string 107 dstPbGoFile, err = filepath.Rel(modulePath, srcPbGoFile) 108 if err != nil { 109 fmt.Println(err) 110 continue 111 } 112 err = os.Link(srcPbGoFile, dstPbGoFile) 113 if err != nil { 114 if os.IsNotExist(err) { 115 fmt.Printf("'%s' does not exist\n", srcPbGoFile) 116 continue 117 } 118 if os.IsPermission(err) { 119 fmt.Println(err) 120 continue 121 } 122 if os.IsExist(err) { 123 err = os.Remove(dstPbGoFile) 124 if err != nil { 125 fmt.Printf("Failed to delete file '%s'\n", dstPbGoFile) 126 continue 127 } 128 err = os.Rename(srcPbGoFile, dstPbGoFile) 129 if err != nil { 130 fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile) 131 } 132 continue 133 } 134 } 135 err = os.Rename(srcPbGoFile, dstPbGoFile) 136 if err != nil { 137 fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile) 138 } 139 continue 140 } 141 } 142 143 if err == nil { 144 err = os.RemoveAll(strings.Split(modulePath, "/")[0]) 145 if err != nil { 146 fmt.Println(err) 147 } 148 } 149 }