File Coverage

blib/lib/HTTP/Proxy/GreaseMonkey/ScriptHome.pm
Criterion Covered Total %
statement 37 46 80.4
branch 6 16 37.5
condition 4 12 33.3
subroutine 10 11 90.9
pod 2 2 100.0
total 59 87 67.8


line stmt bran cond sub pod time code
1             package HTTP::Proxy::GreaseMonkey::ScriptHome;
2              
3 2     2   24861 use warnings;
  2         7  
  2         69  
4 2     2   11 use strict;
  2         3  
  2         63  
5 2     2   12 use Carp;
  2         5  
  2         148  
6 2     2   13 use File::Find;
  2         4  
  2         158  
7 2     2   647 use HTTP::Proxy::GreaseMonkey::Script;
  2         17  
  2         63  
8 2     2   15 use base qw( HTTP::Proxy::GreaseMonkey );
  2         4  
  2         1452  
9              
10             =head1 NAME
11              
12             HTTP::Proxy::GreaseMonkey::ScriptHome - A directory of GreaseMonkey scripts
13              
14             =head1 VERSION
15              
16             This document describes HTTP::Proxy::GreaseMonkey::ScriptHome version 0.05
17              
18             =cut
19              
20             our $VERSION = '0.05';
21              
22             =head1 SYNOPSIS
23              
24             use HTTP::Proxy::GreaseMonkey::ScriptHome;
25            
26             =head1 DESCRIPTION
27              
28             Represents a directory containing a number of GreaseMonkey user scripts.
29              
30             =head1 INTERFACE
31              
32             =head2 C<< add_dir >>
33              
34             Add a directory that may contain user scripts. The directory will be
35             scanned recursively looking for files with the '.js' extension.
36              
37             =cut
38              
39             sub add_dir {
40 1     1 1 132 my $self = shift;
41 1         4 push @{ $self->{dirs} }, @_;
  1         4  
42             }
43              
44             =head2 C<< begin >>
45              
46             Begin filter processing. Rescans script directories adding / removing /
47             updating scripts as appropriate.
48              
49             =cut
50              
51             sub begin {
52 0     0 1 0 my ( $self, $message ) = @_;
53              
54 0         0 $self->_reload;
55 0         0 $self->SUPER::begin( $message );
56             }
57              
58             sub _reload {
59 1     1   1101 my $self = shift;
60              
61             # Invasive superclass surgery follows. Look away if squeamish.
62              
63 1         4 my @files = $self->_walk;
64 1 50       3 my @current = @{ $self->{script} || [] };
  1         10  
65 1         3 $self->{script} = [];
66              
67             # Loop over all found scripts replacing any that have been updated,
68             # removing any that have been deleted, adding any that are new,
69             # maintaining original order.
70              
71 1         6 while ( my $f = shift @files ) {
72 2   33     8 while ( @current && $f gt $current[0]->file ) {
73             # Delete orphan
74 0         0 shift @current;
75             }
76              
77 2 50 33     8 if ( @current && $f eq $current[0]->file ) {
78             # Match: updated?
79 0         0 my $cur = shift @current;
80 0 0       0 my @nstat = stat $f or croak "Can't stat $f ($!)";
81 0         0 my @ostat = $cur->stat;
82             # If the script file hasn't changed recycle the current
83             # script object else replace it with a new one.
84 0 0       0 $self->add_script( $nstat[9] > $ostat[9] ? $f : $cur );
85 0 0 0     0 print "Reloading $f\n"
86             if $self->verbose && $nstat[9] > $ostat[9];
87             }
88             else {
89             # New script
90 2         13 $self->add_script( $f );
91 2 50       10 print "Loading $f\n" if $self->verbose;
92             }
93             }
94             }
95              
96             sub _walk {
97 2     2   33 my $self = shift;
98 2         4 my @files = ();
99             find(
100             {
101             wanted => sub {
102 6 100 66 6   314 push @files, $_ if -f && /[.]js$/i;
103             },
104 2 50       156 no_chdir => 1,
105             },
106 2         13 @{ $self->{dirs} || [] }
107             );
108 2         16 return sort @files;
109             }
110              
111             1;
112             __END__