-
Notifications
You must be signed in to change notification settings - Fork 10
/
re.go
90 lines (72 loc) · 1.38 KB
/
re.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
package main
import (
"errors"
"fmt"
"log"
"os"
"os/signal"
"sync"
"time"
"github.com/AnuchitO/re/runner"
"github.com/AnuchitO/re/traverse"
)
func splitCommand(args []string) (prog string, params []string, err error) {
if len(args) < 2 {
err = errors.New("you should add command after re [command], e.g. 're go test -v .'")
return
}
prog = args[1]
params = args[2:]
return
}
func main() {
prog, params, err := splitCommand(os.Args)
if err != nil {
log.Fatal(err)
}
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
stop := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
task := runner.New(prog, params...)
go run(dir, task, stop, &wg)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
close(stop)
wg.Wait()
fmt.Println("process terminated")
}
func run(dir string, task *runner.Runner, stop chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
lastMod := time.Now()
err := task.Run()
if err != nil {
fmt.Println(err)
}
for {
select {
case <-stop:
err = task.KillCommand()
if err != nil {
fmt.Println(err)
}
return
default:
}
mod := traverse.IsModify(dir, lastMod)
if mod {
lastMod = time.Now()
err := task.Run()
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("\n----------------- Rerun ------------------\n\n")
}
}
time.Sleep(800 * time.Millisecond)
}
}