File Coverage

blib/lib/App/codefork.pm
Criterion Covered Total %
statement 13 55 23.6
branch 0 20 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 0 3 0.0
total 18 91 19.7


line stmt bran cond sub pod time code
1             package App::codefork;
2             BEGIN {
3 1     1   796 $App::codefork::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Worlds dumbest code forker
6             $App::codefork::VERSION = '0.001';
7 1     1   2484 use Moo;
  1         49018  
  1         9  
8 1     1   8992 use MooX::Options;
  1         3530  
  1         7  
9 1     1   554410 use IO::All;
  1         23283  
  1         12  
10 1     1   1383 use File::chdir;
  1         6444  
  1         1857  
11              
12             option config => (
13             is => 'ro',
14             format => 's',
15             required => 1,
16             isa => sub { die "can't find config file ".$_[0] unless -f $_[0] },
17             doc => 'forking instructions'
18             );
19              
20             option debug => (
21             is => 'ro',
22             doc => 'show debug informations'
23             );
24              
25             option dir => (
26             is => 'lazy',
27             doc => 'work directory'
28             );
29              
30             sub _build_dir {
31 0     0     my ( $self ) = @_;
32 0           return '.';
33             }
34              
35             has mods => (
36             is => 'lazy',
37             );
38              
39             sub _build_mods {
40 0     0     my ( $self ) = @_;
41 0           return [ map {
42 0           chomp($_);
43 0 0         if ($_ =~ /\|/) {
    0          
44 0           [ replace => split('\|',$_) ]
45             } elsif ($_ =~ /%/) {
46 0           [ word => split('%',$_) ]
47             }
48             } ( io($self->config)->slurp ) ];
49             }
50              
51             sub BUILD {
52 0     0 0   my ( $self ) = @_;
53 0           $self->log('Parsing config...');
54 0           $self->log($_->[0].': '.$_->[1].' => '.$_->[2]) for (@{$self->mods});
  0            
55 0           $self->work_on_dir(io($self->dir)->absolute);
56             }
57              
58             sub work_on_dir {
59 0     0 0   my ( $self, $dir ) = @_;
60 0           my @mods = @{$self->mods};
  0            
61 0           local $CWD = $dir;
62 0           for my $entry (io('.')->all) {
63 0           my $fn = $entry->filename;
64 0 0 0       next if $fn eq '.git' || $fn eq '.svn';
65 0           for my $mod (@mods) {
66 0 0         if ($mod->[0] eq 'replace') {
67 0           my $from = $mod->[1];
68 0           my $to = $mod->[2];
69 0           $fn =~ s/$from/$to/g;
70 0 0         if ($fn ne $entry->filename) {
71 0           $entry = $entry->rename($fn);
72 0           last;
73             }
74             }
75             }
76 0 0         if ($entry->is_file) {
77 0           my $content = $entry->slurp;
78 0           for my $mod (@mods) {
79 0           my $cmd = $mod->[0];
80 0           my $from = $mod->[1];
81 0           my $to = $mod->[2];
82 0 0         if ($cmd eq 'replace') {
    0          
83 0           $content =~ s/$from/$to/g;
84             } elsif ($cmd eq 'word') {
85 0           $content =~ s/([^A-Za-z0-9]+)$from([^A-Za-z0-9]+)/$1$to$2/g;
86             }
87             }
88 0 0         if ($content ne $entry->slurp) {
89 0           $entry->print($content);
90             }
91             } else {
92 0           $self->work_on_dir($entry->absolute);
93             }
94             }
95             }
96              
97             sub log {
98 0     0 0   my ( $self, $text ) = @_;
99 0 0         print $text."\n" if $self->debug;
100             }
101              
102              
103             1;
104              
105             __END__