1 package Catalyst::Watcher::Inotify;
7 use namespace::clean -except => 'meta';
9 extends 'Catalyst::Watcher';
13 isa => 'Linux::Inotify2',
14 default => sub { Linux::Inotify2->new },
28 # If this is done via a lazy_build then the call to
29 # ->_add_directory ends up causing endless recursion when it calls
31 $self->_add_directory($_) for @{ $self->directories };
38 my $restarter = shift;
40 my @events = $self->_wait_for_events;
42 $restarter->handle_changes( map { $self->_event_to_change($_) } @events );
47 sub _wait_for_events {
50 my $regex = $self->regex;
53 # This is a blocking read, so it will not return until
54 # something happens. The restarter will end up calling ->watch
55 # again after handling the changes.
56 my @events = $self->_inotify->read;
59 for my $event (@events) {
60 if ( ( $event->IN_CREATE && $event->IN_ISDIR ) ) {
61 $self->_add_directory( $event->fullname );
62 push @interesting, $event;
64 elsif ( $event->IN_DELETE_SELF ) {
66 push @interesting, $event;
68 elsif ( $event->fullname =~ /$regex/ ) {
69 push @interesting, $event;
73 return @interesting if @interesting;
80 my $mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF;
81 $mask |= IN_DONT_FOLLOW unless $self->follow_symlinks;
93 my $path = File::Spec->rel2abs($File::Find::name);
94 return unless -d $path;
96 $self->_inotify->watch( $path, $self->_mask );
98 follow_fast => $self->follow_symlinks ? 1 : 0,
105 sub _event_to_change {
109 my %change = ( file => $event->fullname );
110 if ( $event->IN_CREATE ) {
111 $change{status} = 'added';
113 elsif ( $event->IN_MODIFY ) {
114 $change{status} = 'modified';
116 elsif ( $event->IN_DELETE ) {
117 $change{status} = 'deleted';
120 $change{status} = 'containing directory modified';
126 __PACKAGE__->meta->make_immutable;
134 Catalyst::Watcher - Watch for changed application files
138 my $watcher = Catalyst::Watcher->new(
139 directory => '/path/to/MyApp',
140 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
145 my @changed_files = $watcher->watch();
150 This class monitors a directory of files for changes made to any file
151 matching a regular expression. It correctly handles new files added to the
152 application as well as files that are deleted.
156 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
158 Creates a new Watcher object.
160 =head2 find_changed_files
162 Returns a list of files that have been added, deleted, or changed
163 since the last time watch was called. Each element returned is a hash
164 reference with two keys. The C<file> key contains the filename, and
165 the C<status> key contains one of "modified", "added", or "deleted".
169 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
173 Catalyst Contributors, see Catalyst.pm
177 This program is free software, you can redistribute it and/or modify
178 it under the same terms as Perl itself.