File Coverage

blib/lib/Dist/Zilla/Plugin/CommentOut.pm
Criterion Covered Total %
statement 57 57 100.0
branch 18 20 90.0
condition 2 3 66.6
subroutine 8 8 100.0
pod 0 2 0.0
total 85 90 94.4


line stmt bran cond sub pod time code
1 1     1   189944 use strict;
  1         6  
  1         24  
2 1     1   5 use warnings;
  1         1  
  1         19  
3 1     1   22 use 5.024;
  1         3  
4              
5             package Dist::Zilla::Plugin::CommentOut 0.05 {
6              
7             # ABSTRACT: Comment out code in your scripts and modules
8              
9              
10 1     1   458 use Moose;
  1         385391  
  1         7  
11 1     1   6760 use experimental qw( signatures );
  1         2954  
  1         4  
12              
13             with (
14             'Dist::Zilla::Role::FileMunger',
15             'Dist::Zilla::Role::FileFinderUser' => {
16             default_finders => [ ':ExecFiles', ':InstallModules' ],
17             },
18             );
19              
20 1     1   584 use namespace::autoclean;
  1         6803  
  1         4  
21              
22             has id => (
23             is => 'rw',
24             isa => 'Str',
25             default => 'dev-only',
26             );
27              
28             has remove => (
29             is => 'rw',
30             isa => 'Int',
31             default => 0,
32             );
33              
34             has begin => (
35             is => 'rw',
36             isa => 'Str',
37             );
38              
39             has end => (
40             is => 'rw',
41             isa => 'Str',
42             );
43              
44             sub munge_files ($self)
45 4     4 0 103855 {
  4         12  
  4         18  
46 4         26 $self->munge_file($_) for $self->found_files->@*;
47 4         17 return;
48             }
49              
50 8         17 sub munge_file ($self, $file)
51 8     8 0 6947 {
  8         14  
  8         11  
52 8 50       24 return if $file->is_bytes;
53              
54 8         375 $self->log("commenting out @{[ $self->id ]} in @{[ $file->name ]}");
  8         210  
  8         23  
55              
56 8         2479 my $content = $file->content;
57              
58 8         5673 my $id = $self->id;
59              
60 8 50       23 if($id)
61             {
62 8 100       210 if($self->remove)
63 4         74 { $content =~ s/^(.*?#\s*\Q$id\E\s*)$/\n/mg }
64             else
65 4         87 { $content =~ s/^(.*?#\s*\Q$id\E\s*)$/#$1/mg }
66             }
67              
68 8 100 66     224 if($self->begin && $self->end)
69             {
70 4         101 my $begin = $self->begin;
71 4         99 my $end = $self->end;
72 4         52 $begin = qr{^\s*#\s*\Q$begin\E\s*$};
73 4         27 $end = qr{^\s*#\s*\Q$end\E\s*$};
74              
75 4         29 my @lines = split /\n/, $content;
76 4         10 my $in = 0;
77 4         11 for(@lines)
78             {
79 34 100       55 if(!$in)
80             {
81 24 100       66 if($_ =~ $begin)
82             {
83 2         4 $in = 1;
84 2 100       53 $_ = '' if $self->remove;
85             }
86             }
87             else
88             {
89 10 100       30 if($_ =~ $end)
90             {
91 2         4 $in = 0;
92 2 100       50 $_ = '' if $self->remove;
93             }
94             else
95             {
96 8 100       196 if($self->remove)
97 4         7 { $_ = '' }
98             else
99 4         14 { s/^/#/ }
100             }
101             }
102             }
103 4         21 $content = join "\n", @lines, '';
104             }
105              
106 8         30 $file->content($content);
107 8         1704 return;
108             }
109              
110             __PACKAGE__->meta->make_immutable;
111              
112             }
113              
114             1;
115              
116             __END__
117              
118             =pod
119              
120             =encoding UTF-8
121              
122             =head1 NAME
123              
124             Dist::Zilla::Plugin::CommentOut - Comment out code in your scripts and modules
125              
126             =head1 VERSION
127              
128             version 0.05
129              
130             =head1 SYNOPSIS
131              
132             [CommentOut]
133             id = dev-only
134              
135             =head1 DESCRIPTION
136              
137             This plugin comments out lines of code in your Perl scripts or modules with
138             the provided identification. This allows you to have code in your development
139             tree that gets commented out before it gets shipped by L<Dist::Zilla> as a
140             tarball.
141              
142             =head1 MOTIVATION
143              
144             I use perlbrew and/or perls installed in funny places and I'd like to be able to run
145             executables out of by git checkout tree without invoking C<perl -Ilib> on
146             every call. To that end I write something like this:
147              
148             #!/usr/bin/env perl
149            
150             use strict;
151             use warnings;
152             use lib::findbin '../lib'; # dev-only
153             use App::MyApp;
154              
155             That is lovely, except that the main toolchain installers EUMM and MB will
156             convert C</usr/bin/perl> but not C</usr/bin/env perl> to the correct perl
157             when the distribution is installed. There
158             is a handy plugin C<[SetScriptShebang]> that solves that problem but the
159             C<use lib::findbin '../lib';> is problematic because C<../lib> relative to
160             the install location might not be right! With both C<[SetScriptShebang]>
161             and this plugin, I can fix both problems:
162              
163             [SetScriptShebang]
164             [CommentOut]
165              
166             And my script will be converted to:
167              
168             #!perl
169            
170             use strict;
171             use warnings;
172             #use lib::findbin '../lib'; # dev-only
173             use App::MyApp;
174              
175             Which is the right thing for CPAN. Since lines are commented out, line numbers
176             are retained.
177              
178             =head1 PROPERTIES
179              
180             =head2 id
181              
182             The comment id to search for. The default is C<dev-only>.
183              
184             =head2 remove
185              
186             Remove lines instead of comment them out.
187              
188             =head2 begin
189              
190             For block comments, the id to use for the beginning of the block.
191             Block comments are off unless both C<begin> and C<end> are specified.
192              
193             =head2 end
194              
195             For block comments, the id to use for the beginning of the block.
196             Block comments are off unless both C<begin> and C<end> are specified.
197              
198             =head1 SEE ALSO
199              
200             =over 4
201              
202             =item L<Dist::Zilla::Plugin::Comment>
203              
204             Does something very similar. I did actually do a survay of Dist::Zilla
205             plugins before writing this one, but apparently I missed this one. Anyway
206             I prefer C<[CommentOut]> as it is configurable.
207              
208             =back
209              
210             =head1 AUTHOR
211              
212             Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
213              
214             Contributors:
215              
216             Mohammad S Anwar (MANWAR)
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             This software is copyright (c) 2017 by Graham Ollis.
221              
222             This is free software; you can redistribute it and/or modify it under
223             the same terms as the Perl 5 programming language system itself.
224              
225             =cut