-
Notifications
You must be signed in to change notification settings - Fork 0
/
fresher.go
171 lines (155 loc) · 2.97 KB
/
fresher.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
package fresher
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
type Option struct {
build *BuildConfig
configs []*WatcherConfig
globalExclude *GlobalExclude
exts Extensions
interval time.Duration
}
func defaultOption() *Option {
return &Option{
build: &BuildConfig{
Target: "main.go",
},
configs: []*WatcherConfig{
{
Name: ".",
},
},
exts: Extensions{"go"},
interval: time.Second * 3,
}
}
type Fresher struct {
opt *Option
event chan fsnotify.Event
rebuild chan bool
timer *time.Timer
mu *sync.Mutex
}
func New(fns ...OptionFunc) *Fresher {
fr := &Fresher{
opt: defaultOption(),
event: make(chan fsnotify.Event, 1),
rebuild: make(chan bool, 1),
mu: new(sync.Mutex),
}
for _, fn := range fns {
fn(fr)
}
return fr
}
func (f *Fresher) Watch() error {
log.Info("Start Watching......")
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to init watcher: %v\n", err)
}
defer watcher.Close()
done := make(chan struct{})
go func() {
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
f.rebuild <- true
close(quit)
close(done)
}()
if err := f.run(); err != nil {
log.Error(fmt.Errorf("failed to build: %v\n", err))
}
watcherPath := NewWatcherPath(f.opt.configs, f.opt)
for _, path := range f.opt.configs {
wp, err := path.Walk(watcher, f.opt)
if err != nil {
return err
}
watcherPath.Merge(wp)
}
go f.publish(watcher, watcherPath)
go f.subscribe()
<-done
return nil
}
func (f *Fresher) publish(watcher *fsnotify.Watcher, watcherPath *WatcherPath) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue
}
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
continue
}
if event.Op&fsnotify.Create == fsnotify.Create {
if err := watcherPath.AddIfNeeds(event.Name, watcher); err != nil {
if err != skipToAddErr {
log.Error(err)
}
continue
}
}
if _, exists := watcherPath.watches[event.Name]; !exists {
continue
}
f.event <- event
case err, ok := <-watcher.Errors:
if !ok {
continue
}
log.Error(err)
}
}
}
func (f *Fresher) run() error {
log.Building()
commands := f.opt.build.Commands()
ctx, cancel := context.WithCancel(context.Background())
for _, cmd := range commands {
if err := cmd.ExecContext(ctx); err != nil {
return err
}
}
go func() {
<-f.rebuild
cancel()
for _, cmd := range commands {
cmd.Kill()
}
}()
return nil
}
func (f *Fresher) reserve() error {
timer := time.AfterFunc(f.opt.interval, func() {
f.rebuild <- true
if err := f.run(); err != nil {
log.Error(err)
return
}
})
f.mu.Lock()
if f.timer != nil {
f.timer.Stop()
}
f.timer = timer
f.mu.Unlock()
return nil
}
func (f *Fresher) subscribe() {
for {
event := <-f.event
log.UpdateFile(event.Name)
if err := f.reserve(); err != nil {
log.Println(err)
}
}
}