Merge pull request #2459 from ivantsepp/watch_ignore_files

This commit is contained in:
Parker Moore 2014-06-06 23:11:44 -04:00
commit ccf97a62f1
3 changed files with 49 additions and 53 deletions

View File

@ -19,20 +19,30 @@ module Jekyll
super(base) super(base)
end end
# Listing of all directories (globbed to include subfiles and folders) # Paths to ignore for the watch option
# #
# source - the source path # options - A Hash of options passed to the command
# destination - the destination path
# #
# Returns an Array of directory globs in the source, excluding the destination # Returns a list of relative paths from source that should be ignored
def globs(source, destination) def ignore_paths(options)
Dir.chdir(source) do source = options['source']
dirs = Dir['*'].select { |x| File.directory?(x) } destination = options['destination']
dirs -= [destination, File.expand_path(destination), File.basename(destination)] config_files = Configuration[options].config_files(options)
dirs = dirs.map { |x| "#{x}/**/*" } paths = config_files + Array(destination)
dirs += ['*'] ignored = []
source_abs = Pathname.new(source).expand_path
paths.each do |p|
path_abs = Pathname.new(p).expand_path
begin
rel_path = path_abs.relative_path_from(source_abs).to_s
ignored << Regexp.new(Regexp.escape(rel_path)) unless rel_path.start_with?('../')
rescue ArgumentError
# Could not find a relative path
end end
end end
ignored
end
# Run Site#process and catch errors # Run Site#process and catch errors
# #

View File

@ -56,20 +56,9 @@ module Jekyll
def watch(site, options) def watch(site, options)
require 'listen' require 'listen'
source = options['source']
destination = options['destination']
begin
dest = Pathname.new(destination).relative_path_from(Pathname.new(source)).to_s
ignored = Regexp.new(Regexp.escape(dest))
rescue ArgumentError
# Destination is outside the source, no need to ignore it.
ignored = nil
end
listener = Listen.to( listener = Listen.to(
source, options['source'],
:ignore => ignored, :ignore => ignore_paths(options),
:force_polling => options['force_polling'] :force_polling => options['force_polling']
) do |modified, added, removed| ) do |modified, added, removed|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S") t = Time.now.strftime("%Y-%m-%d %H:%M:%S")

View File

@ -1,38 +1,35 @@
require 'helper' require 'helper'
class TestCommand < Test::Unit::TestCase class TestCommand < Test::Unit::TestCase
context "when calling .globs" do context "when calling .ignore_paths" do
context "when non-default dest & source dirs" do context "when source is absolute" do
setup do setup { @source = source_dir }
@source = source_dir should "return an array with regex for destination" do
@dest = dest_dir absolute = source_dir('dest')
directory_with_contents(@dest) relative = Pathname.new(source_dir('dest')).relative_path_from(Pathname.new('.').expand_path).to_s
@globs = Command.globs(@source, @dest) [absolute, relative].each do |dest|
end config = build_configs("source" => @source, "destination" => dest)
should "return an array without the destination dir" do assert Command.ignore_paths(config).include?(/dest/), "failed with destination: #{dest}"
assert @globs.is_a?(Array)
assert !@globs.include?(@dest)
end
teardown do
clear_dest
end end
end end
context "when using default dest dir" do
setup do
@source = test_dir
@dest = test_dir('_site')
directory_with_contents(@dest)
@globs = Command.globs(@source, @dest)
end end
should "return an array without the destination dir" do context "when source is relative" do
assert @globs.is_a?(Array) setup { @source = Pathname.new(source_dir).relative_path_from(Pathname.new('.').expand_path).to_s }
assert !@globs.include?(@dest) should "return an array with regex for destination" do
@globs.each do |glob| absolute = source_dir('dest')
assert !glob.include?(File.basename(@dest)) relative = Pathname.new(source_dir('dest')).relative_path_from(Pathname.new('.').expand_path).to_s
[absolute, relative].each do |dest|
config = build_configs("source" => @source, "destination" => dest)
assert Command.ignore_paths(config).include?(/dest/), "failed with destination: #{dest}"
end end
end end
teardown do end
FileUtils.rm_r(@dest) context "multiple config files" do
should "return an array with regex for config files" do
config = build_configs("config"=> ["_config.yaml", "_config2.yml"])
ignore_paths = Command.ignore_paths(config)
assert ignore_paths.include?(/_config\.yaml/), 'did not include _config.yaml'
assert ignore_paths.include?(/_config2\.yml/), 'did not include _config2.yml'
end end
end end
end end