You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.go 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package magefiles
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/magefile/mage/sh"
  9. cp "github.com/otiai10/copy"
  10. )
  11. var Global = struct {
  12. OS string
  13. Arch string
  14. BuildRoot string
  15. }{
  16. Arch: "amd64",
  17. }
  18. type BuildArgs struct {
  19. OutputName string
  20. OutputDir string
  21. AssetsDir string
  22. EntryFile string
  23. }
  24. type goBuildArgs struct {
  25. Env map[string]string
  26. OutputExt string
  27. }
  28. func Build(args BuildArgs) error {
  29. buildRoot := Global.BuildRoot
  30. if buildRoot == "" {
  31. buildRoot = "build"
  32. }
  33. fullOutputDir, err := filepath.Abs(filepath.Join(buildRoot, args.OutputDir))
  34. if err != nil {
  35. return err
  36. }
  37. goBuildArgs, err := makeGoBuildArgs()
  38. if err != nil {
  39. return err
  40. }
  41. binPath := filepath.Join(fullOutputDir, args.OutputName+goBuildArgs.OutputExt)
  42. fmt.Printf("building to %s\n", binPath)
  43. goCmdArgs := []string{"build", "-o", binPath}
  44. if args.EntryFile != "" {
  45. goCmdArgs = append(goCmdArgs, args.EntryFile)
  46. }
  47. err = sh.RunWith(goBuildArgs.Env, "go", goCmdArgs...)
  48. if err != nil {
  49. return err
  50. }
  51. if args.AssetsDir != "" {
  52. outputAssetsPath := fullOutputDir
  53. fmt.Printf("copying asset to %s\n", outputAssetsPath)
  54. return CopyAssets(args.AssetsDir, outputAssetsPath)
  55. }
  56. return nil
  57. }
  58. func makeGoBuildArgs() (goBuildArgs, error) {
  59. args := goBuildArgs{
  60. Env: make(map[string]string),
  61. }
  62. if Global.OS == "win" {
  63. args.OutputExt = ".exe"
  64. args.Env["CGO_ENABLED"] = "0"
  65. args.Env["GOOS"] = "windows"
  66. } else if Global.OS == "linux" {
  67. args.OutputExt = ""
  68. args.Env["CGO_ENABLED"] = "0"
  69. args.Env["GOOS"] = "linux"
  70. } else if Global.OS != "" {
  71. return goBuildArgs{}, fmt.Errorf("unknow os type: %s", Global.OS)
  72. }
  73. if Global.Arch == "amd64" {
  74. args.Env["GOARCH"] = "amd64"
  75. } else if Global.Arch == "arm64" {
  76. args.Env["GOARCH"] = "arm64"
  77. } else if Global.Arch != "" {
  78. return goBuildArgs{}, fmt.Errorf("unknow arch type: %s", Global.Arch)
  79. }
  80. var pltParts []string
  81. if Global.OS != "" {
  82. pltParts = append(pltParts, Global.OS)
  83. }
  84. if Global.Arch != "" {
  85. pltParts = append(pltParts, Global.Arch)
  86. }
  87. if len(pltParts) == 0 {
  88. fmt.Print("building platform is not set, will build for current machine.\n")
  89. } else {
  90. fmt.Printf("building for %s.\n", strings.Join(pltParts, "-"))
  91. }
  92. return args, nil
  93. }
  94. func CopyAssets(assrtDir string, targetDir string) error {
  95. info, err := os.Stat(assrtDir)
  96. if errors.Is(err, os.ErrNotExist) || !info.IsDir() {
  97. return nil
  98. }
  99. return cp.Copy(assrtDir, targetDir)
  100. }