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].freeze
CODE_TYPES =

Types which require a reload to eval ruby

[:reload].freeze
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.



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

def initialize(app, _options={}, 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 'lib/middleman-core/sources.rb', line 36

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#by_type(type) ⇒ Object



172
173
174
# File '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



280
281
282
283
284
285
286
# File 'lib/middleman-core/sources.rb', line 280

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



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

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)


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

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

#filesObject



180
181
182
# File '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
199
200
# File 'lib/middleman-core/sources.rb', line 191

def find(types, path, glob=false)
  array_of_types = Array(types)

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

#find_new_files!Object



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

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

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



91
92
93
94
95
96
97
# File '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)


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

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:



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

Contract RespondTo[:on_change] => Any

#poll_once!Object



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

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



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

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

#stop!Object



257
258
259
260
# File 'lib/middleman-core/sources.rb', line 257

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

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

  watcher.unwatch

  bump_count
end

#watch(type_or_handler, options = {}) ⇒ 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 'lib/middleman-core/sources.rb', line 119

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



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

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

#watchersObject



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

def watchers
  @sorted_watchers
end