blib/lib/Module/Starter/Smart.pm

filter: Statementstmt 98.0% 151 / 154 Branchbran 69.6% 39 / 56 Conditioncond 51.6% 16 / 31 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 100% 21 / 21 Podpod 100% 8 / 8 totaltotal 83.2% 239 / 287 CC 39 SCAR 16.0
CC
39
Cov
85%
_list_modules
12.3
create_distro
5.3
_create_module
5.0
CRAP 5.0
↑ Summary
1 package Module::Starter::Smart;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 Module::Starter::Smart - A Module::Starter plugin for adding new modules into
11 an existing distribution
12  
13 =head1 VERSION
14  
15 version 0.0.10
16  
17 =cut
18  
19 5 our $VERSION = '0.0.10';
20  
21 =head1 SYNOPSIS
22  
23 use Module::Starter qw/Module::Starter::Smart/;
24 Module::Starter->create_distro(%args);
25  
26 # or in ~/.module-starter/config
27 plugin: Module::Starter::Smart
28  
29 # create a new distribution named 'Foo-Bar'
30 $ module-starter --module=Foo::Bar
31  
32 # ... then add a new module
33 $ module-starter --module=Foo::Bar::Me --distro=Foo-Bar
34  
35 =head1 DESCRIPTION
36  
37 Module::Starter::Smart is a simple helper plugin for L<Module::Starter>. It
38 subclasses L<Module::Starter::Simple> and provides its own implementation for
39 several file creation subroutines, such as C<create_distro>, C<create_modules>,
40 C<create_t>, and so on. These new implementations were designed to work
41 with existing distributions.
42  
43 When invoked, the plugin checks if the distribution is already created. If so,
44 the plugin would bypass C<create_basedir>) and go ahead pull in all the
45 existing modules and test files; these information would be used later in the
46 corresponding file creation subroutines for skipping already-created files.
47  
48 B<UPDATE>: This plugin only covers the simplest use cases. For advanced usage,
49 check out L<Module::Starter::AddModule>.
50  
51  
52 =head2 Example
53  
54 Say you have an existing distro, Goof-Ball, and you want to add a new module,
55 Goof::Troop.
56  
57 % ls -R Goof-Ball
58 Build.PL Changes MANIFEST README lib/ t/
59  
60 Goof-Ball/lib:
61 Goof/
62  
63 Goof-Ball/lib/Goof:
64 Ball.pm
65  
66 Goof-Ball/t:
67 00.load.t perlcritic.t pod-coverage.t pod.t
68  
69 Go to the directory containing your existing distribution and run
70 module-starter, giving it the names of the existing distribution and the new
71 module:
72  
73 % module-starter --distro=Goof-Ball --module=Goof::Troop
74 Created starter directories and files
75  
76 % ls -R Goof-Ball
77 Build.PL Changes MANIFEST README lib/ t/
78  
79 Goof-Ball/lib:
80 Goof/
81  
82 Goof-Ball/lib/Goof:
83 Ball.pm Troop.pm
84  
85 Goof-Ball/t:
86 00.load.t perlcritic.t pod-coverage.t pod.t
87  
88 Troop.pm has been added to Goof-Ball/lib/Goof.
89  
90 =cut
91  
92 5 ▶use parent qw(Module::Starter::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 5 ▶use ExtUtils::Command qw/mkpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # Module implementation here
98 5 ▶use subs qw/_unique_sort _pull_modules _list_modules _pull_t _list_t/;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 =head1 INTERFACE
101  
102 No public methods. The module works by subclassing Module::Starter::Simple and
103 rewiring its internal behaviors.
104  
105 =cut
106  
107 3 ▶sub create_distro {
Subroutine create_distro: called CC=4 SCAR=16.8
Pod documented
108 3 my $class = shift;
109 3 ▶ my $self = ref $class? $class: $class->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 3 3
110  
111 my $basedir =
112 $self->{dir} ||
113 $self->{distro} ||
114 3 ▶ do {
Condition33%2/6
$self->{'dir'} || $self->{'distro'} || do { (my $first = $self->{'modules'}[0]) =~ s/::/-/g; $first }
l!l&&r!l&&!r
300
$self->{"dir"} or $self->{"distro"}
l!l&&r!l&&!r
300
MC/DC0%0/3
$self->{'dir'} || $self->{'distro'} || do { (my $first = $self->{'modules'}[0]) =~ s/::/-/g; $first }
$self->{"dir"} $self->{"distro"} do { (my $first = $self->{'modules'}[0]) =~ s/::/-/g; $first }
Truth table25%1/4
A or B || C
A= $self->{"dir"}B= $self->{"distro"}C= do { (my $first = $self->{'modules'}[0]) =~ s/::/-/g; $first }
ABCresult
1XX1
01X1
0011
0000
115 0 (my $first = $self->{modules}[0]) =~ s/::/-/g;
116 0 $first;
117 };
118  
119 3 $self->{modules} = [ _unique_sort _pull_modules($basedir), @{$self->{modules}} ];
120 3 $self->SUPER::create_distro;
121 }
122  
123 3 ▶sub create_basedir {
Subroutine create_basedir: called CC=3 SCAR=11.0
Pod documented
124 3 my $self = shift;
125 3 ▶ return $self->SUPER::create_basedir(@_) unless -e $self->{basedir} && !$self->{force};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"basedir"} and not $self->{"force"} 2 1 3
Condition66%2/3
-e $self->{"basedir"} and not $self->{"force"}
!ll&&!rl&&r
201
MC/DC50%1/2
-e $self->{"basedir"} and not $self->{"force"}
-e $self->{"basedir"} not $self->{"force"}
Truth table66%2/3
A and not(B)
A= -e $self->{"basedir"}B= not $self->{"force"}
ABresult
0X0
100
111
126 1 $self->progress( "Found $self->{basedir}. Use --force if you want to stomp on it." );
127 }
128  
129 3 ▶sub create_modules {
Subroutine create_modules: called CC=1 SCAR=0.0
Pod documented
130 3 my $self = shift;
131 3 $self->SUPER::create_modules(@_);
132 }
133  
134 4 ▶sub _create_module {
Subroutine _create_module: called CC=5 SCAR=16.1
135 4 my $self = shift;
136 4 my $module = shift;
137 4 my $rtname = shift;
138  
139 4 my @parts = split( /::/, $module );
140 4 my $filepart = (pop @parts) . ".pm";
141 4 my @dirparts = ( $self->{basedir}, 'lib', @parts );
142 4 my $manifest_file = join( "/", "lib", @parts, $filepart );
143 4 ▶ if ( @dirparts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirparts) 4 0 4
144 4 my $dir = File::Spec->catdir( @dirparts );
145 4 ▶ if ( not -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 2 4
146 2 local @ARGV = $dir;
147 2 mkpath @ARGV;
148 2 $self->progress( "Created $dir" );
149 }
150 }
151  
152 4 my $module_file = File::Spec->catfile( @dirparts, $filepart );
153  
154 4 $self->{module_file}{$module} =
155 File::Spec->catfile('lib', @parts, $filepart);
156  
157 4 ▶ if (-e $module_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $module_file) { } 1 3 4
158 1 $self->progress( "Skipped $module_file" );
159 } else {
160 3 ▶ open( my $fh, ">", $module_file ) or die "Can't create $module_file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $module_file or die "Can't create ${module_file}: $!\n" 3 0 3
161 3 print $fh $self->module_guts( $module, $rtname );
162 3 close $fh;
163 3 $self->progress( "Created $module_file" );
164 }
165  
166 4 return $manifest_file;
167 }
168  
169 3 ▶sub create_t {
Subroutine create_t: called CC=1 SCAR=0.0
Pod documented
170 3 my $self = shift;
171 3 _unique_sort $self->SUPER::create_t(@_), _pull_t $self->{basedir};
172 }
173  
174 18 ▶sub _create_t {
Subroutine _create_t: called CC=5 SCAR=16.1
175 18 my $self = shift;
176 18 ▶ my $testdir = @_ == 2 ? 't' : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 1 17 18
177 18 my $filename = shift;
178 18 my $content = shift;
179  
180 18 my @dirparts = ( $self->{basedir}, $testdir );
181 18 my $tdir = File::Spec->catdir( @dirparts );
182 18 ▶ if ( not -d $tdir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $tdir) 7 11 18
183 7 local @ARGV = $tdir;
184 7 mkpath();
185 7 $self->progress( "Created $tdir" );
186 }
187  
188 18 my $fname = File::Spec->catfile( @dirparts, $filename );
189  
190 18 ▶ if (-e $fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname) { } 5 13 18
191 5 $self->progress( "Skipped $fname" );
192 } else {
193 13 ▶ open( my $fh, ">", $fname ) or die "Can't create $fname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fname or die "Can't create ${fname}: $!\n" 13 0 13
194 13 print $fh $content;
195 13 close $fh;
196 13 $self->progress( "Created $fname" );
197 }
198  
199 18 return File::Spec->catfile( $testdir, $filename );
200 }
201  
202 2 ▶sub create_Makefile_PL {
Subroutine create_Makefile_PL: called CC=3 SCAR=11.0
Pod documented
203 2 my $self = shift;
204 2 my $main_module = shift;
205  
206 2 my @parts = split( /::/, $main_module );
207 2 my $pm = pop @parts;
208 2 my $main_pm_file = File::Spec->catfile( "lib", @parts, "${pm}.pm" );
209 2 $main_pm_file =~ s{\\}{/}g; # even on Win32, use forward slash
210  
211 2 my $fname = File::Spec->catfile( $self->{basedir}, "Makefile.PL" );
212  
213 2 ▶ if (-e $fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname) { } 1 1 2
214 1 $self->progress( "Skipped $fname" );
215 } else {
216 1 ▶ open( my $fh, ">", $fname ) or die "Can't create $fname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fname or die "Can't create ${fname}: $!\n" 1 0 1
217 1 print $fh $self->Makefile_PL_guts($main_module, $main_pm_file);
218 1 close $fh;
219 1 $self->progress( "Created $fname" );
220 }
221  
222 2 return "Makefile.PL";
223 }
224  
225 1 ▶sub create_Build_PL {
Subroutine create_Build_PL: called CC=3 SCAR=11.1
Pod documented
226 1 my $self = shift;
227 1 my $main_module = shift;
228  
229 1 my @parts = split( /::/, $main_module );
230 1 my $pm = pop @parts;
231 1 my $main_pm_file = File::Spec->catfile( "lib", @parts, "${pm}.pm" );
232 1 $main_pm_file =~ s{\\}{/}g; # even on Win32, use forward slash
233  
234 1 my $fname = File::Spec->catfile( $self->{basedir}, "Build.PL" );
235  
236 1 ▶ if (-e $fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname) { } 0 1 1
237 0 $self->progress( "Skipped $fname" );
238 } else {
239 1 ▶ open( my $fh, ">", $fname ) or die "Can't create $fname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fname or die "Can't create ${fname}: $!\n" 1 0 1
240 1 print $fh $self->Build_PL_guts($main_module, $main_pm_file);
241 1 close $fh;
242 1 $self->progress( "Created $fname" );
243 }
244  
245 1 return "Build.PL";
246 }
247  
248 3 ▶sub create_Changes {
Subroutine create_Changes: called CC=3 SCAR=11.0
Pod documented
249 3 my $self = shift;
250  
251 3 my $fname = File::Spec->catfile( $self->{basedir}, "Changes" );
252  
253 3 ▶ if (-e $fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname) { } 1 2 3
254 1 $self->progress( "Skipped $fname" );
255 } else {
256 2 ▶ open( my $fh, ">", $fname ) or die "Can't create $fname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fname or die "Can't create ${fname}: $!\n" 2 0 2
257 2 print $fh $self->Changes_guts();
258 2 close $fh;
259 2 $self->progress( "Created $fname" );
260 }
261  
262 3 return "Changes";
263 }
264  
265 3 ▶sub create_README {
Subroutine create_README: called CC=3 SCAR=11.0
Pod documented
266 3 my $self = shift;
267 3 my $build_instructions = shift;
268  
269 3 my $fname = File::Spec->catfile( $self->{basedir}, "README" );
270  
271 3 ▶ if (-e $fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname) { } 1 2 3
272 1 $self->progress( "Skipped $fname" );
273 } else {
274 2 ▶ open( my $fh, ">", $fname ) or die "Can't create $fname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fname or die "Can't create ${fname}: $!\n" 2 0 2
275 2 print $fh $self->README_guts($build_instructions);
276 2 close $fh;
277 2 $self->progress( "Created $fname" );
278 }
279  
280 3 return "README";
281 }
282  
283 # Utility functions
284 3 ▶sub _pull_modules {
Subroutine _pull_modules: called CC=4 SCAR=14.0
285 3 my $basedir = shift;
286 3 ▶ return unless $basedir;
Branch: 1 branch
BranchTrueFalseTotal
unless $basedir 0 3 3
287 3 my $libdir = File::Spec->catdir($basedir, "lib");
288 3 ▶ return unless $libdir && -d $libdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $libdir and -d $libdir 2 1 3
Condition66%2/3
$libdir and -d $libdir
!ll&&!rl&&r
021
MC/DC50%1/2
$libdir and -d $libdir
$libdir -d $libdir
Truth table66%2/3
A and B
A= $libdirB= -d $libdir
ABresult
0X0
100
111
289 1 return _list_modules($libdir);
290 }
291  
292 2 ▶sub _list_modules {
Subroutine _list_modules: called CC=10 SCAR=25.1
293 2 my $dir = shift;
294 2 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
11
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
295  
296 2 ▶ opendir my $dh, $dir or die "Cannot opendir $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Cannot opendir ${dir}: $!" 2 0 2
297 2 my @entries = grep { !/^\.{1,2}/ } readdir $dh;
298 2 close $dh;
299  
300 2 my @modules = ();
301 2 for (@entries) {
302 2 my $name = File::Spec->catfile($dir, $_);
303 2 ▶ push @modules, _list_modules($name, $prefix ? "$prefix\:\:$_": $_) and next if -d $name;
Branch: 2 branches
BranchTrueFalseTotal
if -d $name 1 1 2
$prefix ? : 0 1 1
Condition60%3/5
-d $name && (push(@modules, _list_modules($name, $prefix ? "${prefix}::$_" : $_)) && (next))
!ll&&!rl&&r
110
push @modules, _list_modules($name, $prefix ? "${prefix}::$_" : $_) and next
!ll
01
MC/DC0%0/2
-d $name && (push(@modules, _list_modules($name, $prefix ? "${prefix}::$_" : $_)) && (next))
-d $name push @modules, _list_modules($name, $prefix ? "${prefix}::$_" : $_)
Truth table33%1/3
A && B
A= -d $nameB= push @modules, _list_modules($name, $prefix ? "${prefix}::$_" : $_)
ABresult
0X0
100
111
304 1 ▶ $_ =~ s/\.pm$// and push @modules, $prefix ? "$prefix\:\:$_": $_ if $name =~ /\.pm$/;
Branch: 2 branches
BranchTrueFalseTotal
if $name =~ /\.pm$/ 1 0 1
$prefix ? : 1 0 1
Condition33%2/6
$name =~ /\.pm$/ && ($_ =~ s/\.pm$// && push(@modules, $prefix ? "${prefix}::$_" : $_))
!ll&&!rl&&r
010
$_ =~ s/\.pm$// and push @modules, $prefix ? "${prefix}::$_" : $_
!ll&&!rl&&r
010
MC/DC0%0/3
$name =~ /\.pm$/ && ($_ =~ s/\.pm$// && push(@modules, $prefix ? "${prefix}::$_" : $_))
$name =~ /\.pm$/ $_ =~ s/\.pm$// push @modules, $prefix ? "${prefix}::$_" : $_
Truth table0%0/4
A && B and C
A= $name =~ /\.pm$/B= $_ =~ s/\.pm$//C= push @modules, $prefix ? "${prefix}::$_" : $_
ABCresult
0XX0
10X0
1100
1111
305 }
306  
307 2 return sort @modules;
308 }
309  
310 3 ▶sub _pull_t {
Subroutine _pull_t: called CC=4 SCAR=15.2
311 3 my $basedir = shift;
312 3 ▶ return unless $basedir;
Branch: 1 branch
BranchTrueFalseTotal
unless $basedir 0 3 3
313 3 my $tdir = File::Spec->catdir($basedir, "t");
314 3 ▶ return unless $tdir && -d $tdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $tdir and -d $tdir 0 3 3
Condition33%1/3
$tdir and -d $tdir
!ll&&!rl&&r
003
MC/DC0%0/2
$tdir and -d $tdir
$tdir -d $tdir
Truth table33%1/3
A and B
A= $tdirB= -d $tdir
ABresult
0X0
100
111
315 3 return _list_t($tdir);
316 }
317  
318 3 ▶sub _list_t {
Subroutine _list_t: called CC=3 SCAR=11.1
319 3 my $dir = shift;
320  
321 3 ▶ opendir my $dh, $dir or die "Cannot opendir $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Cannot opendir ${dir}: $!" 3 0 3
322 3 ▶ my @entries = grep { !/^\.{1,2}/ && /\.t$/ } readdir $dh;
Condition66%2/3
!/^\.{1,2}/ && /\.t$/
!ll&&!rl&&r
6012
MC/DC50%1/2
!/^\.{1,2}/ && /\.t$/
!/^\.{1,2}/ /\.t$/
Truth table66%2/3
not(A) && B
A= !/^\.{1,2}/B= /\.t$/
ABresult
0X0
100
111
323 3 close $dh;
324  
325 3 map { "t/$_" } @entries;
326 }
327  
328 # Remove duplicated entries
329 6 ▶sub _unique_sort {
Subroutine _unique_sort: called CC=1 SCAR=0.0
330 6 my %bag = map { $_ => 1 } @_;
331 6 sort keys %bag;
332 }
333  
334 # Magic true value required at end of module
335 5 1;
336  
337 __END__
↑ Summary