1 package Catalyst::Watcher::FileModified;
8 use Time::HiRes qw/sleep/;
9 use namespace::clean -except => 'meta';
11 extends 'Catalyst::Watcher';
19 has _watched_files => (
21 isa => 'HashRef[Str]',
23 clearer => '_clear_watched_files',
28 isa => 'File::Modified',
30 clearer => '_clear_modified',
34 sub _build__watched_files {
37 my $regex = $self->regex;
43 my $path = File::Spec->rel2abs($File::Find::name);
44 return unless $path =~ /$regex/;
45 return unless -f $path;
49 # also watch the directory for changes
50 my $cur_dir = File::Spec->rel2abs($File::Find::dir);
51 $cur_dir =~ s{/script/..}{};
54 follow_fast => $self->follow_symlinks ? 1 : 0,
57 @{ $self->directories }
63 sub _build__modified {
66 return File::Modified->new(
68 files => [ keys %{ $self->_watched_files } ],
74 my $restarter = shift;
77 sleep $self->interval if $self->interval > 0;
79 my @changes = $self->_changed_files;
83 $restarter->handle_changes(@changes);
95 @changes = map { { file => $_, status => 'modified' } }
96 grep { -f $_ } $self->_modified->changed;
100 # File::Modified will die if a file is deleted.
101 die unless $@ =~ /stat '(.+)'/;
104 file => $1 || 'unknown file',
108 $self->_clear_watched_files;
109 $self->_clear_modified;
112 $self->_modified->update;
114 my $old_watch = $self->_watched_files;
116 $self->_clear_watched_files;
118 my $new_watch = $self->_watched_files;
120 my @new_files = grep { !defined $old_watch->{$_} }
125 $self->_clear_modified;
126 push @changes, map { { file => $_, status => 'added' } } @new_files;
133 __PACKAGE__->meta->make_immutable;
141 Catalyst::Watcher::FileModified - Watch for changed application files using File::Modified
145 my $watcher = Catalyst::Watcher::FileModified->new(
146 directories => '/path/to/MyApp',
147 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
151 my @changed_files = $watcher->watch();
157 This class monitors a directory of files for changes made to any file
158 matching a regular expression. It correctly handles new files added to the
159 application as well as files that are deleted.
163 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
165 Creates a new Watcher object.
167 =head2 find_changed_files
169 Returns a list of files that have been added, deleted, or changed
170 since the last time watch was called. Each element returned is a hash
171 reference with two keys. The C<file> key contains the filename, and
172 the C<status> key contains one of "modified", "added", or "deleted".
176 L<Catalyst>, L<Catalyst::Watcher>, L<Catalyst::Restarter>,
181 Catalyst Contributors, see Catalyst.pm
185 This program is free software, you can redistribute it and/or modify
186 it under the same terms as Perl itself.