fix powershell output for use with invoke-expression
[p5sagit/local-lib.git] / lib / local / lib.pm
index b3953b1..68baba7 100644 (file)
@@ -109,6 +109,8 @@ sub _as_list {
 }
 sub _remove_from {
   my ($list, @remove) = @_;
+  return @$list
+    if !@remove;
   my %remove = map { $_ => 1 } @remove;
   grep !$remove{$_}, _as_list($list);
 }
@@ -205,15 +207,18 @@ sub deactivate_all {
 
   my @active_lls = $self->active_paths;
 
-  my %args = (
-    bins => [ _remove_from($self->bins,
-      map $self->install_base_bin_path($_), @active_lls) ],
-    libs => [ _remove_from($self->libs,
-      map $self->install_base_perl_path($_), @active_lls) ],
-    inc => [ _remove_from($self->inc,
-      map $self->lib_paths_for($_), @active_lls) ],
-    roots => [ _remove_from($self->roots, @active_lls) ],
-  );
+  my %args;
+  if (@active_lls) {
+    %args = (
+      bins => [ _remove_from($self->bins,
+        map $self->install_base_bin_path($_), @active_lls) ],
+      libs => [ _remove_from($self->libs,
+        map $self->install_base_perl_path($_), @active_lls) ],
+      inc => [ _remove_from($self->inc,
+        map $self->lib_paths_for($_), @active_lls) ],
+      roots => [ _remove_from($self->roots, @active_lls) ],
+    );
+  }
 
   $args{extra} = $self->installer_options_for(undef);
 
@@ -232,16 +237,19 @@ sub activate {
 
   my @active_lls = $self->active_paths;
 
-  if (grep { $_ eq $path } @active_lls) {
+  if (grep { $_ eq $path } @active_lls[1 .. $#active_lls]) {
     $self = $self->deactivate($path);
   }
 
-  my %args = (
-    bins  => [ $self->install_base_bin_path($path), @{$self->bins} ],
-    libs  => [ $self->install_base_perl_path($path), @{$self->libs} ],
-    inc   => [ $self->lib_paths_for($path), @{$self->inc} ],
-    roots => [ $path, @{$self->roots} ],
-  );
+  my %args;
+  if (!@active_lls || $active_lls[0] ne $path) {
+    %args = (
+      bins  => [ $self->install_base_bin_path($path), @{$self->bins} ],
+      libs  => [ $self->install_base_perl_path($path), @{$self->libs} ],
+      inc   => [ $self->lib_paths_for($path), @{$self->inc} ],
+      roots => [ $path, @{$self->roots} ],
+    );
+  }
 
   $args{extra} = $self->installer_options_for($path);
 
@@ -313,13 +321,20 @@ sub environment_vars_string_for {
         && ${$value->[0]} eq $name) {
       next;
     }
-    if (!ref $value
-        && defined $value ? $value eq $ENV{$name} : !defined $ENV{$name}
+    if (
+        !ref $value
+        and defined $value
+          ? (defined $ENV{$name} && $value eq $ENV{$name})
+          : !defined $ENV{$name}
     ) {
       next;
     }
     $out .= $self->$build_method($name, $value);
   }
+  my $wrap_method = 'wrap_' . $self->shelltype . '_output';
+  if ($self->can($wrap_method)) {
+    return $self->$wrap_method($out);
+  }
   return $out;
 }
 
@@ -349,8 +364,12 @@ sub build_powershell_env_declaration {
   my ($class, $name, $args) = @_;
   my $value = $class->_interpolate($args, '$env:', '', '"', '`');
   defined $value
-    ? qq{\$env:$name = "$value"\n}
-    : "Remove-Item Env:\\$name\n";
+    ? qq{\$env:$name = "$value";\n}
+    : "Remove-Item Env:\\$name;\n";
+}
+sub wrap_powershell_output {
+  my ($class, $out) = @_;
+  return $out || " \n";
 }