build.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "strings"
  9. "github.com/kr/pty"
  10. "github.com/urfave/cli"
  11. )
  12. func runbazel(param ...string) {
  13. command := append([]string{"build", "--watchfs"}, param...)
  14. fmt.Println(command)
  15. cmd := exec.Command("bazel", command...)
  16. f, err := pty.Start(cmd)
  17. if err != nil {
  18. panic(err)
  19. }
  20. io.Copy(os.Stdout, f)
  21. }
  22. func bazelAction(c *cli.Context) error {
  23. pwd, err := os.Getwd()
  24. if err != nil {
  25. return err
  26. }
  27. index := strings.Index(pwd, "go-common")
  28. if index == -1 {
  29. fmt.Println("not in go-common")
  30. os.Exit(1)
  31. }
  32. result := strings.Split(pwd[index:], "/")
  33. runPath := strings.Join(result[1:], "/")
  34. if c.NArg() > 0 {
  35. param := []string{}
  36. for index := 0; index < c.NArg(); index++ {
  37. name := path.Join(runPath, path.Clean(c.Args().Get(index)))
  38. if name == "." {
  39. continue
  40. }
  41. if strings.HasSuffix(name, "/...") {
  42. param = append(param, "//"+name)
  43. } else {
  44. param = append(param, "//"+name+"/...")
  45. }
  46. }
  47. runbazel(param...)
  48. } else {
  49. if len(runPath) == 0 {
  50. runbazel("//app/...", "//library/...", "//vendor/...")
  51. } else {
  52. runbazel("//" + strings.Join(result[1:], "/") + "/...")
  53. }
  54. }
  55. return nil
  56. }