-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
346 lines (295 loc) · 7.41 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var (
gitMail = flag.String("git-author-mail", "[email protected]", "Git author mail")
gitName = flag.String("git-author-name", "cs3org-bot", "Git author name")
gitSSH = flag.Bool("git-ssh", false, "Use git protocol instead of https for cloning repos")
_pushGo = flag.Bool("push-go", false, "Push Go library to github.com/cs3org/go-cs3apis")
_pushPython = flag.Bool("push-python", false, "Push Python library to github.com/cs3org/python-cs3apis")
_pushJs = flag.Bool("push-js", false, "Push Js library to github.com/cs3org/js-cs3apis")
_pushNode = flag.Bool("push-node", false, "Push Node.js library to github.com/cs3org/node-cs3apis")
)
func init() {
flag.Parse()
}
func getProtoOS() string {
switch runtime.GOOS {
case "darwin":
return "osx"
case "linux":
return "linux"
default:
panic("no build procedure for " + runtime.GOOS)
}
}
func clone(repo, dir string) {
repo = getRepo(repo) // get git or https repo location
cmd := exec.Command("git", "clone", "--quiet", repo)
cmd.Dir = dir
run(cmd)
}
func checkout(branch, dir string) {
// See https://stackoverflow.com/questions/26961371/switch-on-another-branch-create-if-not-exists-without-checking-if-already-exi
cmd := exec.Command("bash", "-c", fmt.Sprintf("git checkout %s || git checkout -b %s", branch, branch))
cmd.Dir = dir
run(cmd)
}
func update(dir string) error {
cmd := exec.Command("git", "pull", "--quiet")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
return cmd.Run()
}
func isRepoDirty(repo string) bool {
cmd := exec.Command("git", "status", "-s")
cmd.Dir = repo
changes := runAndGet(cmd)
if changes != "" {
fmt.Println("repo is dirty")
fmt.Println(changes)
}
return changes != ""
}
func getCommitID(dir string) string {
if os.Getenv("BUILD_GIT_COMMIT") != "" {
return os.Getenv("BUILD_GIT_COMMIT")
}
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = dir
commit := runAndGet(cmd)
return commit
}
func getRepo(repo string) string {
if *gitSSH {
return fmt.Sprintf("[email protected]:%s", repo)
}
return fmt.Sprintf("https://github.com/%s", repo)
}
func commit(repo, msg string) {
// set correct author name and mail
cmd := exec.Command("git", "config", "user.email", *gitMail)
cmd.Dir = repo
run(cmd)
cmd = exec.Command("git", "config", "user.name", *gitName)
cmd.Dir = repo
run(cmd)
// check if repo is dirty
if !isRepoDirty(repo) {
// nothing to do
return
}
cmd = exec.Command("git", "add", ".")
cmd.Dir = repo
run(cmd)
cmd = exec.Command("git", "commit", "-m", msg)
cmd.Dir = repo
run(cmd)
}
func push(repo string) {
protoBranch := getGitBranch(".")
cmd := exec.Command("git", "push", "--set-upstream", "origin", protoBranch)
cmd.Dir = repo
run(cmd)
}
func getGitBranch(repo string) string {
// check if branch is provided by env variable
if os.Getenv("BUILD_GIT_BRANCH") != "" {
return os.Getenv("BUILD_GIT_BRANCH")
}
// obtain branch from repo
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = repo
branch := runAndGet(cmd)
return branch
}
// getVersionFromGit returns a version string that identifies the currently
// checked out git commit.
func getVersionFromGit(repodir string) string {
cmd := exec.Command("git", "describe",
"--long", "--tags", "--dirty", "--always")
cmd.Dir = repodir
out, err := cmd.Output()
if err != nil {
panic(fmt.Sprintf("git describe returned error: %v\n", err))
}
version := strings.TrimSpace(string(out))
return version
}
func run(cmd *exec.Cmd) {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stdout = mw
cmd.Stderr = mw
err := cmd.Run()
fmt.Println(cmd.Dir, cmd.Args)
fmt.Println(b.String())
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
}
func runAndGet(cmd *exec.Cmd) string {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stderr = mw
out, err := cmd.Output()
fmt.Println(cmd.Dir, cmd.Args)
fmt.Println(b.String())
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
return strings.TrimSpace(string(out))
}
// Works with Go 1.8+
// https://stackoverflow.com/questions/32649770/how-to-get-current-gopath-from-code
func getGoPath() string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return gopath
}
func sed(dir, suffix, old, new string) {
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, suffix) {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
newData := strings.ReplaceAll(string(data), old, new)
err = ioutil.WriteFile(path, []byte(newData), 0)
if err != nil {
return err
}
}
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func find(patterns ...string) []string {
var files []string
for _, p := range patterns {
fs, err := filepath.Glob(p)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
files = append(files, fs...)
}
return files
}
func findProtos() []string {
return find("cs3/*/*.proto", "cs3/*/*/*.proto", "cs3/*/*/*/*.proto")
}
func findFolders() []string {
var folders []string
err := filepath.Walk("cs3",
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if info.IsDir() {
folders = append(folders, path)
}
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return folders
}
func generate() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
fmt.Println("Starting generation of protobuf language bindings ...")
fmt.Printf("current working directory: %s\n", cwd)
cmd := exec.Command("git", "config", "--global", "--add", "safe.directory", cwd)
run(cmd)
// Remove build dir
os.RemoveAll("build")
os.MkdirAll("build", 0755)
languages := []string{"go", "js", "node", "python"}
// prepare language git repos
for _, l := range languages {
target := fmt.Sprintf("%s-cs3apis", l)
fmt.Println("cloning repo for " + target)
// Clone Go repo and set branch to current branch
clone("cs3org/"+target, "build")
protoBranch := getGitBranch(".")
targetBranch := getGitBranch("build/" + target)
fmt.Printf("Proto branch: %s\n%s branch: %s\n", l, protoBranch, targetBranch)
if targetBranch != protoBranch {
checkout(protoBranch, "build/"+target)
}
// remove leftovers (existing defs)
os.RemoveAll(fmt.Sprintf("build/%s/cs3", target))
}
fmt.Println("Generating ...")
cmd = exec.Command("buf", "generate")
run(cmd)
for _, l := range languages {
target := fmt.Sprintf("%s-cs3apis", l)
fmt.Println("Commiting changes for " + target)
if !isRepoDirty("build/" + target) {
fmt.Println("Repo is clean, nothing to do")
}
// get proto repo commit id
hash := getCommitID(".")
repo := "build/" + target
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
commit(repo, msg)
}
fmt.Println("Generation done!")
}
func pushPython() {
push("build/python-cs3apis")
}
func pushGo() {
push("build/go-cs3apis")
}
func pushJS() {
push("build/js-cs3apis")
}
func pushNode() {
push("build/node-cs3apis")
}
func main() {
generate()
if *_pushGo {
fmt.Println("Pushing Go ...")
pushGo()
}
if *_pushPython {
fmt.Println("Pushing Python ...")
pushPython()
}
if *_pushJs {
fmt.Println("Pushing Js ...")
pushJS()
}
if *_pushNode {
fmt.Println("Pushing Node.js ...")
pushNode()
}
}