-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
filesystem.go
215 lines (186 loc) · 4.79 KB
/
filesystem.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
// standard packages
"fmt"
"log"
"os"
"path/filepath"
"time"
// external packages
"github.com/radovskyb/watcher"
)
var programDir string
var workingDir string
func init() {
// Attempt to get the program directory, failure is okay.
if pp, err := os.Executable(); err == nil {
programDir = filepath.Dir(pp)
}
// Attempt to get the working directory, failure is okay.
if wd, err := os.Getwd(); err == nil {
workingDir = wd
}
}
var errNoOutToIn = fmt.Errorf("no output to input source")
// Walk the specified pathnames, collecting regular files.
func getFilenames(pathnames []string, outFilename string) []string {
var (
filenames []string
absOutFile string
)
var fileWalker filepath.WalkFunc = func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
absolute, err := filepath.Abs(path)
if err != nil {
return err
}
if absolute == absOutFile {
return errNoOutToIn
}
relative, _ := filepath.Rel(workingDir, absolute) // Failure is okay.
if relative != "" {
filenames = append(filenames, relative)
} else {
filenames = append(filenames, absolute)
}
return nil
}
// Get the absolute output filename.
absOutFile, err := filepath.Abs(outFilename)
if err != nil {
log.Fatalf("error: path %s: %s", outFilename, err.Error())
}
for _, pathname := range pathnames {
if pathname == "-" {
log.Print("warning: path -: Reading from standard input is unsupported.")
continue
} else if err := filepath.Walk(pathname, fileWalker); err != nil {
if err == errNoOutToIn {
log.Fatalf("error: path %s: Output file cannot be an input source.", pathname)
} else {
log.Printf("warning: path %s: %s", pathname, err.Error())
continue
}
}
}
return filenames
}
// Watch the specified pathnames, calling the build callback as necessary.
func watchFilesystem(pathnames []string, outFilename string, buildCallback func()) {
var (
buildRate = time.Millisecond * 500
pollRate = buildRate * 2
)
// Create a new watcher instance.
w := watcher.New()
// Only notify on certain events.
w.FilterOps(
watcher.Create,
watcher.Write,
watcher.Remove,
watcher.Rename,
watcher.Move,
)
// Ignore the output file.
w.Ignore(outFilename)
// Start a goroutine to handle the event loop.
go func() {
build := false
for {
select {
case <-time.After(buildRate):
if build {
buildCallback()
build = false
}
case event := <-w.Event:
if event.FileInfo != nil {
isDir := event.IsDir()
if event.Op == watcher.Write && isDir {
continue
}
var pathname string
switch event.Op {
case watcher.Move, watcher.Rename:
pathname = fmt.Sprintf("%s -> %s", relPath(event.OldPath), relPath(event.Path))
if !build && !isDir {
build = knownFileType(event.OldPath) || knownFileType(event.Path)
}
default:
pathname = relPath(event.Path)
if !build && !isDir {
build = knownFileType(event.Path)
}
}
log.Printf("%s: %s", event.Op, pathname)
}
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Recursively watch the specified paths for changes.
for _, pathname := range pathnames {
if err := w.AddRecursive(pathname); err != nil {
log.Fatalln(err)
}
}
// Print a message telling the user how to cancel watching
// and list all paths being watched.
log.Print()
log.Print("Watch mode started. Press CTRL+C to stop.")
log.Print()
log.Printf("Recursively watched paths: %d", len(pathnames))
for _, pathname := range pathnames {
log.Printf(" %s", relPath(pathname))
}
log.Print()
// Build the ouput once before the watcher starts.
buildCallback()
// Start watching.
if err := w.Start(pollRate); err != nil {
log.Fatalln(err)
}
}
func relPath(original string) string {
absolute, err := filepath.Abs(original)
if err != nil {
// Failure is okay, just return the original path.
return original
}
relative, err := filepath.Rel(workingDir, absolute)
if err != nil {
// Failure is okay, just return the absolute path.
return absolute
}
return relative
}
func knownFileType(filename string) bool {
switch normalizedFileExt(filename) {
// NOTE: The case values here should match those in `storyload.go:(*story).load()`.
case "tw", "twee",
"tw2", "twee2",
"htm", "html",
"css",
"js",
"otf", "ttf", "woff", "woff2",
"gif", "jpeg", "jpg", "png", "svg", "tif", "tiff", "webp",
"aac", "flac", "m4a", "mp3", "oga", "ogg", "opus", "wav", "wave", "weba",
"mp4", "ogv", "webm",
"vtt":
return true
}
return false
}