Class: Middleman::Sources

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
middleman-core/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.

%i[source locales data].freeze
Matcher =
Or[Regexp, RespondTo[:call]
HANDLER =

Duck-typed definition of a valid source watcher

RespondTo[:on_change]

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_hash = ::Middleman::EMPTY_HASH, watchers = []) ⇒ Sources

Returns a new instance of Sources.


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'middleman-core/lib/middleman-core/sources.rb', line 59

def initialize(app, _options_hash = ::Middleman::EMPTY_HASH, watchers = [])
  @app = app
  @watchers = watchers
  @sorted_watchers = @watchers.dup.freeze

  ::Middleman::Sources.file_cache = {}

  # 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.


36
37
38
# File 'middleman-core/lib/middleman-core/sources.rb', line 36

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.


43
44
45
# File 'middleman-core/lib/middleman-core/sources.rb', line 43

def options
  @options
end

Instance Method Details

#by_type(type) ⇒ Object


172
173
174
# File 'middleman-core/lib/middleman-core/sources.rb', line 172

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

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


292
293
294
295
296
297
298
# File 'middleman-core/lib/middleman-core/sources.rb', line 292

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

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


304
305
306
307
308
309
310
# File 'middleman-core/lib/middleman-core/sources.rb', line 304

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

#exists?(types, path) ⇒ Boolean

Returns:

  • (Boolean)

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

def exists?(types, path)
  watchers.any? { |d| includes_type?(types, d.type) && d.exists?(path) }
end

#filesObject


180
181
182
# File 'middleman-core/lib/middleman-core/sources.rb', line 180

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

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


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

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

#find_new_files!Object


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

def find_new_files!
  return [] unless @update_count != @last_update_count

  @last_update_count = @update_count
  watchers.reduce([]) { |sum, w| sum + w.find_new_files! }
end

#globally_ignored?(file) ⇒ Boolean

Returns:

  • (Boolean)

104
105
106
107
108
109
# File 'middleman-core/lib/middleman-core/sources.rb', line 104

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

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


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

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

  bump_count
  poll_once! if @running
end

#ignored?(path) ⇒ Boolean

Returns:

  • (Boolean)

316
317
318
319
# File 'middleman-core/lib/middleman-core/sources.rb', line 316

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

#includes_type?(types, type) ⇒ Boolean

Returns:

  • (Boolean)

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

def includes_type?(types, type)
  if types.is_a? Symbol
    types == type
  else
    types.include? type
  end
end

#on_change(types, &block)

This method returns an undefined value.

Disconnect a specific watcher.

Parameters:


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

Contract RespondTo[:on_change] => Any

#poll_once!Object


249
250
251
252
253
254
# File 'middleman-core/lib/middleman-core/sources.rb', line 249

def poll_once!
  return [] unless @update_count != @last_update_count

  @last_update_count = @update_count
  watchers.reduce([]) { |sum, w| sum + w.poll_once! }
end

#start!Object


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

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

#stop!Object


269
270
271
272
# File 'middleman-core/lib/middleman-core/sources.rb', line 269

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.


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

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

#unwatch(watcher) ⇒ Object


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

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

  watcher.unwatch

  bump_count
end

#watch(type_or_handler, options_hash = ::Middleman::EMPTY_HASH) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'middleman-core/lib/middleman-core/sources.rb', line 119

def watch(type_or_handler, options_hash = ::Middleman::EMPTY_HASH)
  handler = if type_or_handler.is_a? Symbol
              path = File.expand_path(options_hash.delete(:path), app.root)
              SourceWatcher.new(self, type_or_handler, path, options_hash)
            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


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

def watcher_for_path(types, path)
  watchers.detect { |d| includes_type?(types, d.type) && d.exists?(path) }
end

#watchersObject


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

def watchers
  @sorted_watchers
end