File Coverage

blib/lib/Dist/Zilla/App/Command/perltidy.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::App::Command::perltidy;
2             $Dist::Zilla::App::Command::perltidy::VERSION = '0.19';
3 1     1   3405 use strict;
  1         2  
  1         34  
4 1     1   4 use warnings;
  1         2  
  1         30  
5              
6             # ABSTRACT: perltidy your dist
7 1     1   184 use Dist::Zilla::App -command;
  0            
  0            
8             use Path::Iterator::Rule;
9             use File::Copy;
10              
11             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             [ 'backend|b=s', 'tidy backend to use', { default => 'vanilla' } ];
34             }
35              
36             sub execute {
37             my ( $self, $opt, $arg ) = @_;
38              
39             # use perltidyrc from command line or from config
40             my $perltidyrc;
41             if ( scalar @$arg and -r $arg->[0] ) {
42             $perltidyrc = $arg->[0];
43             } else {
44             my $plugin = $self->zilla->plugin_named('PerlTidy');
45             if ( defined $plugin and defined $plugin->perltidyrc ) {
46             $perltidyrc = $plugin->perltidyrc;
47             }
48             }
49              
50             # Verify that if a file is specified it is readable
51             if ( defined $perltidyrc and not -r $perltidyrc ) {
52             $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             if ( not exists $backends->{ $opt->{backend} } ) {
60             $self->zilla->log_fatal(
61             [ "specified backend not known, known backends are: %s ",
62             join q[,], sort keys %{$backends}
63             ]
64             );
65             }
66              
67             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             my $rule = Path::Iterator::Rule->new();
72             $rule->skip_vcs;
73             $rule->skip(
74             sub {
75             return if not -d $_;
76             if ( $_[1] =~ qr/^\.build$/ ) {
77             $self->zilla->log_debug('Ignoring .build');
78             return 1;
79             }
80             if ( $_[1] =~ qr/^[A-Za-z].*-[0-9.]+(-TRIAL)?$/ ) {
81             $self->zilla->log_debug('Ignoring dzil build tree');
82             return 1;
83             }
84             return;
85             }
86             );
87             $rule->file->nonempty;
88             $rule->file->not_binary;
89             $rule->file->perl_file;
90              
91             # $rule->file->line_match(qr/\s\n/);
92              
93             my $next = $rule->iter(
94             '.' => {
95             follow_symlinks => 0,
96             sorted => 0,
97             }
98             );
99              
100             while ( my $file = $next->() ) {
101             my $tidyfile = $file . '.tdy';
102             $self->zilla->log_debug( [ 'Tidying %s', $file ] );
103             if ( my $pid = fork() ) {
104             waitpid $pid, 0;
105             $self->zilla->log_fatal(
106             [ 'Child exited with nonzero status: %s', $? ] )
107             if $? > 0;
108             File::Copy::move( $tidyfile, $file );
109             next;
110             }
111             $tidy->(
112             source => $file,
113             destination => $tidyfile,
114             ( $perltidyrc ? ( perltidyrc => $perltidyrc ) : () ),
115             );
116             exit 0;
117             }
118              
119             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.19
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