Class: Middleman::CoreExtensions::FileWatcher::API

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-core/core_extensions/file_watcher.rb

Overview

Core File Change API class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ API

Initialize api and internal path cache



71
72
73
74
75
76
77
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 71

def initialize(app)
  @app = app
  @known_paths = Set.new

  @_changed = []
  @_deleted = []
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



66
67
68
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 66

def app
  @app
end

#known_pathsObject (readonly)

Returns the value of attribute known_paths.



67
68
69
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 67

def known_paths
  @known_paths
end

Instance Method Details

#changed(matcher = nil, &block) ⇒ Array<Proc>

Add callback to be run on file change

Parameters:

  • matcher (nil, Regexp) (defaults to: nil)

    A Regexp to match the change path against

Returns:

  • (Array<Proc>)


83
84
85
86
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 83

def changed(matcher=nil, &block)
  @_changed << [block, matcher] if block_given?
  @_changed
end

#deleted(matcher = nil, &block) ⇒ Array<Proc>

Add callback to be run on file deletion

Parameters:

  • matcher (nil, Regexp) (defaults to: nil)

    A Regexp to match the deleted path against

Returns:

  • (Array<Proc>)


92
93
94
95
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 92

def deleted(matcher=nil, &block)
  @_deleted << [block, matcher] if block_given?
  @_deleted
end

#did_change(path)

This method returns an undefined value.

Notify callbacks that a file changed

Parameters:

  • path (Pathname)

    The file that changed



101
102
103
104
105
106
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 101

def did_change(path)
  path = Pathname(path)
  logger.debug "== File Change: #{path}"
  @known_paths << path
  self.run_callbacks(path, :changed)
end

#did_delete(path)

This method returns an undefined value.

Notify callbacks that a file was deleted

Parameters:

  • path (Pathname)

    The file that was deleted



112
113
114
115
116
117
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 112

def did_delete(path)
  path = Pathname(path)
  logger.debug "== File Deletion: #{path}"
  @known_paths.delete(path)
  self.run_callbacks(path, :deleted)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 152

def exists?(path)
  p = Pathname(path)
  p = p.relative_path_from(Pathname(@app.root)) if !p.relative?
  @known_paths.include?(p)
end

#find_new_files(path)

This method returns an undefined value.

Like reload_path, but only triggers events on new files

Parameters:

  • path (Pathname)

    The path to reload



148
149
150
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 148

def find_new_files(path)
  reload_path(path, true)
end

#ignored?(path) ⇒ Boolean

Whether this path is ignored

Parameters:

  • path (Pathname)

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 161

def ignored?(path)
  path = path.to_s
  app.config[:file_watcher_ignore].any? { |r| path =~ r }
end

#reload_path(path, only_new = false)

This method returns an undefined value.

Manually trigger update events

Parameters:

  • path (Pathname)

    The path to reload

  • only_new (Boolean) (defaults to: false)

    Whether we only look for new files



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 124

def reload_path(path, only_new=false)
  # chdir into the root directory so Pathname can work with relative paths
  Dir.chdir @app.root_path do
    path = Pathname(path)
    return unless path.exist?

    glob = (path + '**').to_s
    subset = @known_paths.select { |p| p.fnmatch(glob) }

    ::Middleman::Util.all_files_under(path, &method(:ignored?)).each do |filepath|
      next if only_new && subset.include?(filepath)

      subset.delete(filepath)
      did_change(filepath)
    end

    subset.each(&method(:did_delete)) unless only_new
  end
end