-
Notifications
You must be signed in to change notification settings - Fork 40
/
rerun.go
287 lines (241 loc) · 5.88 KB
/
rerun.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
// Copyright 2013 The rerun AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"github.com/howeyc/fsnotify"
"go/build"
"log"
"os"
"os/exec"
"path"
"path/filepath"
)
var (
do_tests = flag.Bool("test", false, "Run tests (before running program)")
do_build = flag.Bool("build", false, "Build program")
never_run = flag.Bool("no-run", false, "Do not run")
race_detector = flag.Bool("race", false, "Run program and tests with the race detector")
)
func install(buildpath, lastError string) (installed bool, errorOutput string, err error) {
cmdline := []string{"go", "get"}
if *race_detector {
cmdline = append(cmdline, "-race")
}
cmdline = append(cmdline, buildpath)
// setup the build command, use a shared buffer for both stdOut and stdErr
cmd := exec.Command("go", cmdline[1:]...)
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Run()
// when there is any output, the go command failed.
if buf.Len() > 0 {
errorOutput = buf.String()
if errorOutput != lastError {
fmt.Print(errorOutput)
}
err = errors.New("compile error")
return
}
// all seems fine
installed = true
return
}
func test(buildpath string) (passed bool, err error) {
cmdline := []string{"go", "test"}
if *race_detector {
cmdline = append(cmdline, "-race")
}
cmdline = append(cmdline, "-v", buildpath)
// setup the build command, use a shared buffer for both stdOut and stdErr
cmd := exec.Command("go", cmdline[1:]...)
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Run()
passed = err == nil
if !passed {
fmt.Println(buf)
} else {
log.Println("tests passed")
}
return
}
func gobuild(buildpath string) (passed bool, err error) {
cmdline := []string{"go", "build"}
if *race_detector {
cmdline = append(cmdline, "-race")
}
cmdline = append(cmdline, "-v", buildpath)
// setup the build command, use a shared buffer for both stdOut and stdErr
cmd := exec.Command("go", cmdline[1:]...)
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Run()
passed = err == nil
if !passed {
fmt.Println(buf)
} else {
log.Println("build passed")
}
return
}
func run(binName, binPath string, args []string) (runch chan bool) {
runch = make(chan bool)
go func() {
cmdline := append([]string{binName}, args...)
var proc *os.Process
for relaunch := range runch {
if proc != nil {
err := proc.Signal(os.Interrupt)
if err != nil {
log.Printf("error on sending signal to process: '%s', will now hard-kill the process\n", err)
proc.Kill()
}
proc.Wait()
}
if !relaunch {
continue
}
cmd := exec.Command(binPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Print(cmdline)
err := cmd.Start()
if err != nil {
log.Printf("error on starting process: '%s'\n", err)
}
proc = cmd.Process
}
}()
return
}
func getWatcher(buildpath string) (watcher *fsnotify.Watcher, err error) {
watcher, err = fsnotify.NewWatcher()
addToWatcher(watcher, buildpath, map[string]bool{})
return
}
func addToWatcher(watcher *fsnotify.Watcher, importpath string, watching map[string]bool) {
pkg, err := build.Import(importpath, "", 0)
if err != nil {
return
}
if pkg.Goroot {
return
}
watcher.Watch(pkg.Dir)
watching[importpath] = true
for _, imp := range pkg.Imports {
if !watching[imp] {
addToWatcher(watcher, imp, watching)
}
}
}
func rerun(buildpath string, args []string) (err error) {
log.Printf("setting up %s %v", buildpath, args)
pkg, err := build.Import(buildpath, "", 0)
if err != nil {
return
}
if pkg.Name != "main" {
err = errors.New(fmt.Sprintf("expected package %q, got %q", "main", pkg.Name))
return
}
_, binName := path.Split(buildpath)
var binPath string
if gobin := os.Getenv("GOBIN"); gobin != "" {
binPath = filepath.Join(gobin, binName)
} else {
binPath = filepath.Join(pkg.BinDir, binName)
}
var runch chan bool
if !(*never_run) {
runch = run(binName, binPath, args)
}
no_run := false
if *do_tests {
passed, _ := test(buildpath)
if !passed {
no_run = true
}
}
if *do_build && !no_run {
gobuild(buildpath)
}
var errorOutput string
_, errorOutput, ierr := install(buildpath, errorOutput)
if !no_run && !(*never_run) && ierr == nil {
runch <- true
}
var watcher *fsnotify.Watcher
watcher, err = getWatcher(buildpath)
if err != nil {
return
}
for {
// read event from the watcher
we, _ := <-watcher.Event
// other files in the directory don't count - we watch the whole thing in case new .go files appear.
if filepath.Ext(we.Name) != ".go" {
continue
}
log.Print(we.Name)
// close the watcher
watcher.Close()
// to clean things up: read events from the watcher until events chan is closed.
go func(events chan *fsnotify.FileEvent) {
for _ = range events {
}
}(watcher.Event)
// create a new watcher
log.Println("rescanning")
watcher, err = getWatcher(buildpath)
if err != nil {
return
}
// we don't need the errors from the new watcher.
// we continiously discard them from the channel to avoid a deadlock.
go func(errors chan error) {
for _ = range errors {
}
}(watcher.Error)
var installed bool
// rebuild
installed, errorOutput, _ = install(buildpath, errorOutput)
if !installed {
continue
}
if *do_tests {
passed, _ := test(buildpath)
if !passed {
continue
}
}
if *do_build {
gobuild(buildpath)
}
// rerun. if we're only testing, sending
if !(*never_run) {
runch <- true
}
}
return
}
func main() {
flag.Parse()
if len(flag.Args()) < 1 {
log.Fatal("Usage: rerun [--test] [--no-run] [--build] [--race] <import path> [arg]*")
}
buildpath := flag.Args()[0]
args := flag.Args()[1:]
err := rerun(buildpath, args)
if err != nil {
log.Print(err)
}
}