This is a Fork
This is a fork of the official version 3.0.x
branch. Sass need to support older
versions of ruby than Guard wants to support on an ongoing basis, so we are releasing
updates as needed for critical fixes and will support ruby 2.0 and
greater for as long as Sass users need it. Our blog has more information about
ths Ruby version policy for Sass.
Listen
The Listen gem listens to file modifications and notifies you about the changes.
Features
- OS-optimized adapters on MRI for Mac OS X 10.6+, Linux, *BSD and Windows, more info below.
- Detects file modification, addition and removal.
- You can watch multiple directories.
- Regexp-patterns for ignoring paths for more accuracy and speed
- Increased change detection accuracy on OS X HFS and VFAT volumes.
- Tested on MRI Ruby environments (2.0+ only) via Travis CI,
Issues / limitations
- Limited support for symlinked directories (#279):
- No directory/adapter-specific configuration options.
- Support for plugins planned for future.
- TCP functionality was removed in Listen 3.0.0 (#319, #218). There are plans to extract this feature to separate gems (#258), until this is finished, you can use by locking the
listen
gem to version '~> 2.10'
. - Some filesystems won't work without polling (VM/Vagrant Shared folders, NFS, Samba, sshfs, etc.).
- Specs suite on JRuby and Rubinius aren't reliable on Travis CI, but should work.
- Windows and *BSD adapter aren't continuously and automatically tested.
- OSX adapter has some performance limitations (#342).
- Ruby 1.9.3 is no longer maintained (and may not work with Listen) - it's best to upgrade to Ruby 2.2.2.
Pull requests or help is very welcome for these.
Install
The simplest way to install Listen is to use Bundler.
gem 'listen', '~> 3.0'
Usage
Call Listen.to
with either a single directory or multiple directories, then define the "changes" callback in a block.
listener = Listen.to('dir/to/listen', 'dir/to/listen2') do |modified, added, removed|
puts "modified absolute path: #{modified}"
puts "added absolute path: #{added}"
puts "removed absolute path: #{removed}"
end
listener.start
sleep
Pause / unpause / stop
Listeners can also be easily paused/unpaused:
listener = Listen.to('dir/path/to/listen') { |modified, added, removed| puts 'handle changes here...' }
listener.start
listener.paused?
listener.processing?
listener.pause
listener.paused?
listener.processing?
listener.unpause
listener.stop
Note: While paused, Listen keeps on collecting changes in the background - to clear them, call "stop"
Note: You should keep track of all started listeners and stop them properly on finish.
Ignore / ignore!
Listen ignores some directories and extensions by default (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer), you can add ignoring patterns with the ignore
option/method or overwrite default with ignore!
option/method.
listener = Listen.to('dir/path/to/listen', ignore: /\.txt/) { |modified, added, removed|
listener.start
listener.ignore! /\.pkg/
listener.ignore /\.rb/
sleep
Note: :ignore
regexp patterns are evaluated against relative paths.
Note: Ignoring paths does not improve performance, except when Polling (#274)
Only
Listen catches all files (less the ignored ones) by default. If you want to only listen to a specific type of file (i.e., just .rb
extension), you should use the only
option/method.
listener = Listen.to('dir/path/to/listen', only: /\.rb$/) { |modified, added, removed|
listener.start
listener.only /_spec\.rb$/
sleep
Note: :only
regexp patterns are evaluated only against relative file paths.
Changes callback
Changes to the listened-to directories gets reported back to the user in a callback.
The registered callback gets invoked, when there are changes, with three parameters:
modified
, added
and removed
paths, in that particular order.
Paths are always returned in their absolute form.
Example:
listener = Listen.to('path/to/app') do |modified, added, removed|
end
listener.start
sleep
or ...
callback = Proc.new do |modified, added, removed|
end
listener = Listen.to('dir', &callback)
listener.start
sleep
Options
All the following options can be set through the Listen.to
after the directory path(s) params.
ignore: [%r{/foo/bar}, /\.pid$/, /\.coffee$/]
ignore!: %r{/foo/bar}
only: %r{.rb$}
latency: 0.5
wait_for_delay: 4
force_polling: true
relative: false
polling_fallback_message: 'custom message'
Debugging
Setting the environment variable LISTEN_GEM_DEBUGGING=1
sets up the INFO level logger, while LISTEN_GEM_DEBUGGING=2
sets up the DEBUG level logger.
You can also set Listen.logger
to a custom logger.
Listen adapters
The Listen gem has a set of adapters to notify it when there are changes.
There are 4 OS-specific adapters to support Darwin, Linux, *BSD and Windows.
These adapters are fast as they use some system-calls to implement the notifying function.
There is also a polling adapter - although it's much slower than other adapters,
it works on every platform/system and scenario (including network filesystems such as VM shared folders).
The Darwin and Linux adapters are dependencies of the Listen gem so they work out of the box. For other adapters a specific gem will have to be added to your Gemfile, please read below.
The Listen gem will choose the best adapter automatically, if present. If you
want to force the use of the polling adapter, use the :force_polling
option
while initializing the listener.
On Windows
If you are on Windows, it's recommended to use the wdm
adapter instead of polling.
Please add the following to your Gemfile:
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
On *BSD
If you are on *BSD you can try to use the rb-kqueue
adapter instead of polling.
Please add the following to your Gemfile:
require 'rbconfig'
if RbConfig::CONFIG['target_os'] =~ /bsd|dragonfly/i
gem 'rb-kqueue', '>= 0.2'
end
Please visit the installation section of the Listen WIKI for more information and options for potential fixes.
Issues and troubleshooting
NOTE: without providing the output after setting the LISTEN_GEM_DEBUGGING=1
environment variable, it can be almost impossible to guess why listen is not working as expected.
See TROUBLESHOOTING
Performance
If Listen seems slow or unresponsive, make sure you're not using the Polling adapter (you should see a warning upon startup if you are).
Also, if the directories you're watching contain many files, make sure you're:
- not using Polling (ideally)
- using
:ignore
and :only
options to avoid tracking directories you don't care about (important with Polling and on MacOS) - running Listen with the
:latency
and :wait_for_delay
options not too small or too big (depends on needs) - not watching directories with log files, database files or other frequently changing files
- not using a version of Listen prior to 2.7.7
- not getting silent crashes within Listen (see LISTEN_GEM_DEBUGGING=2)
- not running multiple instances of Listen in the background
- using a file system with atime modification disabled (ideally)
- not using a filesystem with inaccurate file modification times (ideally), e.g. HFS, VFAT
- not buffering to a slow terminal (e.g. transparency + fancy font + slow gfx card + lots of output)
- ideally not running a slow encryption stack, e.g. btrfs + ecryptfs
When in doubt, LISTEN_GEM_DEBUGGING=2 can help discover the actual events and time they happened.
See also Tips and Techniques.
Development
Pull requests are very welcome! Please try to follow these simple rules if applicable:
- Please create a topic branch for every separate change you make.
- Make sure your patches are well tested. All specs must pass on Travis CI.
- Update the Yard documentation.
- Update the README.
- Please do not change the version number.
For questions please join us in our Google group or on
#guard
(irc.freenode.net).
Acknowledgments
Author
Thibaud Guillaume-Gentil (@thibaudgg)
Contributors
https://github.com/guard/listen/graphs/contributors