github.com/EagleQL/Xray-core@v1.4.3/infra/vprotogen/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"runtime"
    10  	"strings"
    11  
    12  	"github.com/xtls/xray-core/common"
    13  	"github.com/xtls/xray-core/core"
    14  )
    15  
    16  var directory = flag.String("pwd", "", "Working directory of Xray vprotogen.")
    17  
    18  func main() {
    19  	flag.Usage = func() {
    20  		fmt.Fprintf(flag.CommandLine.Output(), "Usage of vprotogen:\n")
    21  		flag.PrintDefaults()
    22  	}
    23  	flag.Parse()
    24  
    25  	if !filepath.IsAbs(*directory) {
    26  		pwd, wdErr := os.Getwd()
    27  		if wdErr != nil {
    28  			fmt.Println("Can not get current working directory.")
    29  			os.Exit(1)
    30  		}
    31  		*directory = filepath.Join(pwd, *directory)
    32  	}
    33  
    34  	pwd := *directory
    35  	GOBIN := common.GetGOBIN()
    36  	binPath := os.Getenv("PATH")
    37  	pathSlice := []string{binPath, GOBIN, pwd}
    38  	binPath = strings.Join(pathSlice, string(os.PathListSeparator))
    39  	os.Setenv("PATH", binPath)
    40  
    41  	EXE := ""
    42  	if runtime.GOOS == "windows" {
    43  		EXE = ".exe"
    44  	}
    45  	protoc := "protoc" + EXE
    46  
    47  	if path, err := exec.LookPath(protoc); err != nil {
    48  		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")
    49  		os.Exit(1)
    50  	} else {
    51  		protoc = path
    52  	}
    53  
    54  	protoFilesMap := make(map[string][]string)
    55  	walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
    56  		if err != nil {
    57  			fmt.Println(err)
    58  			return err
    59  		}
    60  
    61  		if info.IsDir() {
    62  			return nil
    63  		}
    64  
    65  		dir := filepath.Dir(path)
    66  		filename := filepath.Base(path)
    67  		if strings.HasSuffix(filename, ".proto") {
    68  			path = path[len(pwd)+1:]
    69  			protoFilesMap[dir] = append(protoFilesMap[dir], path)
    70  		}
    71  
    72  		return nil
    73  	})
    74  	if walkErr != nil {
    75  		fmt.Println(walkErr)
    76  		os.Exit(1)
    77  	}
    78  
    79  	for _, files := range protoFilesMap {
    80  		for _, relProtoFile := range files {
    81  			var args []string
    82  			if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
    83  				args = []string{"--gofast_out", pwd, "--gofast_opt", "paths=source_relative", "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
    84  			} else {
    85  				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}
    86  			}
    87  			args = append(args, relProtoFile)
    88  			cmd := exec.Command(protoc, args...)
    89  			cmd.Env = append(cmd.Env, os.Environ()...)
    90  			cmd.Dir = pwd
    91  			output, cmdErr := cmd.CombinedOutput()
    92  			if len(output) > 0 {
    93  				fmt.Println(string(output))
    94  			}
    95  			if cmdErr != nil {
    96  				fmt.Println(cmdErr)
    97  				os.Exit(1)
    98  			}
    99  		}
   100  	}
   101  }