Alien-MSYS2

 view release on metacpan or  search on metacpan

share/download.pl  view on Meta::CPAN

  
  sub find
  {
    my $path = shift;

    my $short = Win32::Shortcut->new;
    my $dh;
    opendir $dh, $path;
    foreach my $link_name (readdir $dh)
    {
      next if -d $link_name;
      my $link_path = File::Spec->catfile($path, $link_name);
      $short->Load($link_path) || next;
      my $bat = (split /\s+/, $short->{Arguments})[-1];
      next unless -f $bat;
      if($bat =~ /^(.*)[\\\/]mingw32_shell\.bat$/)
      {
        close $dh;
        return $1;
      }
    }

    closedir $dh;
  
    return;
  }
};

unless(defined $ENV{ALIEN_INSTALL_TYPE})
{
  print "You have not requested an install type.  I could not find MSYS2 on your system\n";
  print "By default, Alien::MSYS2 will only download MSYS2 into a share directory if you\n";
  print "request it by setting ALIEN_INSTALL_TYPE to share.\n";
  exit 2;
}

if(($ENV{ALIEN_INSTALL_TYPE}||'share') eq 'system')
{
  print "You requested a system install via the ALIEN_INSTALL_TYPE environment variable\n";
  print "But I was unable to find MSYS2 in on your system.  Please see the Alien::MSYS2\n";
  print "documentation for details.\n";
  exit 2;
}

my $dest = File::Spec->catdir($root, $Config{ptrsize} == 8 ? 'msys64' : 'msys32');

my $filename = "msys2-$arch-latest.tar.xz";

unless(-r $filename)
{
  my $url = "http://repo.msys2.org/distrib/$filename";
  print "Download $url\n";
  my $http_response = HTTP::Tiny->new->get($url);

  die "@{[ $http_response->{status} ]} @{[ $http_response->{reason} ]} on $url"
    unless $http_response->{success};

  my $fh;
  open($fh, '>', "$filename.tmp") 
    || die "unable to open $filename.tmp $!";
  binmode $fh;
  print($fh $http_response->{content}) 
    || die "unable to write to $filename.tmp $!";
  close($fh) 
    || die "unable to close $filename.tmp $!";
  rename("$filename.tmp" => $filename)
    || die "unable to rename $filename.tmp => $filename";
}

unless(-d $dest)
{
  my $ae = Archive::Extract->new( archive => $filename );
  print "Extract  $filename => $root\n";
  $ae->extract( to => $root ) || do{
    rmtree( $dest, 0, 0 );
    die "error extracting: @{[ $ae->error ]}";
  };
  
  if($^O eq 'MSWin32')
  {
    local $ENV{PATH} = $ENV{PATH};
    unshift @PATH, File::Spec->catdir($dest, qw( usr bin ));
    system 'bash', '-l', -c => 'true';
    system 'bash', '-l', -c => 'pacman -Syuu --noconfirm';
    system 'bash', '-l', -c => 'pacman -S make --noconfirm';
  }
  
  write_config(
    install_type => 'share',
    probe        => 'share',
  );
}

sub write_config
{
  my %config = @_;
  $config{msys2_root} =~ s{\\}{/}g if defined $config{msys2_root};
  $config{ptrsize} = $Config{ptrsize};
  mkpath $root, 0, 0755;  
  my $filename = File::Spec->catfile($root, 'alien_msys2.json');
  open my $fh, '>', $filename;
  print $fh encode_json(\%config);
  close $fh;
}



( run in 2.361 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )