File Coverage

blib/lib/Dist/Zilla/Plugin/CommentOut.pm
Criterion Covered Total %
statement 47 47 100.0
branch 12 14 85.7
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 2 0.0
total 68 73 93.1


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