Debian Codegraph @

lib/Analizo/Command/metrics.pm

raw
1 package Analizo::Command::metrics;
2 use Analizo -command;
3 use parent qw(Analizo::Command);
4 use strict;
5 use warnings;
6 use Analizo::Metrics;
7 use Analizo::Batch::Job::Directories;
8 use File::Basename;
9
10 # ABSTRACT: analizo's metric reporting tool
11
12 =head1 NAME
13
14 analizo-metrics - analizo's metric reporting tool
15
16 =head1 USAGE
17
18 analizo metrics [OPTIONS] [<input>]
19
20 =cut
21
22 sub usage_desc { "%c metrics %o [<input>]" }
23
24 sub opt_spec {
25 my ($class, $app) = @_;
26 return (
27 [ 'list|l', 'displays metric list' ],
28 [ 'extractor=s', 'which extractor method use to parse the source code' ],
29 [ 'globalonly|global-only|g', 'only output global (project-wide) metrics' ],
30 [ 'output|o=s', 'output file name' ],
31 [ 'language=s', 'process only filenames matching known extensions for the <lang>> programming' ],
32 [ 'exclude|x=s', 'exclude <dirs> (a colon-separated list of directories) from the analysis' ],
33 [ 'includedirs|I=s', 'include <dirs> (a colon-separated list of directories) with C/C++ header files', { default => '.' } ],
34 [ 'libdirs|L=s', 'include <dirs> (a colon-separated list of directories) with C/C++ static and dynamic libraries files', { default => '.' } ],
35 [ 'libs=s', 'include <dirs> (a colon-separated list of directories) with C/C++ linked libraries files', { default => '.' } ],
36 );
37 }
38
39 sub validate {
40 my ($self, $opt, $args) = @_;
41 if (@$args > 1) {
42 $self->usage_error('No more than one <input> is suported');
43 }
44 my @unreadable = grep { ! -r $_ || ! -e $_ } @$args;
45 if (@unreadable) {
46 foreach my $file (@unreadable) {
47 $self->usage_error("Input '$file' is not readable");
48 }
49 }
50 if ($opt->output && ! -w dirname($opt->output)) {
51 $self->usage_error("No such file or directory");
52 }
53 }
54
55 sub execute {
56 my ($self, $opt, $args) = @_;
57 if($opt->list){
58 my $metrics_handler = Analizo::Metrics->new(model => Analizo::Model->new);
59 my %metrics = $metrics_handler->list_of_metrics();
60 my %global_metrics = $metrics_handler->list_of_global_metrics();
61 print "Global Metrics:\n";
62 foreach my $key (sort keys %global_metrics){
63 print "$key - $global_metrics{$key}\n";
64 }
65 print "\nModule Metrics:\n";
66 foreach my $key (sort keys %metrics){
67 print "$key - $metrics{$key}\n";
68 }
69 exit 0;
70 }
71 my $tree = $args->[0] || '.';
72 my $job = Analizo::Batch::Job::Directories->new($tree);
73 $job->extractor($opt->extractor);
74 if ($opt->language) {
75 require Analizo::LanguageFilter;
76 if ($opt->language eq 'list') {
77 my @language_list = Analizo::LanguageFilter->list;
78 print "Languages:\n";
79 $" = "\n";
80 print "@language_list\n";
81 exit 0;
82 }
83 my $language_filter = Analizo::LanguageFilter->new($opt->language);
84 $job->filters($language_filter);
85 }
86 if ($opt->exclude) {
87 my @excluded_directories = split(':', $opt->exclude);
88 $job->exclude(@excluded_directories);
89 }
90 $job->includedirs($opt->includedirs);
91 $job->libdirs($opt->libdirs);
92 $job->libs($opt->libs);
93 $job->execute();
94 my $metrics = $job->metrics;
95 if ($opt->output) {
96 open STDOUT, '>', $opt->output or die "$!\n";
97 }
98 if ($opt->globalonly) {
99 print $metrics->report_global_metrics_only;
100 }
101 else {
102 print $metrics->report;
103 }
104 close STDOUT;
105 }
106
107 =head1 DESCRIPTION
108
109 analizo metrics analyzes source code in I<input> and produces a metrics
110 report. If I<input> is omitted, the current directory (I<.>) is assumed.
111
112 The produced report is written to the standard output, or to a file using the
113 I<--output> option, using the YAML format (see I<http://www.yaml.org/>)
114
115 analizo metrics is part of the analizo suite.
116
117 =head1 OPTIONS
118
119 =over
120
121 =item <input>
122
123 Tells analizo which source code directory you want to parse.
124
125 =item --extractor <extractor>
126
127 Define which extractor method use to parse the source code. Default is Doxyparse.
128
129 When using the Doxyparse extractor (default), all files matching the languages
130 supported by doxyparse are processed, unless I<--language> is used.
131
132 =item --list, -l
133
134 Displays metric list.
135
136 =item --output <file>, -o <file>
137
138 Writes the output to <file> instead of standard output.
139
140 =item --globalonly, --global-only, -g
141
142 Don't output the details about modules: only output global (project-wide) metrics.
143
144 =item --language <lang>
145
146 Process only filenames matching known extensions for the <I<lang>> programming
147 language. To see which languages are supported, pass B<--language list>.
148
149 =item --exclude <dirs>, -x <dirs>
150
151 Exclude <I<dirs>> (a colon-separated list of directories) from the analysis.
152 This is useful, for example, when you want to focus on production code and
153 exclude test code from the analysis. You could do that by passing something
154 like pass B<--exclude test>.
155
156 =back
157
158 =head1 OUTPUT FORMAT
159
160 The output is a stream of YAML documents. The first one presents metrics for
161 the project as a whole. The subsequent ones present per-module metrics, and thus
162 there will be as many of them as there are modules in your project.
163
164 =head1 COPYRIGHT AND AUTHORS
165
166 See B<analizo(1)>.
167
168 =cut
169
170 1;