-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.go
85 lines (67 loc) · 1.87 KB
/
build.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
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"os/exec"
)
// This is a build script that produces the assets for a release.
// Use the following command to execute the script:
// go run build.go
func main() {
folderName := "build"
check(os.MkdirAll(folderName, 0777))
check(os.Chdir("aocdl"))
build("windows", "amd64", "", "aocdl.exe", "aocdl-windows.zip", folderName)
build("darwin", "amd64", "", "aocdl", "aocdl-macos.zip", folderName)
build("linux", "amd64", "", "aocdl", "aocdl-linux-amd64.zip", folderName)
build("linux", "arm", "6", "aocdl", "aocdl-armv6.zip", folderName)
}
func build(goos, goarch, goarm string, binaryName, packageName, folderName string) {
fullBinaryName := fmt.Sprintf("../%s/%s", folderName, binaryName)
fullPackageName := fmt.Sprintf("../%s/%s", folderName, packageName)
cmd := exec.Command("go", "build", "-o", fullBinaryName)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("GOOS=%s", goos))
cmd.Env = append(cmd.Env, fmt.Sprintf("GOARCH=%s", goarch))
cmd.Env = append(cmd.Env, fmt.Sprintf("GOARM=%s", goarm))
check(cmd.Run())
check(createZip(fullBinaryName, fullPackageName))
check(os.Remove(fullBinaryName))
}
func createZip(fullBinaryName, fullPackageName string) error {
zipFile, err := os.Create(fullPackageName)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
binaryFile, err := os.Open(fullBinaryName)
if err != nil {
return err
}
defer binaryFile.Close()
info, err := binaryFile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Deflate
header.SetMode(0755)
fileWriter, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(fileWriter, binaryFile)
return err
}
func check(err error) {
if err != nil {
panic(err)
}
}