File Coverage

blib/lib/Dist/Zilla/Role/FileWatcher.pm
Criterion Covered Total %
statement 32 32 100.0
branch 3 6 50.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 2 2 100.0
total 49 53 92.4


line stmt bran cond sub pod time code
1 3     3   56823 use strict;
  3         6  
  3         110  
2 3     3   12 use warnings;
  3         4  
  3         144  
3             package Dist::Zilla::Role::FileWatcher;
4             # ABSTRACT: Receive notification when something changes a file's contents
5             # vim: set ts=8 sw=4 tw=78 et :
6             $Dist::Zilla::Role::FileWatcher::VERSION = '0.005';
7 3     3   12 use Moose::Role;
  3         5  
  3         17  
8 3     3   13085 use Safe::Isa;
  3         1135  
  3         354  
9 3     3   19 use Dist::Zilla::Role::File::ChangeNotification;
  3         4  
  3         60  
10 3     3   15 use namespace::autoclean;
  3         4  
  3         22  
11              
12             sub watch_file
13             {
14 4     4 1 1704853 my ($self, $file, $on_changed) = @_;
15              
16 4 50       21 $file->$_does('Dist::Zilla::Role::File')
17             or $self->log_fatal('watch_file was not passed a valid file object');
18              
19 4 50       1377 Dist::Zilla::Role::File::ChangeNotification->meta->apply($file)
20             if not $file->$_does('Dist::Zilla::Role::File::ChangeNotification');
21              
22 4         20356 my $plugin = $self;
23             $file->on_changed(sub {
24 5     5   10 my $self = shift;
25 5         16 $plugin->$on_changed($self);
26 4         53 });
27              
28 4         18 $file->watch_file;
29             }
30              
31             sub lock_file
32             {
33 2     2 1 796172 my ($self, $file, $message) = @_;
34              
35 2 50       12 $file->$_does('Dist::Zilla::Role::File')
36             or $self->log_fatal('lock_file was not passed a valid file object');
37              
38 2   66     553 $message ||= 'someone tried to munge ' . $file->name
39             . ' after we read from it. You need to adjust the load order of your plugins!';
40              
41             $self->watch_file(
42             $file,
43             sub {
44 2     2   4 my $me = shift;
45 2         11 $me->log_fatal($message);
46             },
47 2         22 );
48             }
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Dist::Zilla::Role::FileWatcher - Receive notification when something changes a file's contents
61              
62             =head1 VERSION
63              
64             version 0.005
65              
66             =head1 SYNOPSIS
67              
68             package Dist::Zilla::Plugin::MyPlugin;
69             use Moose;
70             with 'Dist::Zilla::Role::SomeRole', 'Dist::Zilla::Role::FileWatcher';
71              
72             sub some_phase
73             {
74             my $self = shift;
75              
76             my (file) = grep { $_->name eq 'some_name' } @{$self->zilla->files};
77             # ... do something with this file ...
78              
79             $self->lock_file($file, 'KEEP OUT!');
80              
81             # or:
82              
83             $self->watch_file(
84             $file,
85             sub {
86             my ($plugin, $file) = @_;
87             ... do something with the file object ...
88             },
89             );
90             }
91              
92             =head1 DESCRIPTION
93              
94             This is a role for L<Dist::Zilla> plugins which gives you a mechanism for
95             detecting and acting on files changing their content. This is useful if your
96             plugin performs an action based on a file's content (perhaps copying that
97             content to another file), and then later in the build process, that source
98             file's content is later modified.
99              
100             =head1 METHODS
101              
102             This role adds the following methods to your plugin class:
103              
104             =head2 C<watch_file($file, $subref)>
105              
106             This method takes two arguments: the C<$file> object to watch, and a
107             subroutine which is invoked when the file's contents change. It is called as a
108             method on your plugin, and is passed one additional argument: the C<$file>
109             object that changed.
110              
111             =head2 C<lock_file($file, $message?)>
112              
113             This method takes the C<$file> object to watch, and an optional message
114             string; when the file is modified after it is locked, the build dies.
115              
116             =head1 SUPPORT
117              
118             =for stopwords irc
119              
120             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Role-File-ChangeNotification>
121             (or L<bug-Dist-Zilla-Role-File-ChangeNotification@rt.cpan.org|mailto:bug-Dist-Zilla-Role-File-ChangeNotification@rt.cpan.org>).
122             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
123              
124             =head1 SEE ALSO
125              
126             =over 4
127              
128             =item *
129              
130             L<Dist::Zilla::Role::File::ChangeNotification> - in this distribution, the underlying implementation for watching the file
131              
132             =item *
133              
134             L<Dist::Zilla::File::OnDisk>
135              
136             =item *
137              
138             L<Dist::Zilla::File::InMemory>
139              
140             =back
141              
142             =head1 AUTHOR
143              
144             Karen Etheridge <ether@cpan.org>
145              
146             =head1 COPYRIGHT AND LICENSE
147              
148             This software is copyright (c) 2013 by Karen Etheridge.
149              
150             This is free software; you can redistribute it and/or modify it under
151             the same terms as the Perl 5 programming language system itself.
152              
153             =cut