File Coverage

blib/lib/App/datasection/Command/diff.pm
Criterion Covered Total %
statement 58 60 96.6
branch 8 12 66.6
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 78 84 92.8


line stmt bran cond sub pod time code
1 1     1   360265 use warnings;
  1         2  
  1         70  
2 1     1   18 use 5.020;
  1         12  
3 1     1   16 use experimental qw( signatures );
  1         2  
  1         9  
4 1     1   206 use stable qw( postderef );
  1         2  
  1         8  
5 1     1   89 use true;
  1         3  
  1         8  
6              
7             package App::datasection::Command::diff 0.01 {
8              
9             # ABSTRACT: Show the differences between the filesystem and Perl source __DATA__ section files
10             # VERSION
11              
12 1     1   2398 use App::datasection -command;
  1         2  
  1         11  
13 1     1   972 use Data::Section::Writer 0.04;
  1         33237  
  1         45  
14 1     1   9 use Path::Tiny qw( path );
  1         2  
  1         66  
15 1     1   599 use Text::Diff qw( diff );
  1         10357  
  1         753  
16              
17 2     2 1 19 sub execute ($self, $opt, $args) {
  2         4  
  2         4  
  2         5  
  2         4  
18              
19 2         6 my @files = map { path($_) } @$args;
  2         9  
20              
21 2         105 my $dir;
22 2 50       16 if($opt->{dir}) {
23 0         0 $dir = path($opt->{dir})->absolute;
24             }
25              
26 2         5 foreach my $file (@files) {
27 2 50       21 my $dir = $dir ? $dir : $file->sibling($file->basename . '.data');
28              
29 2         289 my $tmp = Path::Tiny->tempfile;
30 2         1792 $tmp->spew_raw($file->slurp_raw);
31              
32 2 50       2435 if(-d $dir) {
33 2         72 my $dsw = Data::Section::Writer
34             ->new(perl_filename => $tmp);
35              
36 6     6   2460 $dir->visit(sub ($path, $state) {
  6         14  
  6         10  
  6         11  
37 6 100       21 return if $path->is_dir;
38 4 50       67 if(-B $path) {
39 0         0 $dsw->add_file($path->relative($dir), $path->slurp_raw, 'base64');
40             } else {
41 4         238 $dsw->add_file($path->relative($dir), $path->slurp_utf8);
42             }
43 2         720 }, { recurse => 1 });
44              
45 2         1118 $dsw->update_file;
46 2 100       6975 unless($dsw->unchanged) {
47 1         66 my $diff = diff \$file->slurp_utf8, \$tmp->slurp_utf8;
48 1         2434 chomp $diff;
49 1         8 my $from = path('a')->child($file);
50 1         145 my $to = path('b')->child($file);
51 1         88 say "--- $from";
52 1         55 say "+++ $to";
53 1         22 say $diff;
54             }
55             }
56             }
57             }
58              
59             }
60              
61             __END__