App/Perlanalyst.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 273 CC 17 SCAR 32.0
CC
17
Cov
0%
run
110.0
_files
20.0
show_version
12.0
CRAP 24.6
1 package App::Perlanalyst;
2 {
3 $App::Perlanalyst::VERSION = '0.004'; # TRIAL
4 }
5  
6 # ABSTRACT: main package for the perlanalyst tool
7  
8  
9 use strict;
10 use warnings;
11  
12 use Cwd;
13 use English qw( -no_match_vars ); # Avoids regex performance penalty
14 use Getopt::Long;
15 use IO::Interactive qw(is_interactive);
16 use List::Util qw(sum);
17 use Module::Runtime qw(use_module);
18 use Term::ANSIColor qw(colored);
19 use Term::ProgressBar::Simple;
20  
21 use Perl::Analysis::Static::Question;
22 use Perl::Analysis::Static::Document;
23 use Perl::Analysis::Static::Files;
24 use Perl::Analysis::Static::Questioner;
25  
26 our $COPYRIGHT = 'Copyright 2011 Gregor Goldbach.';
27  
28  
29 sub new {
30 return bless {}, shift;
31 }
32  
33  
34 sub process_args {
35 my ( $self, @args ) = @_;
36  
37 # Getopt::Long processes ARGV
38 local @ARGV = @args;
39 Getopt::Long::Configure(qw(no_ignore_case bundling pass_through));
40  
41 GetOptions(
42 'a|analysis|all=s' => \$self->{analysis},
43 'f|filter=s' => \@{ $self->{filter} },
44 'h|help|?' => \$self->{show_help},
45 'man' => sub {
46 require Pod::Usage;
47 Pod::Usage::pod2usage( { -verbose => 2 } );
48 exit;
49 },
50 'q|question=s' => \$self->{question},
51 'Q|question-arguments=s' => \$self->{question_arguments},
52 'v|verbose!' => \$self->{verbose},
53 'list-analyses!' => \$self->{list_analyses},
54 'list-filters!' => \$self->{list_filters},
55 'list-files!' => \$self->{list_files},
56 'list-questions!' => \$self->{list_questions},
57 'version' => \$self->{show_version}
58 )
59 or App::Perlanalyst::die('Unable to parse options');
60  
61 # Stash the remainder of argv for later
62 $self->{argv} = [@ARGV];
63 }
64  
65 sub run {
66 my ($self) = @_;
67  
68 # Show version or help?
69 return show_version() if $self->{show_version};
70 return show_help() if $self->{show_help};
71  
72 # List what we can do
73 return $self->_list_analyses() if $self->{list_analyses};
74 return $self->_list_filters() if $self->{list_filters};
75 return $self->_list_questions() if $self->{list_questions};
76 return $self->_list_files() if $self->{list_files};
77  
78 # There is neither an analysis or a question, so we'll give help
79 return show_help() unless $self->{analysis} or $self->{question};
80  
81 # No, so we will ask a question. What files do we have?
82 $self->{files} = $self->_files();
83  
84 # Setup or nice progress bar based on the file sizes.
85 $self->_setup_progress_bar();
86  
87 # Just run an analysis if we're told to do so.
88 return $self->analyse() if $self->{analysis};
89  
90 # No analysis wanted, shall we run a question?
91 return $self->_ask_question() if $self->{question};
92  
93 # We should never get here. If we do, the logic above is strange
94 # and we have a bug.
95 _bug('Strange logic in run()');
96 }
97  
98  
99 # taken from App::Ack
100 sub die {
101 my $program = File::Basename::basename($0);
102 return CORE::die( $program, ': ', @_, "\n" );
103 }
104  
105  
106 sub _bug {
107 my ($message) = @_;
108 $message .= '. This is a bug. Please report it so we can fix it.';
109 &die($message); # The ampersand calls the die() in this package.
110 }
111  
112 sub analyse {
113 my ($self) = @_;
114  
115 my $element_class = $self->{analysis};
116  
117 # preprend Perl::Analysis::Static::Element
118 $element_class = 'Perl::Analysis::Static::Element::' . $element_class;
119  
120 my @filters;
121 my @arguments;
122 for my $filter ( @{ $self->{filter} } ) {
123  
124 # split filter and arguments
125 my ( $f, $args ) = split( /=/, $filter );
126  
127 push @filters, $f;
128 push @arguments, $args;
129 }
130  
131 my $question = Perl::Analysis::Static::Question->new(
132 class => $element_class,
133 filter => \@filters,
134 arguments => \@arguments
135 );
136  
137 return $self->_ask_and_print($question);
138 }
139  
140  
141 sub show_help {
142 print( <<"END_OF_HELP" );
143 Usage: perlanalyst [OPTION]... [FILES]...
144  
145 Analyse your Perl documents in the tree from the current directory on down.
146 If [FILES] is specified, then only those files/directories are checked.
147  
148 Examples: perlanalyst --all Sub
149  
150 Analyses:
151 --analysis Specify what analysis to run.
152 --list-analyses List all analyses that may be run.
153 Filtering:
154 --filter Specify what filter to run on the list of elements found.
155 May be specified more than once.
156 Arguments to the filter may be given after an equal sign.
157 (e.g. --filter Name=foo)
158 --list-filters List all filters that may be used.
159  
160 Questions:
161 --question Specify what question to ask.
162 Arguments to the question may be given after an equal sign.
163 (e.g. --question Sub::Name=foo)
164 --list-questions List all questions that may be called.
165  
166 Files:
167 --list-files List the files that would be examined.
168  
169 Miscellaneous:
170 --help This help
171 --man man page
172 --version Display version & copyright
173  
174 This is version $App::Perlanalyst::VERSION of the perlanalyst.
175 END_OF_HELP
176 return;
177 }
178  
179  
180 sub _setup_progress_bar {
181 my ($self) = @_;
182  
183 # we expect the time of an analyses to be a function of the
184 # file's size
185 my $total_size = sum map {-s} @{ $self->{files} };
186 my $params = {
187 name => 'Files',
188 count => $total_size
189 };
190  
191 $self->{progress_bar} = Term::ProgressBar::Simple->new($params);
192 }
193  
194 sub _increment_progress_bar {
195 my ( $self, $file ) = @_;
196 $self->{progress_bar}->increment( -s $file );
197 }
198  
199  
200 sub _files {
201 my ($self) = @_;
202 my @files;
203  
204 $self->{argv} = [ getcwd() ] unless @{ $self->{argv} };
205  
206 # use map?
207 for my $arg ( @{ $self->{argv} } ) {
208  
209 # is it a file?
210 if ( -f $arg ) {
211 push @files, $arg;
212 next;
213 }
214  
215 # is it a directory?
216 if ( -d $arg ) {
217 push @files, @{ Perl::Analysis::Static::Files::files($arg) };
218 next;
219 }
220 App::Perlanalyst::die "'$arg' is neither file nor directory";
221 }
222 return \@files;
223 }
224  
225  
226 sub _display_filename {
227 my ($filename) = @_;
228  
229 # remove cwd from the file to make it shorter and readable
230 my $cwd = getcwd();
231 $filename =~ s{$cwd}{.};
232  
233 # colour it if called interactive so we don't get the ANSI sequences
234 # if the results are piped
235 return colored( $filename, 'bold blue' ) if is_interactive;
236 return $filename;
237 }
238  
239 sub _display_elements_for_file {
240 my ( $self, $elements, $filename ) = @_;
241  
242 # Don't display the filename if there are no elements.
243 return unless $elements;
244  
245 print _display_filename($filename) . ':' . "\n";
246  
247 for my $element (@$elements) {
248 print $element->stringify() . "\n";
249 }
250 }
251  
252  
253 sub _ask_question {
254 my ($self) = @_;
255  
256 my $q = $self->{question};
257 my ( $question_class, $args ) = split( /=/, $q );
258  
259 # preprend Perl::Analysis::Static::Question
260 $question_class = 'Perl::Analysis::Static::Question::' . $question_class;
261  
262 # load the question's module
263 use_module($question_class);
264  
265 # create instance and set its arguments
266 my $question = $question_class->new();
267 $question->set_arguments($args);
268  
269 return $self->_ask_and_print($question);
270 }
271  
272  
273 sub _ask_and_print {
274 my ($self, $question)=@_;
275  
276 # Ask the question for every file and store the answer.
277 # Increment the progress bar after we got it.
278 # This would be the perfect place to take the time.
279 my $questioner = Perl::Analysis::Static::Questioner->new();
280  
281 my $collector =
282 $questioner->ask_for_files( $question, $self->{files},
283 sub { $self->_increment_progress_bar(shift) } );
284  
285 return $self->_print_answer($collector);
286 }
287  
288  
289 sub _print_answer {
290 my ( $self, $collector ) = @_;
291  
292 for my $filename ( sort $collector->filenames ) {
293 my $answer = $collector->get_answer($filename);
294 $self->_display_elements_for_file( $answer->elements, $filename );
295 }
296  
297 return 1;
298 }
299  
300  
301 sub _list_modules {
302 my ( $self, $kind, $name ) = @_;
303  
304 use_module('Perl::Analysis::Static::ModuleLister');
305 my $lister=Perl::Analysis::Static::ModuleLister->new(kind=>$kind);
306 my $modules=$lister->list();
307  
308 # the keys are the names, sort them for convenience
309 my @result = sort @$modules;
310  
311 # print them with a simple loop
312 print "These are the available $name:\n";
313 for my $module (@result) {
314 print $module. "\n";
315 }
316  
317 return 1;
318 }
319  
320  
321 sub _list_filters {
322 my ($self) = @_;
323 return $self->_list_modules( 'Filter', 'filters' );
324 }
325  
326  
327 sub _list_analyses {
328 my ($self) = @_;
329 return $self->_list_modules( 'Analysis', 'analyses' );
330 }
331  
332  
333 sub _list_questions {
334 my ($self) = @_;
335 return $self->_list_modules( 'Question', 'questions' );
336 }
337  
338 sub _list_files {
339 my ($self) = @_;
340 my $files = $self->_files;
341 my @display_names = map { _display_filename($_) } @$files;
342  
343 print "$_\n" for @display_names;
344 }
345  
346 # stolen from App::Ack's get_version_statement
347  
348  
349 sub show_version {
350 require Config;
351  
352 my $this_perl = $Config::Config{perlpath};
353 if ( $^O ne 'VMS' ) {
354 my $ext = $Config::Config{_exe};
355 $this_perl .= $ext unless $this_perl =~ m/$ext$/i;
356 }
357 my $ver = sprintf( '%vd', $^V );
358  
359 print <<"END_OF_VERSION";
360 perlanalyst $App::Perlanalyst::VERSION
361 Running under Perl $ver at $this_perl
362  
363 $COPYRIGHT
364  
365 This program is free software; you can redistribute it and/or modify it
366 under the terms as the Perl 5 programming language system itself.
367 END_OF_VERSION
368 }
369  
370  
371 1;
372  
373 __END__