Add some options to watcher to opt out of features later

feature/domain-checker
Thomas Reynolds 2016-01-20 13:34:12 -08:00
parent 6d1c3562a7
commit 5586784947
9 changed files with 125 additions and 22 deletions

View File

@ -1,23 +1,112 @@
require 'contracts' if ENV['CONTRACTS'] != 'false'
require 'hamster' require 'contracts'
require 'hamster'
module Contracts module Contracts
class IsA class IsA
def self.[](val) def self.[](val)
@lookup ||= {} @lookup ||= {}
@lookup[val] ||= new(val) @lookup[val] ||= new(val)
end
def initialize(val)
@val = val
end
def valid?(val)
val.is_a? @val.constantize
end
end end
def initialize(val) VectorOf = Contracts::CollectionOf::Factory.new(::Hamster::Vector)
@val = val ResourceList = Contracts::ArrayOf[IsA['Middleman::Sitemap::Resource']]
end
else
module Contracts
def self.included(base)
base.extend self
end end
def valid?(val) # rubocop:disable MethodName
val.is_a? @val.constantize def Contract(*)
end
class Callable
def self.[](*)
end
end
class Bool
end
class Num
end
class Pos
end
class Neg
end
class Any
end
class None
end
class Or < Callable
end
class Xor < Callable
end
class And < Callable
end
class Not < Callable
end
class RespondTo < Callable
end
class Send < Callable
end
class Exactly < Callable
end
class ArrayOf < Callable
end
class ResourceList < Callable
end
class Args < Callable
end
class HashOf < Callable
end
class Bool
end
class Maybe < Callable
end
class IsA < Callable
end
class SetOf < Callable
end
class Frozen < Callable
end
class VectorOf < Callable
end end
end end
end
VectorOf = ::Contracts::CollectionOf::Factory.new(::Hamster::Vector) module Contracts
ResourceList = ::Contracts::ArrayOf[IsA['Middleman::Sitemap::Resource']]
PATH_MATCHER = Or[String, RespondTo[:match], RespondTo[:call], RespondTo[:to_s]] PATH_MATCHER = Or[String, RespondTo[:match], RespondTo[:call], RespondTo[:to_s]]
end end

View File

