translate.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package got
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  10. )
  11. var (
  12. dependenceMethodDeclared = map[string]bool{}
  13. )
  14. func Translate(p *Project) (err error) {
  15. handlers := map[string]func(*Project) error {
  16. "CreateMainFile":CreateMainFile,
  17. "CreateMetricFiles":CreateMetricFiles,
  18. "createParamsFile":createParamsFile,
  19. "CreateEnvironment":CreateEnvironment,
  20. "CreateVariables":CreateVariables,
  21. "GenSchemas":GenSchemas,
  22. "GenResources":GenResources,
  23. "GenMiddlewares":GenMiddlewares,
  24. }
  25. for stage, fn := range handlers {
  26. if err = fn(p); err != nil {
  27. return fmt.Errorf("[%s] %s",stage,err.Error())
  28. }
  29. }
  30. return include(p)
  31. }
  32. func createSymbolicLinks(p *Project) func(string, os.FileInfo, error) error {
  33. var (
  34. basedir = p.Paths.Include("/go")
  35. fileContent []byte
  36. )
  37. return func(sourcePath string, fileInfo os.FileInfo, errX error) (err error) {
  38. if errX != nil {
  39. fmt.Printf("error 「%v」 at a path 「%q」\n", errX, sourcePath)
  40. return errX
  41. }
  42. relativePath := strings.Replace(sourcePath, basedir, "", 1)
  43. if relativePath == "" {
  44. return
  45. }
  46. if fileInfo.IsDir() {
  47. os.MkdirAll(p.Paths.Build("/%s/%s", p.Package, relativePath), 0777)
  48. } else {
  49. if fileContent, err = ioutil.ReadFile(sourcePath); err != nil {
  50. return
  51. }
  52. if err = ioutil.WriteFile(
  53. p.Paths.Build("/%s/%s", p.Package, relativePath),
  54. fileContent,
  55. 0777,
  56. ); err != nil {
  57. return
  58. }
  59. }
  60. return nil
  61. }
  62. }
  63. func include(p *Project) error {
  64. var err error
  65. delete := fmt.Sprintf("find %s -type l -delete", p.Paths.Build(""))
  66. if err = exec.Command("sh", "-c", delete).Run(); err != nil {
  67. fmt.Println("include error ", err)
  68. }
  69. if err = filepath.Walk(p.Paths.Include("/go"), createSymbolicLinks(p)); err != nil {
  70. return err
  71. }
  72. return err
  73. }