Skip to content
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

Add an option to allow filtering which change events to respond to #146

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions lib/rerun/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Options
:signal => (windows? ? "TERM,KILL" : "TERM,INT,KILL"),
:verbose => false,
:wait => 2,
:filter => [:modified, :added, :removed]
}

def self.parse args: ARGV, config_file: nil
Expand Down Expand Up @@ -68,6 +69,10 @@ def self.parse args: ARGV, config_file: nil
options[:ignore] += [pattern]
end

o.on("-f changes", "--filter", "changes to respond to, default = #{DEFAULTS[:filter].join(",")}") do |value|
options[:filter] = value.strip.split(/\s*,\s*/).reject(&:empty?).map(&:to_sym)
end

o.on("--[no-]ignore-dotfiles", "by default, file globs do not match files that begin with a dot. Setting --no-ignore-dotfiles allows you to monitor a relevant file like .env, but you may also have to explicitly --ignore more dotfiles and dotdirs.") do |value|
options[:ignore_dotfiles] = value
end
Expand Down
23 changes: 20 additions & 3 deletions lib/rerun/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def initialize(options = {}, &client_callback)
options = {
:directory => ".",
:pattern => "**/*",
:filter => [:modified, :added, :removed],
:priority => 0,
:ignore_dotfiles => true,
}.merge(options)

@pattern = options[:pattern]
@directories = options[:directory]
@directories = sanitize_dirs(@directories)
@filter = options[:filter]
@priority = options[:priority]
@force_polling = options[:force_polling]
@ignore = [options[:ignore]].flatten.compact
Expand All @@ -57,16 +59,31 @@ def sanitize_dirs(dirs)
end
end

def filter_changes(modified, added, removed)
changes = { modified: [], added: [], removed: [] }
@filter.each do |change|
case change
when :modified
changes[:modified] = modified
when :added
changes[:added] = added
when :removed
changes[:removed] = removed
end
end
changes
end

def start
if @thread then
raise RuntimeError, "already started"
end

@thread = Thread.new do
@listener = Listen.to(*@directories, only: watching, ignore: ignoring, wait_for_delay: 1, force_polling: @force_polling) do |modified, added, removed|
count = modified.size + added.size + removed.size
if count > 0
@client_callback.call(:modified => modified, :added => added, :removed => removed)
changes = filter_changes(modified, added, removed)
if changes.each_value.map(&:size).sum > 0
@client_callback.call(**changes)
end
end
@listener.start
Expand Down