-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
101 lines (92 loc) · 1.9 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package lib
import (
"bytes"
"fmt"
"github.com/ggdream/compile/conf"
"github.com/ggdream/compile/pack"
"os"
"os/exec"
"strings"
)
func Cmd(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
var dst bytes.Buffer
cmd.Stdout = &dst
if err := cmd.Run(); err != nil {
return "", err
}
return dst.String(), nil
}
func CmdCompile(target string) error {
info := strings.Split(target, "/")
if err := os.Setenv("GOOS", info[0]); err != nil {
return err
}
if err := os.Setenv("GOARCH", info[1]); err != nil {
return err
}
path := fmt.Sprintf("dist/_temp/%s_%s/%s%s",
info[0],
info[1],
conf.NAME,
func() string {
switch info[0] {
case windows:
return ".exe"
default:
return ""
}
}(),
)
fmt.Printf(">>target: %s\n", target)
if msg, err := Cmd("go", "build", "-o", path); err != nil {
fmt.Printf("target: %s | err: %s\n", target, msg)
return err
}
fmt.Printf("<<target: %s\n", target)
var packer pack.Packer
switch info[0] {
case windows:
packer = &pack.ZipPacker{
DirName: path,
DstName: fmt.Sprintf("dist/%s-%s-%s.zip", conf.NAME, conf.VERSION, func() string {
switch info[1] {
case "386":
return "win32"
case "amd64":
return "win64"
default:
return info[1]
}
}()),
}
default:
packer = &pack.TgzPacker{
DirName: path,
DstName: fmt.Sprintf("dist/%s-%s-%s-%s.tgz", conf.NAME, conf.VERSION, func() string {
switch info[0] {
case darwin:
return "macos"
default:
return info[0]
}
}(), func() string {
switch info[1] {
case "386":
return "x86" // i386
case "amd64":
return "x86_64"
default:
return info[1]
}
}()),
}
}
fmt.Printf(">>compress: %s\n", target)
if err := packer.Pack(); err != nil {
fmt.Printf("compress: %s | err: %s\n", target, err.Error())
return err
}
fmt.Printf("<<compress: %s\n", target)
return nil
}