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
# 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
# a Middleman project. They are separated by `type` which can then be
@ -83,7 +83,7 @@ module Middleman
Contract SourceFile => Bool
def globally_ignored?(file)
@ignores.values.any? do |descriptor|
((descriptor[:type] == :all) || (file[:type] == descriptor[:type])) &&
((descriptor[:type] == :all) || file[:types].include?(descriptor[:type])) &&
matches?(descriptor[:validator], file)
end
end
@ -194,10 +194,10 @@ module Middleman
# @param [Symbol] type The file "type".
# @param [String] path The file path relative to it's source root.
# @return [Boolean]
Contract Symbol, String => Maybe[HANDLER]
def watcher_for_path(type, path)
Contract SetOf[Symbol], String => Maybe[HANDLER]
def watcher_for_path(types, path)
watchers
.select { |d| d.type == type }
.select { |d| types.include?(d.type) }
.find { |d| d.exists?(path) }
end
@ -275,11 +275,11 @@ module Middleman
Contract ArrayOf[SourceFile], ArrayOf[SourceFile], HANDLER => Any
def did_change(updated_files, removed_files, watcher)
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
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
return if valid_updated.empty? && valid_removed.empty?
@ -299,8 +299,8 @@ module Middleman
if callback[:type] == :all
callback[:proc].call(updated_files, removed_files)
else
valid_updated = updated_files.select { |f| callback[:type] == f[:type] }
valid_removed = removed_files.select { |f| callback[:type] == f[:type] }
valid_updated = updated_files.select { |f| f[:types].include?(callback[:type]) }
valid_removed = removed_files.select { |f| f[:types].include?(callback[:type]) }
callback[:proc].call(valid_updated, valid_removed)
end

View file

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