App-MiseEnPlace

 view release on metacpan or  search on metacpan

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

  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 ) = @_;

  my( $src , $target ) = @$linkpair;

  my $msg;

  if ( ! -e $src ) {
    $msg = colored( 'ERROR:  src does not exist' , 'bold white on_red' )
  }
  elsif( -e -l $target ) {
    if ( readlink $target eq $src ) {
      $msg = colored('exists ','green') if $self->verbose;
    }
    else {
      unlink $target;
      symlink $src , $target;
      $msg = colored( 'fixed' , 'bold black on_yellow' ) . '  ';
    }
  }
  elsif ( -e $target ) {
    $msg = colored( 'ERROR:  blocked by existing file' , 'bold white on_red' );
  }
  else {
    symlink $src , $target;
    $msg = colored( 'created' , 'bold black on_green' );
  }

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

sub _load_configs {
  my( $self ) = shift;

  unless ( -e $self->config_file() ) {
    say "Whoops, it looks like you don't have a " . $self->config_file() . " file yet.";
    say "Please review the documentation, create one, and try again.";
    exit;
  }

  my $base_config = _load_config_file( $self->config_file() );

  my @links = map { _parse_linkpair( $_ , $self->homedir() ) } @{ $base_config->{create}{links} };

  my @dirs = map { _prepend_dir( $_ , $self->homedir() ) } @{ $base_config->{create}{directories} };

  my @managed_dirs = map { glob _prepend_dir( $_ , $self->homedir() ) } @{ $base_config->{manage} };

  for my $managed_dir ( @managed_dirs ) {
    my $mise_file = path( $managed_dir , '.mise' )->stringify();

    if ( -e -r $mise_file ) {
      my $config = _load_config_file( $mise_file );

      for ( @{ $config->{create}{directories} } ) {
        push @dirs , _prepend_dir( $_ , $managed_dir );
      }

      for ( @{ $config->{create}{links} } ) {
        push @links , _parse_linkpair( $_ , $managed_dir );
      }
    }
  }

  $self->directories( \@dirs );

  $self->links( $self->_parse_create_links( \@links ) );
}

sub _load_config_file {
  my $file = shift;

  my $config;

  try { $config = LoadFile( glob($file) ) }
  catch {
    say "Failed to parse config file $file:\n\t$_";
    exit;
  };

  return $config;
}

sub _parse_create_links {
  my( $self, $link_array ) = @_;

  my( %link_targets , @links );

  for my $link_pair ( @$link_array ) {
    my( $src , $target ) = ( %$link_pair );

    my $src_base = path( $src )->basename();

    $target = $self->bindir() if $target =~ m'BIN$';
    $target = path($target, $src_base)->stringify()
      if path($target)->is_dir() and ! path( $src )->is_dir();



( run in 3.258 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )