From 6ba077cf37c04c287d73da0f9c102600b2675d36 Mon Sep 17 00:00:00 2001 From: Ivan Tse Date: Wed, 28 May 2014 01:15:07 -0400 Subject: [PATCH 1/3] Remove `Command.globs` method It is no longer being used. --- lib/jekyll/command.rb | 15 --------------- test/test_command.rb | 35 ----------------------------------- 2 files changed, 50 deletions(-) diff --git a/lib/jekyll/command.rb b/lib/jekyll/command.rb index 62a532f6..4cfc3f44 100644 --- a/lib/jekyll/command.rb +++ b/lib/jekyll/command.rb @@ -19,21 +19,6 @@ module Jekyll super(base) end - # Listing of all directories (globbed to include subfiles and folders) - # - # source - the source path - # destination - the destination path - # - # Returns an Array of directory globs in the source, excluding the destination - def globs(source, destination) - Dir.chdir(source) do - dirs = Dir['*'].select { |x| File.directory?(x) } - dirs -= [destination, File.expand_path(destination), File.basename(destination)] - dirs = dirs.map { |x| "#{x}/**/*" } - dirs += ['*'] - end - end - # Run Site#process and catch errors # # site - the Jekyll::Site object diff --git a/test/test_command.rb b/test/test_command.rb index ef5b04b0..9f45581d 100644 --- a/test/test_command.rb +++ b/test/test_command.rb @@ -1,41 +1,6 @@ require 'helper' class TestCommand < Test::Unit::TestCase - context "when calling .globs" do - context "when non-default dest & source dirs" do - setup do - @source = source_dir - @dest = dest_dir - directory_with_contents(@dest) - @globs = Command.globs(@source, @dest) - end - should "return an array without the destination dir" do - assert @globs.is_a?(Array) - assert !@globs.include?(@dest) - end - teardown do - clear_dest - 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 - should "return an array without the destination dir" do - assert @globs.is_a?(Array) - assert !@globs.include?(@dest) - @globs.each do |glob| - assert !glob.include?(File.basename(@dest)) - end - end - teardown do - FileUtils.rm_r(@dest) - end - end - end context "when calling .add_build_options" do should "add common options" do cmd = Object.new From c4434f27af5c84b38d5927840b949a8ea173089d Mon Sep 17 00:00:00 2001 From: Ivan Tse Date: Wed, 28 May 2014 01:16:34 -0400 Subject: [PATCH 2/3] Get relative paths of directories to ignore Use `Pathname#realpath` to get absolute paths so that `Pathname#relative_path_from` will not raise an exception. Also add the config files to this list. --- lib/jekyll/commands/build.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index 816c5b3f..cdf8cc63 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -58,13 +58,19 @@ module Jekyll source = options['source'] destination = options['destination'] + config_files = Configuration[options].config_files(options) + paths = config_files + Array(destination) + ignored = [] - 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 + source_abs = Pathname.new(source).realpath + paths.each do |p| + path_abs = Pathname.new(p).realpath + 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 listener = Listen.to( From 6684a8f9141e28ce0a577a82be4593a5aa2e6412 Mon Sep 17 00:00:00 2001 From: Ivan Tse Date: Wed, 28 May 2014 17:49:10 -0400 Subject: [PATCH 3/3] Refactor logic into Command class. Add tests. --- lib/jekyll/command.rb | 25 +++++++++++++++++++++++++ lib/jekyll/commands/build.rb | 21 ++------------------- test/test_command.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/lib/jekyll/command.rb b/lib/jekyll/command.rb index 4cfc3f44..654f0ae3 100644 --- a/lib/jekyll/command.rb +++ b/lib/jekyll/command.rb @@ -19,6 +19,31 @@ module Jekyll super(base) end + # Paths to ignore for the watch option + # + # options - A Hash of options passed to the command + # + # Returns a list of relative paths from source that should be ignored + def ignore_paths(options) + source = options['source'] + destination = options['destination'] + config_files = Configuration[options].config_files(options) + paths = config_files + Array(destination) + 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 + ignored + end + # Run Site#process and catch errors # # site - the Jekyll::Site object diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index cdf8cc63..1374b0ef 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -56,26 +56,9 @@ module Jekyll def watch(site, options) require 'listen' - source = options['source'] - destination = options['destination'] - config_files = Configuration[options].config_files(options) - paths = config_files + Array(destination) - ignored = [] - - source_abs = Pathname.new(source).realpath - paths.each do |p| - path_abs = Pathname.new(p).realpath - 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 - listener = Listen.to( - source, - :ignore => ignored, + options['source'], + :ignore => ignore_paths(options), :force_polling => options['force_polling'] ) do |modified, added, removed| t = Time.now.strftime("%Y-%m-%d %H:%M:%S") diff --git a/test/test_command.rb b/test/test_command.rb index 9f45581d..23f23379 100644 --- a/test/test_command.rb +++ b/test/test_command.rb @@ -1,6 +1,38 @@ require 'helper' class TestCommand < Test::Unit::TestCase + context "when calling .ignore_paths" do + context "when source is absolute" do + setup { @source = source_dir } + should "return an array with regex for destination" do + absolute = source_dir('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 + context "when source is relative" do + setup { @source = Pathname.new(source_dir).relative_path_from(Pathname.new('.').expand_path).to_s } + should "return an array with regex for destination" do + absolute = source_dir('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 + 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 context "when calling .add_build_options" do should "add common options" do cmd = Object.new