File Coverage

blib/lib/Module/New/Command/Basic.pm
Criterion Covered Total %
statement 94 114 82.4
branch 25 52 48.0
condition 13 30 43.3
subroutine 24 26 92.3
pod n/a
total 156 222 70.2


line stmt bran cond sub pod time code
1             package Module::New::Command::Basic;
2            
3 3     3   323 use strict;
  3         3  
  3         88  
4 3     3   10 use warnings;
  3         3  
  3         63  
5 3     3   10 use Carp;
  3         3  
  3         154  
6 3     3   11 use Module::New::Meta;
  3         4  
  3         13  
7 3     3   132 use Module::New::Queue;
  3         4  
  3         3643  
8            
9             functions {
10            
11             set_distname => sub () { Module::New::Queue->register(sub {
12 6     6   9 my ($self, $name) = @_;
13 6 50       16 croak "distribution/main module name is required" unless $name;
14 6         18 Module::New->context->distname( $name );
15 6     6   31 })},
16            
17             guess_root => sub () { Module::New::Queue->register(sub {
18 5     5   7 my $self = shift;
19 5         15 my $context = Module::New->context;
20 5         18 $context->path->guess_root( $context->config('root') );
21 5     5   28 })},
22            
23             set_file => sub () { Module::New::Queue->register(sub {
24 4     4   6 my ($self, $name) = @_;
25            
26 4 50       9 croak "filename is required" unless $name;
27            
28 4         11 my $context = Module::New->context;
29 4         13 my $type = $context->config('type');
30            
31 4 50       8 unless ($type) {
32 4 100 66     47 if ( $name =~ /::/ or $name =~ /\.pm$/ or $name =~ m{^lib/} ) {
    100 66        
    50 66        
    0 33        
33 2         4 $type = 'Module';
34             }
35             elsif ( $name =~ /\.t$/ or $name =~ m{^t/} ) {
36 1         2 $type = 'Test';
37             }
38             elsif ( $name =~ /\.pl/ or $name =~ m{^(?:bin|scripts?)/} ) {
39 1         2 $type = 'Script';
40             }
41             elsif ( $name = /\./ ) {
42 0         0 $type = 'Plain';
43             }
44             }
45 4   50     10 $type ||= 'Module';
46 4         11 $context->config( type => $type );
47            
48 4 100       18 if ( $type =~ /Module$/ ) {
49 2         8 $context->module( $name );
50             }
51             else {
52 2         8 $context->mainfile( $name );
53             }
54 4     4   21 })},
55            
56             create_distdir => sub () { Module::New::Queue->register(sub {
57 6     6   10 my $self = shift;
58            
59 6         15 my $context = Module::New->context;
60            
61 6         15 $context->path->set_root;
62 6 100       16 unless ( $context->config('no_dirs') ) {
63 5         19 my $distname = $context->distname;
64 5         11 my $distdir = $context->path->dir($distname);
65 5 50       101 if ( $distdir->exists ) {
66 0 0       0 if ( $context->config('force') ) {
    0          
67 0         0 $context->path->remove_dir( $distdir, 'absolute' );
68             }
69             elsif ( $context->config('grace') ) {
70             # just skip and do nothing
71             }
72             else {
73 0         0 croak "$distname already exists";
74             }
75             }
76 5         56 $context->path->create_dir($distname);
77 5         107 $context->path->change_dir($distname);
78             }
79             else {
80 1         3 $context->path->change_dir(".");
81             }
82 6         110 $context->path->set_root;
83 6     6   26 })},
84            
85             create_maketool => sub (;$) {
86 6     6   11 my $type = shift;
87             Module::New::Queue->register(sub {
88 6     6   12 my $self = shift;
89            
90 6         80 my $context = Module::New->context;
91 6   100     30 $type ||= $context->config('make') || 'MakeMakerCPANfile';
      33        
92 6 50       13 $type = 'ModuleBuild' if $type eq 'MB';
93 6 50       9 $type = 'MakeMaker' if $type eq 'EUMM';
94            
95 6         21 $context->files->add( $type );
96 6         35 });
97             },
98            
99             create_general_files => sub () { Module::New::Queue->register(sub {
100 6     6   10 my $self = shift;
101            
102 6         13 Module::New->context->files->add(qw( Readme Changes ManifestSkip License ));
103 6     6   20 })},
104            
105             create_tests => sub (;@) {
106 6     6   8 my @files = @_;
107             Module::New::Queue->register(sub {
108 6     6   7 my $self = shift;
109            
110 6         10 my $context = Module::New->context;
111 6 50       11 if ( ref $context->config('test') eq 'ARRAY' ) {
    50          
112 0         0 $context->files->add( @{ Module::New->context->config('test') } );
  0         0  
113             }
114             elsif ( @files ) {
115 0         0 $context->files->add( @files );
116             }
117             else {
118 6         12 $context->files->add(qw( LoadTest PodTest PodCoverageTest ));
119             }
120 6         25 });
121             },
122            
123             create_files => sub (;@) {
124 10     10   21 my @files = @_;
125             Module::New::Queue->register(sub {
126 10     10   14 my $self = shift;
127            
128 10         21 my $context = Module::New->context;
129 10         23 $context->files->add( @files );
130 10 100       22 if ($context->config('xs')) {
131 1         4 $context->files->add('XS');
132 1         2 eval {
133 1         1108 require Devel::PPPort;
134 1         253 Devel::PPPort::WriteFile();
135 1         1806 $context->log( info => "created ppport.h" );
136             };
137 1 50       41 $context->log( warn => $@ ) if $@;
138             }
139 10         26 while ( my $name = $context->files->next ) {
140 59 100       101 if ( $name eq '{ANY_TYPE}' ) {
141 4   50     8 $name = $context->config('type') || 'Module';
142             }
143 59         115 my $file = $context->loader->reload_class( File => $name );
144 59         173 $context->path->create_file( $file->render );
145             }
146 10         52 });
147             },
148            
149             create_manifest => sub () { Module::New::Queue->register(sub {
150 10     10   19 my $self = shift;
151            
152 10         30 my $context = Module::New->context;
153 10 50       30 $context->path->remove_file('MANIFEST') if $context->config('force');
154            
155 10 50       36 local $ENV{PERL_MM_MANIFEST_VERBOSE} = 0 if $context->config('silent');
156            
157 10         729 require ExtUtils::Manifest;
158 10         6565 ExtUtils::Manifest::mkmanifest();
159            
160 10         11170 $context->log( info => 'updated manifest' );
161 10     10   55 })},
162            
163             edit_mainfile => sub (;%) {
164 10     10   20 my %options = @_;
165 10 50 33     88 return if $ENV{HARNESS_ACTIVE} || $INC{'Test/Classy.pm'};
166             Module::New::Queue->register(sub {
167 0     0     my $self = shift;
168            
169 0           my $context = Module::New->context;
170 0 0         return if $options{optional};
171            
172 0   0       my $editor = $context->config('editor') || $ENV{EDITOR};
173 0 0         unless ( $editor ) { carp 'editor is not set'; return; }
  0            
  0            
174 0   0       my $file = $options{file} || $context->mainfile;
175 0           exec( _shell_quote($editor) => _shell_quote($file) );
176 0           });
177             },
178             };
179            
180             sub _shell_quote {
181 0     0     my $str = shift;
182 0 0         return $str unless $str =~ /\s/;
183 0 0         return ( $^O eq 'MSWin32' ) ? qq{"$str"} : qq{'$str'};
184             }
185            
186             1;
187            
188             __END__