build_commands.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package common
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os/exec"
  6. "regexp"
  7. "strings"
  8. "github.com/jeremywohl/flatten"
  9. )
  10. type Command struct {
  11. Id string `json:"id"`
  12. Cmd string `json:"cmd"`
  13. IgnoreErros bool `json:"ignore.errors"`
  14. }
  15. type BuildSet struct {
  16. Modes map[string]*BuildMode `json:"modes"`
  17. Cmd string `json:"cmd"`
  18. Workdir string `json:"workdir"`
  19. Steps []*Command `json:"steps"`
  20. Options *BuildOptions `json:"options"`
  21. }
  22. func (b *BuildSet) Commands() []*Command {
  23. var (
  24. mode *BuildMode
  25. ok, ignore bool
  26. steps = []*Command{}
  27. modeKey = b.Options.Mode
  28. )
  29. if modeKey == "" {
  30. modeKey = "default"
  31. }
  32. if mode, ok = b.Modes[modeKey]; !ok {
  33. panic(fmt.Sprintf("Build mode '%s' not defined", modeKey))
  34. }
  35. if (len(mode.Ignore) == 0) &&
  36. len(mode.ExecuteOnly) == 0 {
  37. return b.Steps
  38. }
  39. ignore = len(mode.Ignore) > 0
  40. if ignore && len(mode.ExecuteOnly) > 0 {
  41. panic("Ignore and ExecuteOnly are mutually exclusive")
  42. }
  43. ids := map[string]bool{}
  44. for _, v := range append(mode.Ignore, mode.ExecuteOnly...) {
  45. ids[v] = true
  46. }
  47. if ignore {
  48. for _, step := range b.Steps {
  49. if _, ok := ids[step.Id]; !ok {
  50. steps = append(steps, step)
  51. }
  52. }
  53. } else {
  54. for _, step := range b.Steps {
  55. if _, ok := ids[step.Id]; ok {
  56. steps = append(steps, step)
  57. }
  58. }
  59. }
  60. return steps
  61. }
  62. type BuildMode struct {
  63. Ignore []string `json:"ignore"`
  64. ExecuteOnly []string `json:"executeOnly"`
  65. }
  66. var (
  67. buildSet = &BuildSet{
  68. Steps: []*Command{},
  69. Modes: map[string]*BuildMode{
  70. "default": &BuildMode{},
  71. },
  72. }
  73. )
  74. func NewBuildOptions() *BuildOptions {
  75. return &BuildOptions{
  76. Mode: "default",
  77. }
  78. }
  79. func RunBuildCommads(project *Project, buildOptions *BuildOptions) (err error) {
  80. var (
  81. instance *exec.Cmd
  82. variables = map[string]interface{}{}
  83. variablesJson, cmd string
  84. step int
  85. out []byte
  86. buildfile = "build/build"
  87. mode = project.Mode
  88. )
  89. if out, err = json.Marshal(project); err != nil {
  90. return
  91. }
  92. if variablesJson, err = flatten.FlattenString(string(out), "project.", flatten.DotStyle); err != nil {
  93. return
  94. }
  95. if err = json.Unmarshal([]byte(variablesJson), &variables); err != nil {
  96. return
  97. }
  98. if buildOptions == nil {
  99. buildOptions = NewBuildOptions()
  100. }
  101. buildOptions.Parse()
  102. if err = JsonParseMode(buildfile, mode, &buildSet); err != nil {
  103. return
  104. }
  105. buildSet.Options = buildOptions
  106. fmt.Println("Running build commands... on mode ", mode)
  107. cmds := buildSet.Commands()
  108. total := len(cmds)
  109. for k, command := range cmds {
  110. if cmd, err = parseCommand(command, variables); err != nil {
  111. return
  112. }
  113. step = k + 1
  114. if buildOptions.IgnoreStep(step) {
  115. fmt.Printf("Step (%d / %d) - [ Skiped ] %s\n", step, total, cmd)
  116. continue
  117. }
  118. fmt.Printf("Step (%d / %d) - %s\n", step, total, cmd)
  119. instance = exec.Command("sh", "-c", cmd)
  120. instance.Dir = buildSet.Workdir
  121. if out, err = instance.CombinedOutput(); err != nil {
  122. fmt.Println(string(out))
  123. return
  124. }
  125. }
  126. return
  127. }
  128. var (
  129. expressionRegexp = regexp.MustCompile(`\{([\w\.]+)\}`)
  130. )
  131. func parseCommand(c *Command, variables map[string]interface{}) (string, error) {
  132. var (
  133. expression, replace, dotPathName string
  134. cmd = c.Cmd
  135. matches = expressionRegexp.FindAllStringSubmatch(cmd, -1)
  136. )
  137. for _, match := range matches {
  138. dotPathName = match[1]
  139. if value, exists := variables[dotPathName]; !exists || value == nil {
  140. return "", fmt.Errorf("Value for expression '%s' not defined", dotPathName)
  141. }
  142. expression = match[0]
  143. replace = fmt.Sprintf("%v", variables[dotPathName])
  144. cmd = strings.ReplaceAll(cmd, expression, replace)
  145. }
  146. return cmd, nil
  147. }