parsemessages.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path"
  8. filepath "path/filepath"
  9. "regexp"
  10. "strings"
  11. )
  12. func main() {
  13. if len(os.Args) < 2 {
  14. os.Exit(1)
  15. }
  16. multi := false
  17. folder := false
  18. for i := 2; i < len(os.Args); i++ {
  19. if os.Args[i] == "MULTI" {
  20. multi = true
  21. folder = true //For multi-file generation folder-based approach is enabled by default and could not be switched off
  22. }
  23. if os.Args[i] == "FOLDER" {
  24. folder = true
  25. }
  26. }
  27. file, err := os.Open(os.Args[1])
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. defer file.Close()
  32. messageFinder, err := regexp.Compile("^message\\s+([a-zA-Z0-9_]+)")
  33. if err != nil {
  34. log.Fatalf("Invalid regexp %s\n", err)
  35. }
  36. serviceFinder, err := regexp.Compile("^service\\s+([a-zA-Z0-9_]+)")
  37. if err != nil {
  38. log.Fatalf("Invalid regexp %s\n", err)
  39. }
  40. packageFinder, err := regexp.Compile("^package\\s+(.+);")
  41. if err != nil {
  42. log.Fatalf("Invalid regexp %s\n", err)
  43. }
  44. scanner := bufio.NewScanner(file)
  45. fullpath := ""
  46. for scanner.Scan() {
  47. capture := packageFinder.FindStringSubmatch(scanner.Text())
  48. if len(capture) == 2 {
  49. packages := strings.Split(capture[1], ".")
  50. fullpath = strings.Join(packages, "/")
  51. fullpath += "/"
  52. break
  53. }
  54. }
  55. newoffset, err := file.Seek(0, 0)
  56. if err != nil || newoffset != 0 {
  57. log.Fatal(err)
  58. }
  59. scanner = bufio.NewScanner(file)
  60. if multi {
  61. for scanner.Scan() {
  62. capture := messageFinder.FindStringSubmatch(scanner.Text())
  63. if len(capture) == 2 {
  64. fmt.Printf("%s%s.h;", fullpath, strings.ToLower(capture[1]))
  65. }
  66. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  67. if len(capture) == 2 {
  68. fmt.Printf("%s%sclient.h;", fullpath, strings.ToLower(capture[1]))
  69. }
  70. }
  71. } else {
  72. //Singlefile version
  73. enumFinder, err := regexp.Compile("^enum\\s+([a-zA-Z0-9_]+)")
  74. if err != nil {
  75. log.Fatalf("Invalid regexp %s\n", err)
  76. }
  77. basename := path.Base(os.Args[1])
  78. basename = strings.TrimSuffix(basename, filepath.Ext(basename))
  79. messageFound := false
  80. serviceFound := false
  81. for scanner.Scan() {
  82. capture := messageFinder.FindStringSubmatch(scanner.Text())
  83. if len(capture) == 2 {
  84. messageFound = true
  85. }
  86. capture = enumFinder.FindStringSubmatch(scanner.Text())
  87. if len(capture) == 2 {
  88. messageFound = true
  89. }
  90. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  91. if len(capture) == 2 {
  92. serviceFound = true
  93. }
  94. }
  95. if messageFound {
  96. if folder {
  97. fmt.Printf("%s%s.qpb.h;", fullpath, basename)
  98. } else {
  99. fmt.Printf("%s.qpb.h;", basename)
  100. }
  101. }
  102. if serviceFound {
  103. if folder {
  104. fmt.Printf("%s%s_grpc.qpb.h;", fullpath, basename)
  105. } else {
  106. fmt.Printf("%s_grpc.qpb.h;", basename)
  107. }
  108. }
  109. }
  110. if err := scanner.Err(); err != nil {
  111. log.Fatal(err)
  112. }
  113. }