App-MiseEnPlace

 view release on metacpan or  search on metacpan

lib/App/MiseEnPlace.pm  view on Meta::CPAN

use File::HomeDir;
use Path::Tiny;
use Term::ANSIColor;
use Try::Tiny;
use Types::Standard -types;
use YAML qw/ LoadFile /;

use Moo;
use MooX::HandlesVia;

has bindir => (
  is      => 'rw' ,
  isa     => Str ,
  lazy    => 1 ,
  default => sub { path( File::HomeDir->my_home , 'bin' )->stringify } ,
);

has config_file => (
  is      => 'rw' ,
  isa     => Str ,
  lazy    => 1 ,
  default => sub { path( File::HomeDir->my_home() , '.mise' )->stringify() } ,
);

has 'directories' => (
  is          => 'rw' ,
  isa         => ArrayRef[Str] ,
  handles_via => 'Array' ,
  handles     => { all_directories => 'elements' } ,
);

has 'homedir' => (
  is      => 'rw' ,
  isa     => Str ,
  lazy    => 1 ,
  default => sub { path( File::HomeDir->my_home )->stringify() } ,
);

has 'links' => (
  is          => 'rw' ,
  isa         => ArrayRef[ArrayRef[Str]] ,
  handles_via => 'Array' ,
  handles     => { all_links => 'elements' } ,
);

has 'verbose' => (
  is      => 'rw' ,
  isa     => Bool ,
  default => 0 ,
);

sub opt_spec {
  return (
    [ 'config|C=s'         => 'config file location (default = ~/.mise)' ] ,
    [ 'remove-bin-links|R' => 'remove all links from ~/bin at beginning of run' ] ,
    [ 'verbose|v'          => 'be verbose' ] ,
    [ 'version|V'          => 'show version' ] ,
  );
}

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

  $self->usage_error( "No args needed" ) if @$args;

  if ( $opt->{version} ) {
    say $App::MiseEnPlace::VERSION;
    exit;
  }

  $self->config_file( $opt->{config} ) if $opt->{config};
  $self->verbose( $opt->{verbose} )    if $opt->{verbose};
}

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

  # set up colored output if we page thru less
  # also exit pager immediately if <1 page of output
  $ENV{LESS} = 'RFX';

  # don't catch any errors here; if this fails we just output stuff like
  # normal and nobody is the wiser.
  eval 'use IO::Page';

  $self->_load_configs();

  $self->_remove_bin_links($opt)
    if $opt->{remove_bin_links} and -e -d $self->bindir();

  $self->_create_dir( $_ ) for $self->all_directories();

  $self->_create_link( $_ ) for $self->all_links();
}

sub _create_dir {
  my( $self , $dir ) = @_;

  my $msg;

  if( -e -d $dir ) {
    $msg = colored('exists ','green') if $self->verbose();
  }
  elsif( -e $dir and ! -l $dir ) {
    $msg = colored('ERROR: blocked by non-dirctory','bold white on_red');
  }
  else {
    path( $dir )->mkpath();
    $msg = colored('created','bold black on_green');
  }

  my $home = $self->homedir();
  if ( $msg ) {
    $dir =~ s/^$home/~/;
    say "[ DIR] $msg $dir";
  }
}

sub _create_link {
  my( $self , $linkpair ) = @_;



( run in 0.863 second using v1.01-cache-2.11-cpan-5837b0d9d2c )