@@ -2,34 +2,99 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "go/build"
6
+ "io/ioutil"
5
7
"os"
6
8
"os/exec"
7
9
"path/filepath"
8
10
"runtime"
9
11
"strings"
10
-
11
- core "github.com/v2fly/v2ray-core/v4"
12
- "github.com/v2fly/v2ray-core/v4/common"
13
12
)
14
13
14
+ // envFile returns the name of the Go environment configuration file.
15
+ // Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
16
+ func envFile () (string , error ) {
17
+ if file := os .Getenv ("GOENV" ); file != "" {
18
+ if file == "off" {
19
+ return "" , fmt .Errorf ("GOENV=off" )
20
+ }
21
+ return file , nil
22
+ }
23
+ dir , err := os .UserConfigDir ()
24
+ if err != nil {
25
+ return "" , err
26
+ }
27
+ if dir == "" {
28
+ return "" , fmt .Errorf ("missing user-config dir" )
29
+ }
30
+ return filepath .Join (dir , "go" , "env" ), nil
31
+ }
32
+
33
+ // GetRuntimeEnv returns the value of runtime environment variable,
34
+ // that is set by running following command: `go env -w key=value`.
35
+ func GetRuntimeEnv (key string ) (string , error ) {
36
+ file , err := envFile ()
37
+ if err != nil {
38
+ return "" , err
39
+ }
40
+ if file == "" {
41
+ return "" , fmt .Errorf ("missing runtime env file" )
42
+ }
43
+ var data []byte
44
+ var runtimeEnv string
45
+ data , readErr := ioutil .ReadFile (file )
46
+ if readErr != nil {
47
+ return "" , readErr
48
+ }
49
+ envStrings := strings .Split (string (data ), "\n " )
50
+ for _ , envItem := range envStrings {
51
+ envItem = strings .TrimSuffix (envItem , "\r " )
52
+ envKeyValue := strings .Split (envItem , "=" )
53
+ if strings .EqualFold (strings .TrimSpace (envKeyValue [0 ]), key ) {
54
+ runtimeEnv = strings .TrimSpace (envKeyValue [1 ])
55
+ }
56
+ }
57
+ return runtimeEnv , nil
58
+ }
59
+
60
+ // GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
61
+ func GetGOBIN () string {
62
+ // The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
63
+ GOBIN := os .Getenv ("GOBIN" )
64
+ if GOBIN == "" {
65
+ var err error
66
+ // The one set by user by running `go env -w GOBIN=/path`
67
+ GOBIN , err = GetRuntimeEnv ("GOBIN" )
68
+ if err != nil {
69
+ // The default one that Golang uses
70
+ return filepath .Join (build .Default .GOPATH , "bin" )
71
+ }
72
+ if GOBIN == "" {
73
+ return filepath .Join (build .Default .GOPATH , "bin" )
74
+ }
75
+ return GOBIN
76
+ }
77
+ return GOBIN
78
+ }
79
+
15
80
func main () {
16
81
pwd , wdErr := os .Getwd ()
17
82
if wdErr != nil {
18
83
fmt .Println ("Can not get current working directory." )
19
84
os .Exit (1 )
20
85
}
21
86
22
- GOBIN := common . GetGOBIN ()
87
+ GOBIN := GetGOBIN ()
23
88
binPath := os .Getenv ("PATH" )
24
- pathSlice := []string {binPath , GOBIN , pwd }
89
+ pathSlice := []string {pwd , GOBIN , binPath }
25
90
binPath = strings .Join (pathSlice , string (os .PathListSeparator ))
26
91
os .Setenv ("PATH" , binPath )
27
92
28
- EXE := ""
93
+ suffix := ""
29
94
if runtime .GOOS == "windows" {
30
- EXE = ".exe"
95
+ suffix = ".exe"
31
96
}
32
- protoc := "protoc" + EXE
97
+ protoc := "protoc" + suffix
33
98
34
99
if path , err := exec .LookPath (protoc ); err != nil {
35
100
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" )
@@ -64,11 +129,13 @@ func main() {
64
129
65
130
for _ , files := range protoFilesMap {
66
131
for _ , relProtoFile := range files {
67
- var args []string
68
- if core .ProtoFilesUsingProtocGenGoFast [relProtoFile ] {
69
- args = []string {"--gofast_out" , pwd , "--gofast_opt" , "paths=source_relative" , "--plugin" , "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE }
70
- } else {
71
- 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 }
132
+ args := []string {
133
+ "--go_out" , pwd ,
134
+ "--go_opt" , "paths=source_relative" ,
135
+ "--go-grpc_out" , pwd ,
136
+ "--go-grpc_opt" , "paths=source_relative" ,
137
+ "--plugin" , "protoc-gen-go=" + filepath .Join (GOBIN , "protoc-gen-go" + suffix ),
138
+ "--plugin" , "protoc-gen-go-grpc=" + filepath .Join (GOBIN , "protoc-gen-go-grpc" + suffix ),
72
139
}
73
140
args = append (args , relProtoFile )
74
141
cmd := exec .Command (protoc , args ... )
0 commit comments