blib/lib/App/watchdo.pm

filter: Statementstmt 41.0% 39 / 95 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 11 / 22 Podpod 100% 4 / 4 totaltotal 35.2% 54 / 153 CC 14 SCAR 29.7
CC
14
Cov
32%
doit
72.0
_recurse
12.0
__ANON__
6.0
CRAP 19.5
↑ Summary
1 package App::watchdo;
2  
3 # Created on: 2015-03-07 08:21:28
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use AnyEvent::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use AnyEvent::Filesys::Notify;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = version->new('0.1.1');
22  
23 1 has [qw/git run done/] => ( is => 'rw' );
24 has [qw/dirs files exclude changed/] => (
25 is => 'rw',
26 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 );
28 1 has wait => (
29 is => 'rw',
30 default => 1,
31 );
32 has vcs => (
33 is => 'rw',
34 lazy => 1,
35 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 require VCS::Which;
37 0 return VCS::Which->new;
38 },
39 1 );
40  
41 0 ▶sub watch {
Subroutine watch: not called CC=1 SCAR=6.9
Pod documented
42 0 my ($self) = @_;
43  
44 my $notify = AnyEvent::Filesys::Notify->new(
45 dirs => [ $self->get_dirs ],
46 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
47 0 my @changed = @_;
48 0 $self->changed([ @{ $self->changed }, @changed ]);
49  
50 0 ▶ if ( ! $self->done ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->done) 0 0 0
51 0 ▶ $self->done( AE::timer $self->wait, 0, sub { $self->doit() } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 }
53 },
54 0 parse_events => 1,
55 );
56  
57 0 return AnyEvent::Loop::run();
58 }
59  
60 0 ▶sub doit {
Subroutine doit: not called CC=8 SCAR=42.8
Pod documented
61 0 my ($self) = @_;
62 0 my %files = map { $_ => 1 } $self->get_files();
63 0 my %dirs = map { $_ => 1 } @{ $self->dirs() };
64 0 my %seen;
65  
66 my @monitored;
67 0 for my $changed (@{ $self->changed() }) {
68 0 my $path = $changed->path;
69 push @monitored, $changed if (
70 !$seen{$path}++
71 || $files{$path}
72 || $dirs{$path}
73 ) && (
74 0 ! @{ $self->exclude }
75 0 ▶ || ! grep { $path =~ /$_/ } @{ $self->exclude }
Branch: 1 branch
BranchTrueFalseTotal
if !$seen{$path}++ || $files{$path} || $dirs{$path} and not @{$self->exclude;} && grep({$path =~ /$_/;} @{$self->exclude;}) 0 0 0
Condition0%0/12
!$seen{$path}++ || $files{$path} || $dirs{$path} and not @{$self->exclude;} && grep({$path =~ /$_/;} @{$self->exclude;})
!ll&&!rl&&r
000
not $seen{$path}++ or $files{$path} or $dirs{$path}
l!l&&r!l&&!r
000
not $seen{$path}++ or $files{$path}
l!l&&r!l&&!r
000
@{$self->exclude;} && grep({$path =~ /$_/;} @{$self->exclude;})
!ll&&!rl&&r
000
MC/DC0%0/5
!$seen{$path}++ || $files{$path} || $dirs{$path} and not @{$self->exclude;} && grep({$path =~ /$_/;} @{$self->exclude;})
not $seen{$path}++ $files{$path} $dirs{$path} @{$self->exclude;} grep({$path =~ /$_/;} @{$self->exclude;})
Truth table0%0/10
not(A) or B or C and not(D && E)
A= not $seen{$path}++B= $files{$path}C= $dirs{$path}D= @{$self->exclude;}E= grep({$path =~ /$_/;} @{$self->exclude;})
ABCDEresult
000XX0
1XX110
01X110
001110
1XX0X1
1XX101
01X0X1
01X101
0010X1
001101
76 );
77 }
78  
79 0 ▶ $self->run()->(@monitored) if @monitored;
Branch: 1 branch
BranchTrueFalseTotal
if @monitored 0 0 0
80 0 $self->done(undef);
81 0 $self->changed([]);
82 }
83  
84 0 ▶sub get_dirs {
Subroutine get_dirs: not called CC=2 SCAR=17.9
Pod documented
85 0 my ($self) = @_;
86  
87 return uniq sort +(
88 0 ▶ @{ $self->dirs || [] },
Condition0%0/2
$self->dirs || []
l!l
00
MC/DC0%0/1
$self->dirs || []
$self->dirs
Truth table0%0/2
A
A= $self->dirs
Aresult
11
00
89 0 map {path($_)->parent . ''}
90 $self->get_files,
91 );
92 }
93  
94 0 ▶sub get_files {
Subroutine get_files: not called CC=1 SCAR=6.9
Pod documented
95 0 my ($self) = @_;
96  
97 0 return ( $self->_files_from_fs, $self->_files_from_git );
98 }
99  
100 0 ▶sub _files_from_fs {
Subroutine _files_from_fs: not called CC=2 SCAR=17.9
101 0 my ($self) = @_;
102  
103 0 ▶ return map { -d $_ ? _recurse($_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
104 0 @{ $self->files };
105 }
106  
107 0 ▶sub _files_from_git {
Subroutine _files_from_git: not called CC=2 SCAR=17.9
108 0 my ($self) = @_;
109  
110 0 ▶ return if !$self->git;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->git 0 0 0
111  
112 0 my $status = $self->vcs->status('.');
113 return (
114 0 map { chomp $_; $_ } ## no critic
115 0 map { @{ $status->{$_} } }
116 0 grep { $_ ne 'merge' }
117 0 keys %{ $status }
118 );
119 }
120  
121 0 ▶sub _recurse {
Subroutine _recurse: not called CC=3 SCAR=24.8
122 0 my $dir = path(shift);
123 0 my @files;
124  
125 0 for my $child ($dir->children) {
126 0 ▶ if (-d $child) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $child) { } 0 0 0
127 0 push @files, _recurse($child);
128 }
129 else {
130 0 push @files, $child;
131 }
132 }
133  
134 0 return @files;
135 }
136  
137 1 1;
138  
139 __DATA__
↑ Summary