-
Notifications
You must be signed in to change notification settings - Fork 48
/
gh_task.go
238 lines (201 loc) · 4.66 KB
/
gh_task.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// +build gotask
package main
import (
"fmt"
"github.com/jingweno/gotask/tasking"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)
// NAME
// install-deps - install dependencies with go get
//
// DESCRIPTION
// Install dependencies with go get.
func TaskInstallDeps(t *tasking.T) {
deps := []string{
"github.com/laher/goxc",
}
for _, dep := range deps {
t.Logf("Installing %s\n", dep)
err := t.Exec("go get", dep)
if err != nil {
t.Fatalf("Can't download dependency %s", err)
}
}
}
// NAME
// package - cross compile gh and package it
//
// DESCRIPTION
// Cross compile gh and package it into PWD/target
func TaskPackage(t *tasking.T) {
gopath, err := ioutil.TempDir("", "gh-build")
os.Setenv("GOPATH", gopath)
t.Logf("GOPATH=%s\n", gopath)
path := fmt.Sprintf("%s%c%s", filepath.Join(gopath, "bin"), os.PathListSeparator, os.Getenv("PATH"))
os.Setenv("PATH", path)
t.Logf("PATH=%s\n", path)
t.Logf("Packaging for %s...\n", runtime.GOOS)
t.Log("Installing dependencies...")
TaskInstallDeps(t)
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
ghPath := filepath.Join(gopath, "src", "github.com", "jingweno", "gh")
t.Logf("Copying source from %s to %s\n", pwd, ghPath)
err = copyDir(pwd, ghPath)
if err != nil {
t.Fatal(err)
}
err = os.Chdir(ghPath)
if err != nil {
t.Fatal(err)
}
t.Log("Cross-compiling...")
godepPath := filepath.Join(ghPath, "Godeps", "_workspace")
gopath = fmt.Sprintf("%s%c%s", gopath, os.PathListSeparator, godepPath)
os.Setenv("GOPATH", gopath)
TaskCrossCompile(t)
source := filepath.Join(ghPath, "target")
target := filepath.Join(pwd, "target")
t.Logf("Copying build artifacts from %s to %s...\n", source, target)
err = mkdirAll(target)
if err != nil {
t.Fatal(err)
}
err = copyBuildArtifacts(source, target)
if err != nil {
t.Fatal(err)
}
}
// NAME
// bottle - build homebrew bottle for gh
//
// DESCRIPTION
// Build homebrew bottle for gh into PWD/target.
func TaskBottle(t *tasking.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
target := filepath.Join(pwd, "target")
err = mkdirAll(target)
if err != nil {
t.Fatal(err)
}
err = t.Exec("brew", "list", "gh")
if err == nil {
err := t.Exec("brew", "uninstall", "gh")
if err != nil {
t.Fatal(err)
}
}
err = t.Exec("brew", "install", "--build-from-source", "--build-bottle", "gh")
if err != nil {
t.Fatal(err)
}
err = os.Chdir(target)
if err != nil {
t.Fatal(err)
}
err = t.Exec("brew", "bottle", "gh")
if err != nil {
t.Fatal(err)
}
}
// NAME
// cross-compile - cross-compiles gh for current platform.
//
// DESCRIPTION
// Cross-compiles gh for current platform. Build artifacts will be in target/VERSION
func TaskCrossCompile(t *tasking.T) {
t.Logf("Cross-compiling gh for %s...\n", runtime.GOOS)
t.Logf("GOPATH=%s\n", os.Getenv("GOPATH"))
err := t.Exec("goxc", "-wd=.", "-os="+runtime.GOOS, "-c="+runtime.GOOS)
if err != nil {
t.Fatalf("Can't cross-compile gh: %s\n", err)
}
}
func copyBuildArtifacts(srcDir, destDir string) error {
artifacts := findBuildArtifacts(srcDir)
for _, artifact := range artifacts {
target := filepath.Join(destDir, filepath.Base(artifact))
fmt.Printf("Copying %s to %s\n", artifact, target)
err := copyFile(artifact, target)
if err != nil {
return err
}
}
return nil
}
func copyFile(source, dest string) error {
sf, err := os.Open(source)
if err != nil {
return err
}
defer sf.Close()
df, err := os.Create(dest)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
if err == nil {
si, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, si.Mode())
}
}
return err
}
func copyDir(source, dest string) (err error) {
fi, err := os.Stat(source)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("Source is not a directory")
}
_, err = os.Open(dest)
if !os.IsNotExist(err) {
return fmt.Errorf("Destination already exists")
}
err = os.MkdirAll(dest, fi.Mode())
if err != nil {
return err
}
entries, err := ioutil.ReadDir(source)
for _, entry := range entries {
sfp := filepath.Join(source, entry.Name())
dfp := filepath.Join(dest, entry.Name())
if entry.IsDir() {
err = copyDir(sfp, dfp)
if err != nil {
return err
}
} else {
err = copyFile(sfp, dfp)
if err != nil {
return err
}
}
}
return
}
func findBuildArtifacts(root string) (artifacts []string) {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == ".deb" || ext == ".zip" || ext == ".gz" {
artifacts = append(artifacts, path)
}
return nil
})
return
}
func mkdirAll(dir string) error {
return os.MkdirAll(dir, 0777)
}