2019-02-10 13:04:11 -05:00
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
2020-08-27 04:37:59 -04:00
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
2019-02-10 13:04:11 -05:00
)
func main ( ) {
2020-08-27 04:37:59 -04:00
pwd , wdErr := os . Getwd ( )
if wdErr != nil {
fmt . Println ( "Can not get current working directory." )
os . Exit ( 1 )
}
2019-02-10 13:04:11 -05:00
2020-08-27 04:37:59 -04:00
GOBIN := common . GetGOBIN ( )
2020-10-28 21:27:05 -04:00
binPath := os . Getenv ( "PATH" )
pathSlice := [ ] string { binPath , GOBIN , pwd }
binPath = strings . Join ( pathSlice , string ( os . PathListSeparator ) )
os . Setenv ( "PATH" , binPath )
2020-10-19 10:39:51 -04:00
EXE := ""
if runtime . GOOS == "windows" {
EXE = ".exe"
}
2020-10-19 11:20:19 -04:00
protoc := "protoc" + EXE
2020-10-20 22:30:16 -04:00
if path , err := exec . LookPath ( protoc ) ; err != nil {
2020-10-28 21:27:05 -04:00
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" )
2020-10-19 11:20:19 -04:00
os . Exit ( 1 )
2020-10-20 22:30:16 -04:00
} else {
protoc = path
2020-10-19 11:20:19 -04:00
}
2020-10-19 10:39:51 -04:00
2020-08-27 04:37:59 -04:00
protoFilesMap := make ( map [ string ] [ ] string )
walkErr := filepath . Walk ( "./" , func ( path string , info os . FileInfo , err error ) error {
2019-02-10 13:04:11 -05:00
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 , ".proto" ) {
2020-08-27 04:37:59 -04:00
protoFilesMap [ dir ] = append ( protoFilesMap [ dir ] , path )
2019-02-10 13:04:11 -05:00
}
return nil
} )
2020-08-27 04:37:59 -04:00
if walkErr != nil {
fmt . Println ( walkErr )
os . Exit ( 1 )
}
2019-02-10 13:04:11 -05:00
2020-08-27 04:37:59 -04:00
for _ , files := range protoFilesMap {
for _ , relProtoFile := range files {
var args [ ] string
2020-10-03 22:05:00 -04:00
if core . ProtoFilesUsingProtocGenGoFast [ relProtoFile ] {
2020-10-28 21:27:05 -04:00
args = [ ] string { "--gofast_out" , pwd , "--gofast_opt" , "paths=source_relative" , "--plugin" , "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE }
2020-08-24 08:10:26 -04:00
} else {
2020-10-28 21:27:05 -04:00
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 }
2020-08-24 08:10:26 -04:00
}
2020-08-27 04:37:59 -04:00
args = append ( args , relProtoFile )
2020-08-24 08:10:26 -04:00
cmd := exec . Command ( protoc , args ... )
cmd . Env = append ( cmd . Env , os . Environ ( ) ... )
2020-08-27 04:37:59 -04:00
output , cmdErr := cmd . CombinedOutput ( )
2020-08-24 08:10:26 -04:00
if len ( output ) > 0 {
fmt . Println ( string ( output ) )
}
2020-08-27 04:37:59 -04:00
if cmdErr != nil {
fmt . Println ( cmdErr )
os . Exit ( 1 )
}
}
}
2019-02-10 13:04:11 -05:00
}