From 38a3c7ffaff19be92cbbb76302f00e0b5abd24a0 Mon Sep 17 00:00:00 2001 From: Milo Winningham Date: Thu, 16 Jan 2025 16:28:00 -0800 Subject: [PATCH 1/2] Pass ignore options to Listen --- README.md | 17 +++++++++++++++++ lib/spring/watcher/listen.rb | 24 +++++++++++++++++++++++- test/unit_test.rb | 19 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 834093e..c9e1b4a 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,20 @@ Add this line to your application's Gemfile: And then execute: $ bundle + +## Ignoring files and directories + +Listen ignores some files and directories by default, including `.git`, `log`, `tmp`, and `vendor` +(see the `Listen::Silencer` [source](https://github.com/guard/listen/blob/master/lib/listen/silencer.rb) for the full list). +To improve performance and/or prevent unwanted restarts, you can adjust these patterns in your `config/spring.rb`: + +```ruby +Spring.watcher.tap do |watcher| + watcher.ignore [/^node_modules/] # Add more pattern(s) to be ignored + watcher.ignore! [/^\.git/] # Or overwrite all default ignored patterns + watcher.only [/\.rb$/] # Only listen for specific file patterns (not applied to directories) +end +``` + +These options are passed to Listen and reapplied when the watcher restarts. +For more information, see the [Listen gem README](https://github.com/guard/listen?tab=readme-ov-file#ignore--ignore). diff --git a/lib/spring/watcher/listen.rb b/lib/spring/watcher/listen.rb index 494c0a3..a484769 100644 --- a/lib/spring/watcher/listen.rb +++ b/lib/spring/watcher/listen.rb @@ -31,7 +31,13 @@ def initialize(*) def start return if @listener - @listener = ::Listen.to(*base_directories, latency: latency, &method(:changed)) + options = { + latency: latency, + ignore: @ignores, + ignore!: @forced_ignores, + only: @onlys + }.compact + @listener = ::Listen.to(*base_directories, **options, &method(:changed)) @listener.start end @@ -65,6 +71,22 @@ def changed(modified, added, removed) end end + def ignore(regexps) + regexps = Array(regexps) + (@ignores ||= []).concat(regexps) + @listener&.ignore(regexps) + end + + def ignore!(regexps) + @forced_ignores = Array(regexps) + @listener&.ignore!(@forced_ignores) + end + + def only(regexps) + @onlys = regexps || false + @listener&.only(@onlys) + end + def mark_stale super diff --git a/test/unit_test.rb b/test/unit_test.rb index c82e5d8..55101b5 100644 --- a/test/unit_test.rb +++ b/test/unit_test.rb @@ -73,4 +73,23 @@ def watcher_class Timeout.timeout(1) { sleep 0.1 while watcher.running? } assert_equal 1, on_stale_count end + + test "passes ignore options to listener" do + watcher.add @dir + watcher.ignore(/\.not_ignored.tmp$/) # .tmp file ignored by default + watcher.ignore!(/ignored\.txt$/) # not be ignored by default, overrides previous + watcher.only(/\.tmp$/) # also ignore non .tmp files + watcher.start + + touch "#{@dir}/ignored.txt" + assert_not_stale + + watcher.restart + + touch "#{@dir}/still_ignored.txt" + assert_not_stale + + touch "#{@dir}/not_ignored.tmp" + assert_stale + end end From a43aa516c5c79ed9d179bfa8778b84e0a9e2202d Mon Sep 17 00:00:00 2001 From: Milo Winningham Date: Thu, 16 Jan 2025 16:41:30 -0800 Subject: [PATCH 2/2] Fix CI --- .github/workflows/ci.yml | 9 ++------- Gemfile | 5 +++++ test/helper.rb | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7eef7d6..5c3ee5e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,13 +6,8 @@ jobs: strategy: fail-fast: false matrix: - ruby: [ '2.7', '3.0', '3.1', 'head' ] - rails: [ '6.0', '6.1', '7.0', 'edge' ] - exclude: - - ruby: '3.1' - rails: '6.0' - - ruby: '3.1' - rails: '6.1' + ruby: [ '3.2', '3.3', '3.4', 'head' ] + rails: [ '7.1', '7.2', '8.0', 'edge' ] env: RAILS_VERSION: ${{ matrix.rails }} diff --git a/Gemfile b/Gemfile index 8db2146..c950816 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,11 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } # Specify your gem's dependencies in spring-watcher-listen.gemspec gemspec +gem "drb" +gem "logger" +gem "mutex_m" +gem "timeout" + if ENV["RAILS_VERSION"] == "edge" gem "activesupport", github: "rails/rails", branch: "main" elsif ENV["RAILS_VERSION"] diff --git a/test/helper.rb b/test/helper.rb index 050a917..8ca3477 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,6 +1,7 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require "bundler/setup" +require "logger" require File.dirname(Gem::Specification.find_by_name("spring").loaded_from) + "/test/support/test" require "minitest/autorun"