CLI/Command/clean.pm

filter: Statementstmt 27.2% 18 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.2% 6 / 17 Podpod n/a totaltotal 24.0% 24 / 100 CC 10 SCAR 20.1
CC
10
Cov
22%
delete_file
20.0
pmc_to_mopc
12.0
filter_file
12.0
CRAP 7.5
« CLI/Command/compile.pm ↑ Summary CLI.pm »
1 #!/usr/bin/perl
2  
3 package MooseX::Compile::CLI::Command::clean;
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 extends qw(MooseX::Compile::CLI::Base);
7  
8 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MooseX::AttributeHelpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Prompt::ReadKey::Sequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Tie::RefHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has '+force' => ( documentation => "Delete without prompting." );
15  
16 has clean_includes => (
17 documentation => "The dirs argument implicitly gets all the 'inc' dirs as well.",
18 metaclass => "Getopt",
19 cmd_aliases => ["C"],
20 isa => "Bool",
21 is => "rw",
22 default => 0,
23 );
24  
25 has '+perl_inc' => (
26 documentation => "Also include '\@INC' in the 'inc' dirs. Defaults to true when 'clean_includes' is false.",
27 lazy => 1,
28 default => sub {
29 my $self = shift;
30 return not $self->clean_includes;
31 },
32 );
33  
34 augment run => sub {
35 my ( $self, $opts, $args ) = @_;
36  
37 $self->usage->die unless @{$self->classes} or @{$self->dirs};
38  
39 $self->clean_all_files;
40 };
41  
42 0 ▶sub clean_all_files {
Subroutine clean_all_files: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44  
45 0 $self->clean_files( $self->all_files );
46 }
47  
48 0 ▶sub clean_files {
Subroutine clean_files: not called CC=2 SCAR=17.9
49 0 my ( $self, @files ) = @_;
50  
51 0 my @delete = $self->should_delete(@files);
52  
53 0 $self->delete_file($_) for @delete;
54 }
55  
56 0 ▶sub should_delete {
Subroutine should_delete: not called CC=2 SCAR=17.9
57 0 my ( $self, @files ) = @_;
58  
59 0 ▶ return @files if $self->force;
Branch: 1 branch
BranchTrueFalseTotal
if $self->force 0 0 0
60  
61 0 my @ret;
62  
63 0 my @file_list = @files;
64  
65 0 my $file; # shared by while loop and these closures
66  
67 0 my $seq = $self->create_prompt_sequence(@file_list);
68  
69 0 my $answers = $seq->run;
70  
71 0 grep { $answers->{$_} eq 'yes' } @files;
72 }
73  
74 0 ▶sub create_prompt_sequence {
Subroutine create_prompt_sequence: not called CC=1 SCAR=6.9
75 0 my ( $self, @files ) = @_;
76  
77 0 my %options;
78 my @options = (
79 {
80 name => "yes",
81 doc => "delete this file and the associated .mopc file",
82 },
83 {
84 name => "no",
85 doc => "don't delete this file",
86 default => 1,
87 },
88 {
89 name => "rest",
90 doc => "delete all remaining files",
91 key => 'a',
92 sequence_command => 1,
93 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my ( $self, @args ) = @_;
95 0 $self->set_option_for_remaining_items( @args, option => $options{yes} );
96 },
97 },
98 {
99 name => "everything",
100 doc => "delete all files, including ones previously marked 'no'",
101 sequence_command => 1,
102 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 my ( $self, @args ) = @_;
104 0 $self->set_option_for_all_items( @args, option => $options{yes} );
105 },
106 },
107 {
108 name => "none",
109 key => "d",
110 doc => "don't delete any more files, but do delete the ones specified so far",
111 sequence_command => 1,
112 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 my ( $self, @args ) = @_;
114 0 $self->set_option_for_remaining_items( @args, option => $options{yes} );
115 },
116 },
117 {
118 name => "quit",
119 doc => "exit, without deleting any files",
120 sequence_command => 1,
121 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my ( $self, @args ) = @_;
123 0 $self->set_option_for_all_items( @args, option => $options{no} );
124 },
125 },
126 0 );
127  
128 0 %options = map { $_->{name} => $_ } @options;
129  
130 0 tie my %file_args, 'Tie::RefHash';
131  
132 %file_args = map {
133 0 my $file = $_;
134  
135 0 my $name = $file->{rel};
136 0 $name =~ s/\.pmc$/.{pmc,mopc}/;
137  
138 0 $file => {
139 %$file,
140 filename => $name,
141 };
142 } @files;
143  
144 0 Prompt::ReadKey::Sequence->new(
145 default_prompt => "Clean up class '%(class)s' (%(filename)s in %(dir)s)?",
146 items => \@files,
147 item_arguments => \%file_args,
148 default_options => \@options,
149 );
150 }
151  
152 0 ▶sub delete_file {
Subroutine delete_file: not called CC=4 SCAR=30.0
153 0 my ( $self, $file ) = @_;
154  
155 0 foreach my $file ( @{ $file }{qw(file mopc)} ) {
156 0 ▶ warn "Deleting $file\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
157 0 ▶ $file->remove or die "couldn't unlink $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$file->remove or die "couldn't unlink ${file}: $!" 0 0 0
158 }
159 }
160  
161 0 ▶sub pmc_to_mopc {
Subroutine pmc_to_mopc: not called CC=3 SCAR=24.8
162 0 my ( $self, $pmc_file ) = @_;
163  
164 0 my $pmc_basename = $pmc_file->basename;
165  
166 0 ▶ ( my $mopc_basename = $pmc_basename ) =~ s/\.pmc$/.mopc/ or return;
Branch: 1 branch
BranchTrueFalseTotal
(my $mopc_basename = $pmc_basename) =~ s/\.pmc$/.mopc/ or return 0 0 0
167  
168 0 my $mopc_file = $pmc_file->parent->file($mopc_basename);
169  
170 0 ▶ return $mopc_file if -f $mopc_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $mopc_file 0 0 0
171  
172 0 return;
173 }
174  
175 override file_in_dir => sub {
176 my ( $self, %args ) = @_;
177  
178 my $entry = super();
179  
180 $entry->{mopc} = $self->pmc_to_mopc($entry->{file}) or return;
181  
182 return $entry;
183 };
184  
185 override class_to_filename => sub {
186 my ( $self, $class ) = @_;
187 super() . "c"; # we are only interested in pmc files
188 };
189  
190 0 ▶sub filter_file {
Subroutine filter_file: not called CC=3 SCAR=24.8
191 0 my ( $self, $file ) = @_;
192  
193 0 ▶ return $file if $file->basename =~ m/\.pmc$/ and -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file->basename =~ /\.pmc$/ and -f $file 0 0 0
Condition0%0/3
$file->basename =~ /\.pmc$/ and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file->basename =~ /\.pmc$/ and -f $file
$file->basename =~ /\.pmc$/ -f $file
Truth table0%0/3
A and B
A= $file->basename =~ /\.pmc$/B= -f $file
ABresult
0X0
100
111
194  
195 0 return;
196 }
197  
198 augment build_from_opts => sub {
199 my ( $self, $opts, $args ) = @_;
200  
201 $self->add_to_dirs( $self->inc ) if $self->clean_includes;
202 };
203  
204 __PACKAGE__
205  
206 __END__
« CLI/Command/compile.pm ↑ Summary CLI.pm »