App-MiseEnPlace
view release on metacpan or search on metacpan
lib/App/MiseEnPlace.pm view on Meta::CPAN
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 ) = @_;
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;
( run in 0.685 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )