translate.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package got
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  9. )
  10. var (
  11. OutputDirectory string
  12. CurrentDirectory string
  13. )
  14. func Translate(p *Project) (err error) {
  15. if err = initGlobalVars(p); err != nil {
  16. fmt.Println("error initGlobalVars")
  17. return
  18. }
  19. if err = CreateMainFile(p); err != nil {
  20. fmt.Println("error CreateMainFile")
  21. return
  22. }
  23. if err = createParamsFile(p); err != nil {
  24. fmt.Println("error createParamsFile")
  25. return
  26. }
  27. if err = CreateEnvironment(p); err != nil {
  28. fmt.Println("error CreateEnvironment")
  29. return
  30. }
  31. if err = CreateVariables(p); err != nil {
  32. fmt.Println("error CreateVariables")
  33. return
  34. }
  35. if err = GenSchemas(p); err != nil {
  36. fmt.Println("error GenSchemas")
  37. return
  38. }
  39. if err = GenResources(p); err != nil {
  40. fmt.Println("error GenResources")
  41. return
  42. }
  43. if err = GenMiddlewares(p); err != nil {
  44. fmt.Println("error GenMiddlewares")
  45. return
  46. }
  47. return include(p)
  48. }
  49. func createSymbolicLinks(basedir string) func(string, os.FileInfo, error) error {
  50. var (
  51. callback func(string, os.FileInfo, error) error
  52. relative = func(path string) string {
  53. return fmt.Sprintf("%s/%s%s", OutputDirectory, "v1", strings.Replace(filepath.Dir(path), basedir, "", 1))
  54. }
  55. )
  56. callback = func(pathX string, infoX os.FileInfo, errX error) (err error) {
  57. if errX != nil {
  58. fmt.Printf("error 「%v」 at a path 「%q」\n", errX, pathX)
  59. return errX
  60. }
  61. if infoX.IsDir() {
  62. relativedir := strings.Replace(pathX, basedir, "", 1)
  63. if relativedir != "" {
  64. // fmt.Println("crinado diretorio", fmt.Sprintf("%s/%s%s", OutputDirectory, "v1", relativedir))
  65. os.MkdirAll(fmt.Sprintf("%s/%s%s", OutputDirectory, "v1", relativedir), 0777)
  66. }
  67. } else {
  68. // fmt.Println("crinado link symbolico aqui", relative(pathX))
  69. os.Symlink(pathX, filepath.Join(relative(pathX), infoX.Name()))
  70. // fmt.Printf(" dir ------ : 「%v」\n", pathX)
  71. // fmt.Printf(" dir: 「%v」\n", strings.Replace(filepath.Dir(pathX), basedir, "", 1))
  72. // fmt.Printf(" file name 「%v」\n", infoX.Name())
  73. // fmt.Printf(" extenion: 「%v」\n", filepath.Ext(pathX))
  74. // fmt.Printf(" target: 「%v」\n", relative(pathX))
  75. // fmt.Sprintf("%s/%s%s", OutputDirectory, "v1", strings.Replace(filepath.Dir(pathX), basedir, "", 1)),
  76. }
  77. return nil
  78. }
  79. return callback
  80. }
  81. func include(p *Project) error {
  82. var err error
  83. if err = exec.Command("sh", "-c", fmt.Sprintf("find %s -type l -delete", OutputDirectory)).Run(); err != nil {
  84. fmt.Println("include error ", err)
  85. }
  86. basedir := fmt.Sprintf("%s/include/go", CurrentDirectory)
  87. if err = filepath.Walk(basedir, createSymbolicLinks(basedir)); err != nil {
  88. return err
  89. }
  90. // args := fmt.Sprintf("ln -st %s/include/go/* %s/%s/", CurrentDirectory, OutputDirectory, p.Package)
  91. // args := fmt.Sprintf("ln -st %s/include/go/* %s/%s/", CurrentDirectory, OutputDirectory, p.Package)
  92. // command := exec.Command("sh", "-c", args)
  93. // var out bytes.Buffer
  94. // var out []byte
  95. // // command.Stdout = &out
  96. // // if err = command.Run(); err != nil {
  97. // if out, err = command.Output(); err != nil {
  98. // fmt.Println("exec command error ", err, args, string(out))
  99. // }
  100. return err
  101. }
  102. func initGlobalVars(p *Project) error {
  103. var err error
  104. if CurrentDirectory, err = os.Getwd(); err != nil {
  105. return err
  106. }
  107. if OutputDirectory, err = filepath.Abs(CurrentDirectory + "/" + p.OutPath); err != nil {
  108. return err
  109. }
  110. return nil
  111. }