notify.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package watch
  2. import (
  3. "expvar"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/windmilleng/tilt/internal/logger"
  7. )
  8. var (
  9. numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
  10. )
  11. type FileEvent struct {
  12. path string
  13. }
  14. func NewFileEvent(p string) FileEvent {
  15. if !filepath.IsAbs(p) {
  16. panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
  17. }
  18. return FileEvent{path: p}
  19. }
  20. func (e FileEvent) Path() string {
  21. return e.path
  22. }
  23. type Notify interface {
  24. // Start watching the paths set at init time
  25. Start() error
  26. // Stop watching and close all channels
  27. Close() error
  28. // A channel to read off incoming file changes
  29. Events() chan FileEvent
  30. // A channel to read off show-stopping errors
  31. Errors() chan error
  32. }
  33. // When we specify directories to watch, we often want to
  34. // ignore some subset of the files under those directories.
  35. //
  36. // For example:
  37. // - Watch /src/repo, but ignore /src/repo/.git
  38. // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
  39. //
  40. // The PathMatcher inteface helps us manage these ignores.
  41. type PathMatcher interface {
  42. Matches(file string) (bool, error)
  43. // If this matches the entire dir, we can often optimize filetree walks a bit.
  44. MatchesEntireDir(file string) (bool, error)
  45. }
  46. type EmptyMatcher struct {
  47. }
  48. func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
  49. func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
  50. var _ PathMatcher = EmptyMatcher{}
  51. func NewWatcher(paths []string, ignore PathMatcher, l logger.Logger) (Notify, error) {
  52. return newWatcher(paths, ignore, l)
  53. }