Skip to content

Pass ignore options to Listen #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
24 changes: 23 additions & 1 deletion lib/spring/watcher/listen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
19 changes: 19 additions & 0 deletions test/unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading