class Middleman::CoreExtensions::FileWatcher::API

Core File Change API class

Attributes

app[R]
known_paths[R]

Public Class Methods

new(app) click to toggle source

Initialize api and internal path cache

# File lib/middleman-core/core_extensions/file_watcher.rb, line 67
def initialize(app)
  @app = app
  @known_paths = Set.new

  @_changed = []
  @_deleted = []
end

Public Instance Methods

changed(matcher=nil, &block) click to toggle source

Add callback to be run on file change

@param [nil,Regexp] matcher A Regexp to match the change path against @return [Array<Proc>]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 79
def changed(matcher=nil, &block)
  @_changed << [block, matcher] if block_given?
  @_changed
end
deleted(matcher=nil, &block) click to toggle source

Add callback to be run on file deletion

@param [nil,Regexp] matcher A Regexp to match the deleted path against @return [Array<Proc>]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 88
def deleted(matcher=nil, &block)
  @_deleted << [block, matcher] if block_given?
  @_deleted
end
did_change(path) click to toggle source

Notify callbacks that a file changed

@param [Pathname] path The file that changed @return [void]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 97
def did_change(path)
  path = Pathname(path)
  logger.debug "== File Change: #{path}"
  @known_paths << path
  run_callbacks(path, :changed)
end
did_delete(path) click to toggle source

Notify callbacks that a file was deleted

@param [Pathname] path The file that was deleted @return [void]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 108
def did_delete(path)
  path = Pathname(path)
  logger.debug "== File Deletion: #{path}"
  @known_paths.delete(path)
  run_callbacks(path, :deleted)
end
exists?(path) click to toggle source
# File lib/middleman-core/core_extensions/file_watcher.rb, line 148
def exists?(path)
  p = Pathname(path)
  p = p.relative_path_from(Pathname(@app.root)) unless p.relative?
  @known_paths.include?(p)
end
find_new_files(path) click to toggle source

Like reload_path, but only triggers events on new files

@param [Pathname] path The path to reload @return [void]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 144
def find_new_files(path)
  reload_path(path, true)
end
ignored?(path) click to toggle source

Whether this path is ignored @param [Pathname] path @return [Boolean]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 157
def ignored?(path)
  path = path.to_s
  app.config[:file_watcher_ignore].any? { |r| path =~ r }
end
reload_path(path, only_new=false) click to toggle source

Manually trigger update events

@param [Pathname] path The path to reload @param [Boolean] only_new Whether we only look for new files @return [void]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 120
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

Protected Instance Methods

run_callbacks(path, callbacks_name) click to toggle source

Notify callbacks for a file given an array of callbacks

@param [Pathname] path The file that was changed @param [Symbol] callbacks_name The name of the callbacks method @return [void]

# File lib/middleman-core/core_extensions/file_watcher.rb, line 169
def run_callbacks(path, callbacks_name)
  path = path.to_s
  send(callbacks_name).each do |callback, matcher|
    next unless matcher.nil? || path.match(matcher)
    @app.instance_exec(path, &callback)
  end
end