File Coverage

blib/lib/Dist/Zilla/App/Command/perltidy.pm
Criterion Covered Total %
statement 15 56 26.7
branch 0 20 0.0
condition 0 9 0.0
subroutine 5 9 55.5
pod 3 3 100.0
total 23 97 23.7


line stmt bran cond sub pod time code
1             package Dist::Zilla::App::Command::perltidy;
2             $Dist::Zilla::App::Command::perltidy::VERSION = '0.18';
3 1     1   1394 use strict;
  1         2  
  1         38  
4 1     1   5 use warnings;
  1         1  
  1         37  
5              
6             # ABSTRACT: perltidy your dist
7 1     1   493 use Dist::Zilla::App -command;
  1         454644  
  1         16  
8 1     1   7977 use Path::Iterator::Rule;
  1         10342  
  1         44  
9 1     1   580 use File::Copy;
  1         2264  
  1         679  
10              
11 0     0 1   sub abstract {'perltidy your dist'}
12              
13             my $backends = {
14             vanilla => sub {
15             local @ARGV = ();
16             require Perl::Tidy;
17             return sub {
18             local @ARGV = ();
19             Perl::Tidy::perltidy(@_);
20             };
21             },
22             sweet => sub {
23             local @ARGV = ();
24             require Perl::Tidy::Sweetened;
25             return sub {
26             local @ARGV = ();
27             Perl::Tidy::Sweetened::perltidy(@_);
28             };
29             },
30             };
31              
32             sub opt_spec {
33 0     0 1   [ 'backend|b=s', 'tidy backend to use', { default => 'vanilla' } ];
34             }
35              
36             sub execute {
37 0     0 1   my ( $self, $opt, $arg ) = @_;
38              
39             # use perltidyrc from command line or from config
40 0           my $perltidyrc;
41 0 0 0       if ( scalar @$arg and -r $arg->[0] ) {
42 0           $perltidyrc = $arg->[0];
43             } else {
44 0           my $plugin = $self->zilla->plugin_named('PerlTidy');
45 0 0 0       if ( defined $plugin and defined $plugin->perltidyrc ) {
46 0           $perltidyrc = $plugin->perltidyrc;
47             }
48             }
49              
50             # Verify that if a file is specified it is readable
51 0 0 0       if ( defined $perltidyrc and not -r $perltidyrc ) {
52 0           $self->zilla->log_fatal(
53             [ "specified perltidyrc is not readable: %s ,\nNote: ~ and other shell expansions are not applicable",
54             $perltidyrc
55             ]
56             );
57             }
58              
59 0 0         if ( not exists $backends->{ $opt->{backend} } ) {
60 0           $self->zilla->log_fatal(
61             [ "specified backend not known, known backends are: %s ",
62 0           join q[,], sort keys %{$backends}
63             ]
64             );
65             }
66              
67 0           my $tidy = $backends->{ $opt->{backend} }->();
68              
69             # RT 91288
70             # copied from https://metacpan.org/source/KENTNL/Dist-Zilla-PluginBundle-Author-KENTNL-2.007000/utils/strip_eol.pl
71 0           my $rule = Path::Iterator::Rule->new();
72 0           $rule->skip_vcs;
73             $rule->skip(
74             sub {
75 0 0   0     return if not -d $_;
76 0 0         if ( $_[1] =~ qr/^\.build$/ ) {
77 0           $self->zilla->log_debug('Ignoring .build');
78 0           return 1;
79             }
80 0 0         if ( $_[1] =~ qr/^[A-Za-z].*-[0-9.]+(-TRIAL)?$/ ) {
81 0           $self->zilla->log_debug('Ignoring dzil build tree');
82 0           return 1;
83             }
84 0           return;
85             }
86 0           );
87 0           $rule->file->nonempty;
88 0           $rule->file->not_binary;
89 0           $rule->file->perl_file;
90              
91             # $rule->file->line_match(qr/\s\n/);
92              
93 0           my $next = $rule->iter(
94             '.' => {
95             follow_symlinks => 0,
96             sorted => 0,
97             }
98             );
99              
100 0           while ( my $file = $next->() ) {
101 0           my $tidyfile = $file . '.tdy';
102 0           $self->zilla->log_debug( [ 'Tidying %s', $file ] );
103 0 0         if ( my $pid = fork() ) {
104 0           waitpid $pid, 0;
105 0 0         $self->zilla->log_fatal(
106             [ 'Child exited with nonzero status: %s', $? ] )
107             if $? > 0;
108 0           File::Copy::move( $tidyfile, $file );
109 0           next;
110             }
111 0 0         $tidy->(
112             source => $file,
113             destination => $tidyfile,
114             ( $perltidyrc ? ( perltidyrc => $perltidyrc ) : () ),
115             );
116 0           exit 0;
117             }
118              
119 0           return 1;
120             }
121              
122             1;
123              
124             __END__
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             Dist::Zilla::App::Command::perltidy - perltidy your dist
133              
134             =head1 VERSION
135              
136             version 0.18
137              
138             =head2 SYNOPSIS
139              
140             $ dzil perltidy
141             # OR
142             $ dzil perltidy .myperltidyrc
143              
144             =head2 CONFIGURATION
145              
146             In your global dzil setting (which is '~/.dzil' or '~/.dzil/config.ini'),
147             you can config the perltidyrc like:
148              
149             [PerlTidy]
150             perltidyrc = /home/fayland/somewhere/.perltidyrc
151              
152             =head2 DEFAULTS
153              
154             If you do not specify a specific perltidyrc in dist.ini it will try to use
155             the same defaults as Perl::Tidy.
156              
157             =head2 SEE ALSO
158              
159             L<Perl::Tidy>
160              
161             =head1 AUTHORS
162              
163             =over 4
164              
165             =item *
166              
167             Fayland Lam <fayland@gmail.com>
168              
169             =item *
170              
171             Mark Gardner <mjgardner@cpan.org>
172              
173             =item *
174              
175             Kent Fredric <kentfredric@gmail.com>
176              
177             =back
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2014 by Fayland Lam.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut