Analizo

 view release on metacpan or  search on metacpan

lib/Analizo/Command/metrics_history.pm  view on Meta::CPAN

package Analizo::Command::metrics_history;
use Analizo -command;
use parent qw(Analizo::Command);
use strict;
use warnings;
use Analizo::Batch::Git;

#ABSTRACT: processes a Git repository collection metrics

=head1 NAME

analizo-metrics-history - processes a Git repository collection metrics

=head1 USAGE

  analizo metrics-history [OPTIONS] [<input>]

=cut

sub usage_desc { "%c metrics-history %o [<input>]" }

sub command_names { qw/metrics-history/ }

sub opt_spec {
  return (
    [ 'output|o=s',    'output file name' ],
    [ 'list|l',        'just print out the ids of the commits that would be processed '],
    [ 'language=s',    'process only filenames matching known extensions for the <lang> programming' ],
    [ 'exclude|x=s',   'exclude <dirs> (a colon-separated list of directories) from the analysis' ],
    [ 'parallel|p=i',  'activates support for parallel processing' ],
    [ 'format|f=s',    'specifies the output format', { default => 'csv' } ],
    [ 'progressbar|b', 'displays a progress bar during the execution' ],
  );
}

sub validate {
  my ($self, $opt, $args) = @_;
  unless ($self->output_driver($opt->format)) {
    $self->usage_error("Invalid output driver " . $opt->format);
  }
}

sub output_driver {
  my ($self, $format) = @_;
  my %available_outputs = (
    csv => 'Analizo::Batch::Output::CSV',
    db  => 'Analizo::Batch::Output::DB',
  );
  $available_outputs{$format};
}

sub load_output_driver {
  my ($self, $format) = @_;
  my $output_driver = $self->output_driver($format);
  eval "require $output_driver";
  return $output_driver->new;
}

sub execute {
  my ($self, $opt, $args) = @_;
  my $batch = Analizo::Batch::Git->new(@$args);
  if ($opt->list) {
    while (my $job = $batch->next()) {
      print $job->id, "\n";
    }
    exit 0;
  }
  if ($opt->language) {
    require Analizo::LanguageFilter;
    my $language_filter = Analizo::LanguageFilter->new($opt->language);
    $batch->filters($language_filter);
  }
  if ($opt->exclude) {
    my @excluded_directories = split(':', $opt->exclude);
    $batch->exclude(@excluded_directories);
  }
  my $output = $self->load_output_driver($opt->format);
  if ($opt->output) {
    $output->file($opt->output);
  }
  my $runner = undef;
  if ($opt->parallel) {
    require Analizo::Batch::Runner::Parallel;
    $runner = Analizo::Batch::Runner::Parallel->new($opt->parallel);
  } else {
    require Analizo::Batch::Runner::Sequential;
    $runner = Analizo::Batch::Runner::Sequential->new;
  }
  if ($opt->progressbar) {
    require Term::ProgressBar;
    my $progressbar = Term::ProgressBar->new({ count => 100, ETA => 'linear' });
    $runner->progress(
      sub {
        my ($job, $done, $total) = @_;
        $progressbar->update(100 * $done / $total);
      }



( run in 1.407 second using v1.01-cache-2.11-cpan-d01c6094234 )