App-GitGot

 view release on metacpan or  search on metacpan

lib/App/GitGot/Command/lib.pm  view on Meta::CPAN

package App::GitGot::Command::lib;
our $AUTHORITY = 'cpan:GENEHACK';
$App::GitGot::Command::lib::VERSION = '1.339';
# ABSTRACT: Generate a lib listing off a .gotlib file
use 5.014;

use List::Util qw/ uniq /;
use Path::Tiny;
use Types::Standard -types;

use App::GitGot -command;

use Moo;
extends 'App::GitGot::Command';
use namespace::autoclean;

sub options {
  my( $class , $app ) = @_;
  return (
    [ 'gotlib=s'    => 'gotlib file' => { default => '.gotlib' } ] ,
    [ 'libvar=s'    => 'library environment variable' => { default => 'PERL5LIB' } ] ,
    [ 'separator=s' => 'library path separator' => { default => ':' } ] ,
  );
}

sub _execute {
  my( $self, $opt, $args ) = @_;

  my @libs = map { $self->_expand_lib($_) } $self->_raw_libs( $args );

  no warnings; # $ENV{$self->opt->libvar} can be undefined
  say join $self->opt->separator, uniq @libs, split ':', $ENV{$self->opt->libvar};

}

sub _expand_lib {
  my( $self, $lib ) = @_;

  return path($lib)->absolute if $lib =~ m#^(?:\.|/)#;

  if ( $lib =~ s/^\@(\w+)// ) {
    # it's a tag
    return map { $_->path . $lib } $self->search_repos->tags($1)->all;
  }

  # it's a repo name
  $lib =~ s#^([^/]+)## or return;
  return map { $_->path . $lib } $self->search_repos->name($1)->all;

}

sub _raw_libs {
  my( $self, $args ) = @_;

  my $file = path( $self->opt->gotlib );

  return @$args,
    # remove comments and clean whitespaces
    grep { $_ }
    map { s/^\s+|#.*|\s+$//gr }
    ( -f $file ) ? $file->lines({ chomp => 1 }) : ();
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::GitGot::Command::lib - Generate a lib listing off a .gotlib file



( run in 0.573 second using v1.01-cache-2.11-cpan-0d23b851a93 )