From 0a7cf349f27a9a0e6d2d467f63dc38a611edaf0a Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Fri, 3 Jan 2020 22:35:06 +0530 Subject: [PATCH] Add basic glob support to Watcher module --- lib/jekyll/watcher.rb | 8 ++++++-- spec/watcher_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/watcher.rb b/lib/jekyll/watcher.rb index ea49ad6..b91ea01 100644 --- a/lib/jekyll/watcher.rb +++ b/lib/jekyll/watcher.rb @@ -100,9 +100,13 @@ def to_exclude(options) def listen_ignore_paths(options) source = Pathname.new(options["source"]).expand_path paths = to_exclude(options) + convert_to_regex(paths, options, source).compact + [%r!^\.jekyll\-metadata!] + end - paths.map do |p| + def convert_to_regex(paths, options, source) + paths.flat_map do |p| absolute_path = Pathname.new(normalize_encoding(p, options["source"].encoding)).expand_path + next convert_to_regex(Dir.glob(p), options, source) if p.include?("*") next unless absolute_path.exist? begin @@ -116,7 +120,7 @@ def listen_ignore_paths(options) rescue ArgumentError # Could not find a relative path end - end.compact + [%r!^\.jekyll\-metadata!] + end end def sleep_forever diff --git a/spec/watcher_spec.rb b/spec/watcher_spec.rb index 98af237..4f9b56e 100644 --- a/spec/watcher_spec.rb +++ b/spec/watcher_spec.rb @@ -126,6 +126,35 @@ end end + context "with excluded globs" do + let(:excluded) { ["foo/**/*"] } + let(:excluded_relative_paths) do + ["foo/bar/sit.md", "foo/lore/sit.md", "foo/barsite.md"] + end + let(:excluded_absolute_dirs) do + ["foo", "foo/bar", "foo/lore"].map do |path| + Jekyll.sanitized_path(options["source"], path) + end + end + let(:excluded_absolute_files) do + excluded_relative_paths.map { |p| Jekyll.sanitized_path(options["source"], p) } + end + let(:options) { base_opts.merge("exclude" => excluded) } + before(:each) do + FileUtils.mkdir_p(excluded_absolute_dirs) + FileUtils.touch(excluded_absolute_files) + end + after(:each) do + FileUtils.rm_rf(excluded_absolute_files + excluded_absolute_dirs) + end + + it "ignores the excluded files" do + excluded_relative_paths.each do |path| + expect(ignore_path?(ignored, path)).to eql(true) + end + end + end + context "with a custom destination" do let(:default_ignored) { [%r!^_config\.yml!, %r!^_dest/!, %r!^\.jekyll\-metadata!] }