forked from skelterjohn/rerun
/
rerun.go
152 lines (131 loc) · 2.9 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
package main
import (
"bytes"
"errors"
"fmt"
"github.com/howeyc/fsnotify"
"go/build"
"log"
"os"
"os/exec"
"path"
"path/filepath"
)
func install(buildpath, lastError string) (installed bool, errorOutput string, err error) {
cmdline := []string{"go", "get", "-v", buildpath}
cmd := exec.Command("go", cmdline[1:]...)
bufOut := bytes.NewBuffer([]byte{})
bufErr := bytes.NewBuffer([]byte{})
cmd.Stdout = bufOut
cmd.Stderr = bufErr
err = cmd.Run()
if bufOut.Len() != 0 {
errorOutput = bufOut.String()
if errorOutput != lastError {
fmt.Print(bufOut)
}
err = errors.New("compile error")
return
}
installed = bufErr.Len() != 0
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 _ = range runch {
if proc != nil {
proc.Kill()
}
cmd := exec.Command(binPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Print(cmdline)
cmd.Start()
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)
binPath := filepath.Join(pkg.BinDir, binName)
runch := run(binName, binPath, args)
var errorOutput string
_, errorOutput, ierr := install(buildpath, errorOutput)
if ierr == nil {
runch <- true
}
var watcher *fsnotify.Watcher
watcher, err = getWatcher(buildpath)
if err != nil {
return
}
for {
we, _ := <-watcher.Event
var installed bool
installed, errorOutput, _ = install(buildpath, errorOutput)
if installed {
log.Print(we.Name)
runch <- true
watcher.Close()
/* empty the buffer */
go func(events chan *fsnotify.FileEvent) {
for _ = range events {
}
}(watcher.Event)
go func(errors chan error) {
for _ = range errors {
}
}(watcher.Error)
/* rescan */
log.Println("rescanning")
watcher, err = getWatcher(buildpath)
if err != nil {
return
}
}
}
return
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Usage: rerun <import path> [arg]*")
}
err := rerun(os.Args[1], os.Args[2:])
if err != nil {
log.Print(err)
}
}