Class: Middleman::Sources

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
lib/middleman-core/sources.rb

Overview

Sources handle multiple on-disk collections of files which make up a Middleman project. They are separated by type which can then be queried. For example, the source type represents all content that the sitemap uses to build a project. The data type represents YAML data. The locales type represents localization YAML, and so on.

Defined Under Namespace

Classes: CallbackDescriptor

Constant Summary collapse

OUTPUT_TYPES =

Types which could cause output to change.

[:source, :locales, :data]
CODE_TYPES =

Types which require a reload to eval ruby

[:reload]
Matcher =
HANDLER =

Duck-typed definition of a valid source watcher

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options = {}, watchers = []) ⇒ Sources

Returns a new instance of Sources.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/middleman-core/sources.rb', line 48

def initialize(app, options={}, watchers=[])
  @app = app
  @watchers = watchers
  @sorted_watchers = @watchers.dup.freeze

  @options = options

  # Set of procs wanting to be notified of changes
  @on_change_callbacks = ::Hamster::Vector.empty

  # Global ignores
  @ignores = ::Hamster::Hash.empty

  # Whether we're "running", which means we're in a stable
  # watch state after all initialization and config.
  @running = false

  @update_count = 0
  @last_update_count = -1

  # When the app is about to shut down, stop our watchers.
  @app.before_shutdown(&method(:stop!))
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



27
28
29
# File 'lib/middleman-core/sources.rb', line 27

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/middleman-core/sources.rb', line 34

def options
  @options
end

Instance Method Details

#by_type(type) ⇒ Object



160
161
162
# File 'lib/middleman-core/sources.rb', line 160

def by_type(type)
  self.class.new @app, @options, watchers.select { |d| d.type == type }
end

#changed(matcher = nil, &block) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/middleman-core/sources.rb', line 260

def changed(matcher=nil, &block)
  on_change OUTPUT_TYPES do |updated, _removed|
    updated
      .select { |f| matcher.nil? ? true : matches?(matcher, f) }
      .each { |f| block.call(f[:relative_path]) }
  end
end

#deleted(matcher = nil, &block) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/middleman-core/sources.rb', line 272

def deleted(matcher=nil, &block)
  on_change OUTPUT_TYPES do |_updated, removed|
    removed
      .select { |f| matcher.nil? ? true : matches?(matcher, f) }
      .each { |f| block.call(f[:relative_path]) }
  end
end

#exists?(types, path) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
# File 'lib/middleman-core/sources.rb', line 194

def exists?(types, path)
  watchers
    .lazy
    .select { |d| Array(types).include?(d.type) }
    .any? { |d| d.exists?(path) }
end

#filesObject



168
169
170
# File 'lib/middleman-core/sources.rb', line 168

def files
  watchers.flat_map(&:files).uniq { |f| f[:relative_path] }
end

#find(types, path, glob = false) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/middleman-core/sources.rb', line 179

def find(types, path, glob=false)
  watchers
    .lazy
    .select { |d| Array(types).include?(d.type) }
    .map { |d| d.find(path, glob) }
    .reject(&:nil?)
    .first
end

#find_new_files!Object



217
218
219
220
221
222
# File 'lib/middleman-core/sources.rb', line 217

def find_new_files!
  return unless @update_count != @last_update_count

  @last_update_count = @update_count
  watchers.each(&:poll_once!)
end

#globally_ignored?(file) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/middleman-core/sources.rb', line 93

def globally_ignored?(file)
  @ignores.values.any? do |descriptor|
    ((descriptor[:type] == :all) || file[:types].include?(descriptor[:type])) &&
      matches?(descriptor[:validator], file)
  end
end

#ignore(name, type, regex = nil, &block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/middleman-core/sources.rb', line 80

def ignore(name, type, regex=nil, &block)
  @ignores = @ignores.put(name, type: type,
                                validator: (block_given? ? block : regex))

  bump_count
  find_new_files! if @running
end

#ignored?(path) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
287
# File 'lib/middleman-core/sources.rb', line 284

def ignored?(path)
  descriptor = find(OUTPUT_TYPES, path)
  !descriptor || globally_ignored?(descriptor)
end

#on_change(types, &block)

This method returns an undefined value.

Disconnect a specific watcher.

Parameters:



146
# File 'lib/middleman-core/sources.rb', line 146

Contract RespondTo[:on_change] => Any

#start!Object



228
229
230
231
# File 'lib/middleman-core/sources.rb', line 228

def start!
  watchers.each(&:listen!)
  @running = true
end

#stop!Object



237
238
239
240
# File 'lib/middleman-core/sources.rb', line 237

def stop!
  watchers.each(&:stop_listener!)
  @running = false
end

#Symbol

This method returns an undefined value.

Add a proc to ignore paths with either a regex or block.

Parameters:

  • name (Symbol)

    A name for the ignore.

  • type (Symbol)

    The type of content to apply the ignore to.

  • regex (Regexp)

    Ignore by path regex.

  • block (Proc)

    Ignore by block evaluation.



79
# File 'lib/middleman-core/sources.rb', line 79

Contract Symbol, Symbol, Or[Regexp, Proc] => Any

#unwatch(watcher) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/middleman-core/sources.rb', line 147

def unwatch(watcher)
  @watchers.delete(watcher)

  watcher.unwatch

  bump_count
end

#watch(type_or_handler, options = {}) ⇒ Object



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
# File 'lib/middleman-core/sources.rb', line 108

def watch(type_or_handler, options={})
  handler = if type_or_handler.is_a? Symbol
    SourceWatcher.new(self, type_or_handler, options.delete(:path), options)
  else
    type_or_handler
  end

  @watchers << handler

  # The index trick is used so that the sort is stable - watchers with the same priority
  # will always be ordered in the same order as they were registered.
  n = 0
  @sorted_watchers = @watchers.sort_by do |w|
    priority = w.options.fetch(:priority, 50)
    n += 1
    [priority, n]
  end.reverse.freeze

  handler.on_change(&method(:did_change))

  if @running
    handler.poll_once!
    handler.listen!
  end

  handler
end

#watcher_for_path(types, path) ⇒ Object



207
208
209
210
211
# File 'lib/middleman-core/sources.rb', line 207

def watcher_for_path(types, path)
  watchers
    .select { |d| Array(types).include?(d.type) }
    .find { |d| d.exists?(path) }
end

#watchersObject



138
139
140
# File 'lib/middleman-core/sources.rb', line 138

def watchers
  @sorted_watchers
end