main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path"
  8. "strings"
  9. "go-common/app/tool/warden/generator"
  10. "go-common/app/tool/warden/goparser"
  11. "go-common/app/tool/warden/types"
  12. )
  13. const (
  14. // GoCommon .
  15. GoCommon = "go-common"
  16. )
  17. var (
  18. name string
  19. dir string
  20. recvName string
  21. workDir string
  22. protoOut string
  23. csCode string
  24. goPackage string
  25. protoPackage string
  26. ignoreTypeError bool
  27. noprotoc bool
  28. importPaths string
  29. )
  30. func init() {
  31. flag.StringVar(&name, "name", "", "service name")
  32. flag.StringVar(&dir, "dir", "service", "service go code dir")
  33. flag.StringVar(&recvName, "recv", "Service", "receiver name")
  34. flag.StringVar(&workDir, "workdir", ".", "workdir")
  35. flag.StringVar(&csCode, "cs-code", "server/grpc", "server code directory")
  36. flag.StringVar(&protoOut, "proto-out", "api/api.proto", "proto file save path")
  37. flag.StringVar(&goPackage, "go-package", "", "go-package")
  38. flag.StringVar(&protoPackage, "proto-package", "", "proto-package")
  39. flag.BoolVar(&ignoreTypeError, "ignore-type-error", true, "ignore type error")
  40. flag.BoolVar(&noprotoc, "noprotoc", false, "don't run protoc")
  41. flag.StringVar(&importPaths, "proto-path", defaultImportPath(), "specify the directory in which to search for imports.")
  42. }
  43. func defaultImportPath() string {
  44. for _, goPath := range strings.Split(os.Getenv("GOPATH"), ":") {
  45. fixPath := path.Join(goPath, "src", GoCommon)
  46. if _, err := os.Stat(fixPath); err == nil {
  47. return fixPath
  48. }
  49. }
  50. return ""
  51. }
  52. func main() {
  53. var err error
  54. if !flag.Parsed() {
  55. flag.Parse()
  56. }
  57. if name == "" {
  58. log.Fatal("service name required")
  59. }
  60. var servicePackage string
  61. servicePackage, err = goparser.GoPackage(dir)
  62. if err != nil {
  63. log.Fatalf("auto detect gopackage error %s", err)
  64. }
  65. if goPackage == "" {
  66. // auto set go package
  67. goPackage = path.Join(path.Dir(servicePackage), csCode)
  68. }
  69. if protoPackage == "" {
  70. log.Fatal("proto package name required")
  71. }
  72. var spec *types.ServiceSpec
  73. spec, err = goparser.Parse(name, dir, recvName, workDir)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. var paths []string
  78. if importPaths != "" {
  79. paths = strings.Split(importPaths, ",")
  80. }
  81. options := &generator.ServiceProtoOptions{
  82. GoPackage: goPackage,
  83. ProtoPackage: protoPackage,
  84. IgnoreType: ignoreTypeError,
  85. ImportPaths: paths,
  86. }
  87. protoFile := path.Join(workDir, protoOut)
  88. if err = os.MkdirAll(path.Dir(protoFile), 0755); err != nil {
  89. log.Print(err)
  90. }
  91. var protoFp *os.File
  92. protoFp, err = os.OpenFile(protoFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. defer protoFp.Close()
  97. if err := generator.GenServiceProto(protoFp, spec, options); err != nil {
  98. log.Fatal(err)
  99. }
  100. if !noprotoc {
  101. if err := generator.Protoc(protoFile, "", "", paths); err != nil {
  102. log.Fatal(err)
  103. }
  104. }
  105. csOptions := &generator.GenCSCodeOptions{
  106. PbPackage: path.Join(path.Dir(servicePackage), path.Dir(protoOut)),
  107. RecvName: recvName,
  108. RecvPackage: servicePackage,
  109. }
  110. if err := generator.GenCSCode(csCode, spec, csOptions); err != nil {
  111. log.Fatal(err)
  112. }
  113. fmt.Printf(`
  114. 🍺 (゜-゜)つロ 干杯~ !
  115. proto file: %s
  116. server: %s
  117. `, protoFile, path.Join(csCode, "server.go"))
  118. }