Treat source file types more like tags, allow a set of them

This commit is contained in:
Thomas Reynolds 2014-10-18 12:45:49 -05:00
parent 54e10cf472
commit d0a9f01b2f
2 changed files with 22 additions and 12 deletions

View file

@ -3,7 +3,7 @@ require 'backports/2.0.0/enumerable/lazy'
module Middleman module Middleman
# The standard "record" that contains information about a file on disk. # The standard "record" that contains information about a file on disk.
SourceFile = Struct.new :relative_path, :full_path, :directory, :type SourceFile = Struct.new :relative_path, :full_path, :directory, :types
# Sources handle multiple on-disk collections of files which make up # Sources handle multiple on-disk collections of files which make up
# a Middleman project. They are separated by `type` which can then be # a Middleman project. They are separated by `type` which can then be
@ -83,7 +83,7 @@ module Middleman
Contract SourceFile => Bool Contract SourceFile => Bool
def globally_ignored?(file) def globally_ignored?(file)
@ignores.values.any? do |descriptor| @ignores.values.any? do |descriptor|
((descriptor[:type] == :all) || (file[:type] == descriptor[:type])) && ((descriptor[:type] == :all) || file[:types].include?(descriptor[:type])) &&
matches?(descriptor[:validator], file) matches?(descriptor[:validator], file)
end end
end end
@ -194,10 +194,10 @@ module Middleman
# @param [Symbol] type The file "type". # @param [Symbol] type The file "type".
# @param [String] path The file path relative to it's source root. # @param [String] path The file path relative to it's source root.
# @return [Boolean] # @return [Boolean]
Contract Symbol, String => Maybe[HANDLER] Contract SetOf[Symbol], String => Maybe[HANDLER]
def watcher_for_path(type, path) def watcher_for_path(types, path)
watchers watchers
.select { |d| d.type == type } .select { |d| types.include?(d.type) }
.find { |d| d.exists?(path) } .find { |d| d.exists?(path) }
end end
@ -275,11 +275,11 @@ module Middleman
Contract ArrayOf[SourceFile], ArrayOf[SourceFile], HANDLER => Any Contract ArrayOf[SourceFile], ArrayOf[SourceFile], HANDLER => Any
def did_change(updated_files, removed_files, watcher) def did_change(updated_files, removed_files, watcher)
valid_updated = updated_files.select do |file| valid_updated = updated_files.select do |file|
watcher_for_path(file[:type], file[:relative_path].to_s) == watcher watcher_for_path(file[:types], file[:relative_path].to_s) == watcher
end end
valid_removed = removed_files.select do |file| valid_removed = removed_files.select do |file|
watcher_for_path(file[:type], file[:relative_path].to_s).nil? watcher_for_path(file[:types], file[:relative_path].to_s).nil?
end end
return if valid_updated.empty? && valid_removed.empty? return if valid_updated.empty? && valid_removed.empty?
@ -299,8 +299,8 @@ module Middleman
if callback[:type] == :all if callback[:type] == :all
callback[:proc].call(updated_files, removed_files) callback[:proc].call(updated_files, removed_files)
else else
valid_updated = updated_files.select { |f| callback[:type] == f[:type] } valid_updated = updated_files.select { |f| f[:types].include?(callback[:type]) }
valid_removed = removed_files.select { |f| callback[:type] == f[:type] } valid_removed = removed_files.select { |f| f[:types].include?(callback[:type]) }
callback[:proc].call(valid_updated, valid_removed) callback[:proc].call(valid_updated, valid_removed)
end end

View file

@ -28,6 +28,9 @@ module Middleman
Contract None => Hash Contract None => Hash
attr_reader :options attr_reader :options
IMAGE_EXTENSIONS = %w(.png .jpg .jpeg .webp .svg .svgz .gif)
FONT_EXTENSIONS = %w(.otf .woff .eot .ttf)
# Construct a new SourceWatcher # Construct a new SourceWatcher
# #
# @param [Middleman::Sources] parent The parent collection. # @param [Middleman::Sources] parent The parent collection.
@ -217,7 +220,7 @@ module Middleman
.to_a .to_a
.each do |f| .each do |f|
add_file_to_cache(f) add_file_to_cache(f)
logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}" logger.debug "== Change (#{f[:types]}): #{f[:relative_path]}"
end end
valid_removes = removed_paths valid_removes = removed_paths
@ -228,7 +231,7 @@ module Middleman
.to_a .to_a
.each do |f| .each do |f|
remove_file_from_cache(f) remove_file_from_cache(f)
logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}" logger.debug "== Deletion (#{f[:types]}): #{f[:relative_path]}"
end end
run_callbacks( run_callbacks(
@ -265,8 +268,15 @@ module Middleman
# @return [Middleman::SourceFile] # @return [Middleman::SourceFile]
Contract Pathname => IsA['Middleman::SourceFile'] Contract Pathname => IsA['Middleman::SourceFile']
def path_to_source_file(path) def path_to_source_file(path)
types = Set.new([@type])
if @type == :source
types << :image if IMAGE_EXTENSIONS.include?(path.extname)
types << :font if FONT_EXTENSIONS.include?(path.extname)
end
::Middleman::SourceFile.new( ::Middleman::SourceFile.new(
path.relative_path_from(@directory), path, @directory, @type) path.relative_path_from(@directory), path, @directory, types)
end end
# Notify callbacks for a file given an array of callbacks # Notify callbacks for a file given an array of callbacks