-
Notifications
You must be signed in to change notification settings - Fork 67
/
watch.go
184 lines (152 loc) · 3.93 KB
/
watch.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
// Package watcher watches all file changes via fsnotify package and sends
// update events to builder
package watcher
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
fsnotify "gopkg.in/fsnotify.v1"
)
// GoPath not set error
var ErrPathNotSet = errors.New("gopath not set")
var watchedFileExt = []string{".go", ".tmpl", ".tpl", ".html"}
var watchDelta = 1000 * time.Millisecond
// Watcher watches the file change events from fsnotify and
// sends update messages. It is also used as a fsnotify.Watcher wrapper
type Watcher struct {
rootdir string
watcher *fsnotify.Watcher
watchVendor bool
// when a file gets changed a message is sent to the update channel
update chan struct{}
}
// MustRegisterWatcher creates a new Watcher and starts listening to
// given folders
func MustRegisterWatcher(params *Params) *Watcher {
watchVendorStr := params.Get("watch-vendor")
var watchVendor bool
var err error
if watchVendorStr != "" {
watchVendor, err = strconv.ParseBool(watchVendorStr)
if err != nil {
log.Println("Wrong watch-vendor value: %s (default=false)", watchVendorStr)
}
}
w := &Watcher{
update: make(chan struct{}),
rootdir: params.Get("watch"),
watchVendor: watchVendor,
}
w.watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Could not register watcher: %s", err)
}
// add folders that will be watched
w.watchFolders()
return w
}
// Watch listens file updates, and sends signal to
// update channel when .go and .tmpl files are updated
func (w *Watcher) Watch() {
eventSent := false
for {
select {
case event := <-w.watcher.Events:
// discard chmod events
if event.Op&fsnotify.Chmod != fsnotify.Chmod {
// test files do not need a rebuild
if isTestFile(event.Name) {
continue
}
if !isWatchedFileType(event.Name) {
continue
}
if eventSent {
continue
}
eventSent = true
// prevent consequent builds
go func() {
w.update <- struct{}{}
time.Sleep(watchDelta)
eventSent = false
}()
}
case err := <-w.watcher.Errors:
if err != nil {
log.Fatalf("Watcher error: %s", err)
}
return
}
}
}
func isTestFile(fileName string) bool {
return strings.HasSuffix(filepath.Base(fileName), "_test.go")
}
func isWatchedFileType(fileName string) bool {
ext := filepath.Ext(fileName)
return existIn(ext, watchedFileExt)
}
// Wait waits for the latest messages
func (w *Watcher) Wait() <-chan struct{} {
return w.update
}
// Close closes the fsnotify watcher channel
func (w *Watcher) Close() {
w.watcher.Close()
close(w.update)
}
// watchFolders recursively adds folders that will be watched against the changes,
// starting from the working directory
func (w *Watcher) watchFolders() {
wd, err := w.prepareRootDir()
if err != nil {
log.Fatalf("Could not get root working directory: %s", err)
}
filepath.Walk(wd, func(path string, info os.FileInfo, err error) error {
// skip files
if info == nil {
log.Fatalf("wrong watcher package: %s", path)
}
if !info.IsDir() {
return nil
}
if !w.watchVendor {
// skip vendor directory
vendor := fmt.Sprintf("%s/vendor", wd)
if strings.HasPrefix(path, vendor) {
return filepath.SkipDir
}
}
// skip hidden folders
if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
}
w.addFolder(path)
return err
})
}
// addFolder adds given folder name to the watched folders, and starts
// watching it for further changes
func (w *Watcher) addFolder(name string) {
if err := w.watcher.Add(name); err != nil {
log.Fatalf("Could not watch folder: %s", err)
}
}
// prepareRootDir prepares working directory depending on root directory
func (w *Watcher) prepareRootDir() (string, error) {
if w.rootdir == "" {
return os.Getwd()
}
path := os.Getenv("GOPATH")
if path == "" {
return "", ErrPathNotSet
}
root := fmt.Sprintf("%s/src/%s", path, w.rootdir)
return root, nil
}