build.go 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "runtime"
  8. "strings"
  9. )
  10. var target = "../www/"
  11. func main() {
  12. fmt.Println("=============start==============")
  13. dir := flag.String("dir", "", "dir")
  14. flag.Parse()
  15. target += *dir + "/"
  16. fmt.Println("target_dir", target)
  17. os.RemoveAll(target + "static")
  18. os.Remove(target + "index.html")
  19. os.Remove(target + "favicon.ico")
  20. copyDir("./dist", target)
  21. }
  22. func FormatPath(s string) string {
  23. switch runtime.GOOS {
  24. case "windows":
  25. return strings.Replace(s, "/", "\\", -1)
  26. case "darwin", "linux":
  27. return strings.Replace(s, "\\", "/", -1)
  28. default:
  29. return s
  30. }
  31. }
  32. func copyDir(src string, dest string) {
  33. src = FormatPath(src)
  34. dest = FormatPath(dest)
  35. var cmd *exec.Cmd
  36. switch runtime.GOOS {
  37. case "windows":
  38. cmd = exec.Command("xcopy", src, dest, "/I", "/E")
  39. case "darwin", "linux":
  40. cmd = exec.Command("cp", "-R", src, dest)
  41. }
  42. outPut, e := cmd.Output()
  43. if e != nil {
  44. fmt.Println("copy error", e.Error())
  45. return
  46. }
  47. fmt.Println(string(outPut))
  48. }