parsemessages.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "regexp"
  8. "strings"
  9. filepath "path/filepath"
  10. )
  11. func main() {
  12. if len(os.Args) < 2 {
  13. os.Exit(1)
  14. }
  15. multi := false
  16. if len(os.Args) > 2{
  17. if os.Args[2] == "MULTI" {
  18. multi = true
  19. }
  20. }
  21. file, err := os.Open(os.Args[1])
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. defer file.Close()
  26. messageFinder, err := regexp.Compile("^message\\s+([a-zA-Z0-9_]+)")
  27. if err != nil {
  28. log.Fatalf("Invalid regexp %s\n", err)
  29. }
  30. serviceFinder, err := regexp.Compile("^service\\s+([a-zA-Z0-9_]+)")
  31. if err != nil {
  32. log.Fatalf("Invalid regexp %s\n", err)
  33. }
  34. scanner := bufio.NewScanner(file)
  35. if multi {
  36. for scanner.Scan() {
  37. capture := messageFinder.FindStringSubmatch(scanner.Text())
  38. if len(capture) == 2 {
  39. fmt.Printf("%s.h;", strings.ToLower(capture[1]))
  40. }
  41. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  42. if len(capture) == 2 {
  43. fmt.Printf("%sclient.h;", strings.ToLower(capture[1]))
  44. }
  45. }
  46. } else {
  47. //Singlefile version
  48. basename := strings.TrimSuffix(os.Args[1], filepath.Ext(os.Args[1]))
  49. messageFound := false
  50. serviceFound := false
  51. for scanner.Scan() {
  52. capture := messageFinder.FindStringSubmatch(scanner.Text())
  53. if len(capture) == 2 {
  54. messageFound = true
  55. }
  56. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  57. if len(capture) == 2 {
  58. serviceFound = true
  59. }
  60. }
  61. if messageFound {
  62. fmt.Printf("%s.qpb.h;", basename)
  63. }
  64. if serviceFound {
  65. fmt.Printf("%s_grpc.qpb.h;", basename)
  66. }
  67. }
  68. if err := scanner.Err(); err != nil {
  69. log.Fatal(err)
  70. }
  71. }