@ -36,6 +36,7 @@ module Middleman::CoreExtensions
resources.each do |resource| resources.each do |resource|
next if resource.binary? next if resource.binary?
next if resource.file_descriptor.nil? next if resource.file_descriptor.nil?
next if resource.file_descriptor[:types].include?(:no_frontmatter)
fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup
@ -74,6 +75,11 @@ module Middleman::CoreExtensions
return @cache[file[:full_path]] if @cache.key?(file[:full_path]) return @cache[file[:full_path]] if @cache.key?(file[:full_path])
if file[:types].include?(:no_frontmatter)
$stderr.puts file[:relative_path].to_s
require 'pry'
end
@cache[file[:full_path]] = ::Middleman::Util::Data.parse( @cache[file[:full_path]] = ::Middleman::Util::Data.parse(
file, file,
app.config[:frontmatter_delims] app.config[:frontmatter_delims]

View File

@ -14,7 +14,8 @@ class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
@watcher = app.files.watch :source, @watcher = app.files.watch :source,
path: File.expand_path(options[:source], app.root), path: File.expand_path(options[:source], app.root),
latency: options[:latency] latency: options[:latency],
frontmatter: false
logger.info "== Executing: `#{options[:command]}`" logger.info "== Executing: `#{options[:command]}`"

View File

@ -96,12 +96,13 @@ module Middleman
# @return [String] # @return [String]
Contract String Contract String
def template_data_for_file def template_data_for_file
if @app.extensions[:front_matter] file = @app.files.find(:source, @path)
if @app.extensions[:front_matter] || (file && !file[:types].include?(:no_frontmatter))
result = @app.extensions[:front_matter].template_data_for_file(@path) result = @app.extensions[:front_matter].template_data_for_file(@path)
return result unless result.nil? return result unless result.nil?
end end
file = @app.files.find(:source, @path)
file ? file.read : File.read(@path) file ? file.read : File.read(@path)
end end

View File

@ -11,7 +11,7 @@ module Middleman
# Ignore a path or add an ignore callback # Ignore a path or add an ignore callback
# @param [String, Regexp] path Path glob expression, or path regex # @param [String, Regexp] path Path glob expression, or path regex
# @return [IgnoreDescriptor] # @return [IgnoreDescriptor]
Contract Maybe[Or[String, Regexp]], Maybe[Proc] => RespondTo[:execute_descriptor] Contract Or[String, Regexp, Proc] => RespondTo[:execute_descriptor]
def ignore(path=nil, &block) def ignore(path=nil, &block)
@app.sitemap.invalidate_resources_not_ignored_cache! @app.sitemap.invalidate_resources_not_ignored_cache!
IgnoreDescriptor.new(path, block) IgnoreDescriptor.new(path, block)
@ -49,7 +49,7 @@ module Middleman
else else
match_path == path_clean match_path == path_clean
end end
elsif block_given? elsif block
block.call(match_path) block.call(match_path)
end end
end end

View File

@ -12,8 +12,10 @@ module Middleman
ImportFileDescriptor = Struct.new(:from, :to) do ImportFileDescriptor = Struct.new(:from, :to) do
def execute_descriptor(app, resources) def execute_descriptor(app, resources)
source = ::Middleman::SourceFile.new(Pathname(from).relative_path_from(app.source_dir), Pathname(from), app.source_dir, Set.new([:source, :binary]), 0)
resources + [ resources + [
::Middleman::Sitemap::Resource.new(app.sitemap, to, from) ::Middleman::Sitemap::Resource.new(app.sitemap, to, source)
] ]
end end
end end

View File

@ -161,7 +161,7 @@ module Middleman
# @return [Boolean] # @return [Boolean]
Contract Bool Contract Bool
def binary? def binary?
!file_descriptor.nil? && ::Middleman::Util.binary?(file_descriptor[:full_path].to_s) !file_descriptor.nil? && (file_descriptor[:types].include?(:binary) || ::Middleman::Util.binary?(file_descriptor[:full_path].to_s))
end end
# Ignore a resource directly, without going through the whole # Ignore a resource directly, without going through the whole

View File

@ -66,6 +66,8 @@ module Middleman
@files = {} @files = {}
@extensionless_files = {} @extensionless_files = {}
@frontmatter = options.fetch(:frontmatter, true)
@binary = options.fetch(:binary, false)
@validator = options.fetch(:validator, proc { true }) @validator = options.fetch(:validator, proc { true })
@ignored = options.fetch(:ignored, proc { false }) @ignored = options.fetch(:ignored, proc { false })
@only = Array(options.fetch(:only, [])) @only = Array(options.fetch(:only, []))
@ -181,7 +183,7 @@ module Middleman
Contract ArrayOf[Pathname] Contract ArrayOf[Pathname]
def find_new_files! def find_new_files!
new_files = ::Middleman::Util.all_files_under(@directory.to_s) new_files = ::Middleman::Util.all_files_under(@directory.to_s)
.reject { |p| @files.key?(p) } .reject { |p| @files.key?(p) }
update(new_files, []) update(new_files, [])
@ -278,6 +280,8 @@ module Middleman
Contract Pathname, Pathname, Symbol, Maybe[String] => ::Middleman::SourceFile Contract Pathname, Pathname, Symbol, Maybe[String] => ::Middleman::SourceFile
def path_to_source_file(path, directory, type, destination_dir) def path_to_source_file(path, directory, type, destination_dir)
types = Set.new([type]) types = Set.new([type])
types << :no_frontmatter unless @frontmatter
types << :@binary if @binary
relative_path = path.relative_path_from(directory) relative_path = path.relative_path_from(directory)
relative_path = File.join(destination_dir, relative_path) if destination_dir relative_path = File.join(destination_dir, relative_path) if destination_dir

View File

@ -18,7 +18,7 @@ module Middleman
Contract IsA['Middleman::SourceFile'], Maybe[Symbol] => [Hash, Maybe[String]] Contract IsA['Middleman::SourceFile'], Maybe[Symbol] => [Hash, Maybe[String]]
def parse(file, frontmatter_delims, known_type=nil) def parse(file, frontmatter_delims, known_type=nil)
full_path = file[:full_path] full_path = file[:full_path]
return [{}, nil] if ::Middleman::Util.binary?(full_path) return [{}, nil] if ::Middleman::Util.binary?(full_path) || file[:types].include?(:binary)
# Avoid weird race condition when a file is renamed # Avoid weird race condition when a file is renamed
begin begin