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

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

Overview

Core File Change API class

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (API) initialize

Initialize api and internal path cache



59
60
61
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 59

def initialize
  self.known_paths = Set.new
end

Instance Attribute Details

- (Object) instance

Returns the value of attribute instance



56
57
58
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 56

def instance
  @instance
end

- (Object) known_paths

Returns the value of attribute known_paths



56
57
58
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 56

def known_paths
  @known_paths
end

Instance Method Details

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

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>)


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

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

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

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>)


77
78
79
80
81
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 77

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

- (void) did_change(path)

This method returns an undefined value.

Notify callbacks that a file changed

Parameters:

  • path (String)

    The file that changed



87
88
89
90
91
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 87

def did_change(path)
  puts "== File Change: #{path}" if instance.logging? && !::Middleman::Watcher.ignore_list.any? { |r| path.match(r) }
  self.known_paths << path
  self.run_callbacks(path, :changed)
end

- (void) did_delete(path)

This method returns an undefined value.

Notify callbacks that a file was deleted

Parameters:

  • path (String)

    The file that was deleted



97
98
99
100
101
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 97

def did_delete(path)
  puts "== File Deletion: #{path}" if instance.logging? && !::Middleman::Watcher.ignore_list.any? { |r| path.match(r) }
  self.known_paths.delete(path)
  self.run_callbacks(path, :deleted)
end

- (void) find_new_files(path)

This method returns an undefined value.

Like reload_path, but only triggers events on new files

Parameters:

  • path (String)

    The path to reload



128
129
130
131
132
133
134
135
136
137
138
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 128

def find_new_files(path)
  relative_path = path.sub("#{self.instance.root}/", "")
  subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) }
      
  Find.find(path) do |file|
    next if File.directory?(file)
    next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) }
    relative_path = file.sub("#{self.instance.root}/", "")
    self.did_change(relative_path) unless subset.include?(relative_path)
  end if File.exists?(path)
end

- (void) reload_path(path)

This method returns an undefined value.

Manually trigger update events

Parameters:

  • path (String)

    The path to reload



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 107

def reload_path(path)
  relative_path = path.sub("#{self.instance.root}/", "")
  subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) }
      
  Find.find(path) do |path|
    next if File.directory?(path)
    next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) }
    relative_path = path.sub("#{self.instance.root}/", "")
    subset.delete(relative_path)
    self.did_change(relative_path)
  end if File.exists?(path)
      
  subset.each do |removed_path|
    self.did_delete(removed_path)
  end
end