main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // +build generate
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. var protocMap = map[string]string{
  12. "windows": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "windows", "protoc.exe"),
  13. "darwin": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "macos", "protoc"),
  14. "linux": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "linux", "protoc"),
  15. }
  16. func main() {
  17. protofiles := make(map[string][]string)
  18. protoc := protocMap[runtime.GOOS]
  19. gosrc := filepath.Join(os.Getenv("GOPATH"), "src")
  20. filepath.Walk(filepath.Join(gosrc, "v2ray.com", "core"), func(path string, info os.FileInfo, err error) error {
  21. if err != nil {
  22. fmt.Println(err)
  23. return err
  24. }
  25. if info.IsDir() {
  26. return nil
  27. }
  28. dir := filepath.Dir(path)
  29. filename := filepath.Base(path)
  30. if strings.HasSuffix(filename, ".proto") {
  31. protofiles[dir] = append(protofiles[dir], path)
  32. }
  33. return nil
  34. })
  35. for _, files := range protofiles {
  36. args := []string{"--proto_path", gosrc, "--go_out", gosrc}
  37. args = append(args, files...)
  38. cmd := exec.Command(protoc, args...)
  39. cmd.Env = append(cmd.Env, os.Environ()...)
  40. output, err := cmd.CombinedOutput()
  41. if len(output) > 0 {
  42. fmt.Println(string(output))
  43. }
  44. if err != nil {
  45. fmt.Println(err)
  46. }
  47. }
  48. }