forked from cortesi/devd
/
watch.go
109 lines (100 loc) · 2.74 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
package devd
import (
"path"
"path/filepath"
"strings"
"time"
"github.com/bmatcuk/doublestar"
"github.com/cortesi/devd/livereload"
"github.com/cortesi/devd/termlog"
"github.com/cortesi/devd/watch"
)
const batchTime = time.Millisecond * 200
// Watch watches an endpoint for changes, if it supports them.
func (r Route) Watch(ch chan []string, excludePatterns []string, log termlog.Logger) error {
switch r.Endpoint.(type) {
case *filesystemEndpoint:
ep := *r.Endpoint.(*filesystemEndpoint)
pathchan := make(chan []string, 1)
err := watch.Watch(string(ep), batchTime, pathchan)
if err != nil {
return err
}
go func() {
for files := range pathchan {
for i, fpath := range files {
files[i] = path.Join(
r.Path,
strings.TrimPrefix(fpath, string(ep)),
)
}
files = filterFiles("/", files, excludePatterns, log)
ch <- files
}
}()
}
return nil
}
// Determine if a file should be included, based on the given exclude paths.
func shouldInclude(file string, excludePatterns []string, log termlog.Logger) bool {
for _, pattern := range excludePatterns {
match, err := doublestar.Match(pattern, file)
if err != nil {
log.Warn("Error matching pattern '%s': %s", pattern, err)
} else if match {
return false
}
}
return true
}
// Filter out the files that match the given exclude patterns.
func filterFiles(pathPrefix string, files, excludePatterns []string, log termlog.Logger) []string {
ret := []string{}
for _, file := range files {
relFile := strings.TrimPrefix(file, pathPrefix)
if shouldInclude(relFile, excludePatterns, log) {
ret = append(ret, file)
}
}
return ret
}
// WatchPaths watches a set of paths, and broadcasts changes through reloader.
func WatchPaths(paths, excludePatterns []string, reloader livereload.Reloader, log termlog.Logger) error {
ch := make(chan []string, 1)
for _, path := range paths {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
if absPath[len(absPath)-1] != filepath.Separator {
absPath += string(filepath.Separator)
}
pathchan := make(chan []string, 1)
err = watch.Watch(path, batchTime, pathchan)
if err != nil {
return err
}
go func() {
for files := range pathchan {
files = filterFiles(absPath, files, excludePatterns, log)
if len(files) > 0 {
ch <- files
}
}
}()
}
go reloader.Watch(ch)
return nil
}
// WatchRoutes watches the route collection, and broadcasts changes through reloader.
func WatchRoutes(routes RouteCollection, reloader livereload.Reloader, excludePatterns []string, log termlog.Logger) error {
c := make(chan []string, 1)
for i := range routes {
err := routes[i].Watch(c, excludePatterns, log)
if err != nil {
return err
}
}
go reloader.Watch(c)
return nil
}