Alien-SDL2

 view release on metacpan or  search on metacpan

inc/My/Builder.pm  view on Meta::CPAN

}

# pure perl implementation of patch functionality
sub apply_patch {
  my ($self, $dir_to_be_patched, $patch_file) = @_;
  my ($src, $diff);

  undef local $/;
  open(DAT, $patch_file) or die "###ERROR### Cannot open file: '$patch_file'\n";
  $diff = <DAT>;
  close(DAT);
  $diff =~ s/\r\n/\n/g; #normalise newlines
  $diff =~ s/\ndiff /\nSpLiTmArKeRdiff /g;
  my @patches = split('SpLiTmArKeR', $diff);

  print STDERR "Applying patch file: '$patch_file'\n";
  foreach my $p (@patches) {
    my ($k) = $p =~ /^---\s*([\S]+)/;
    # doing the same like -p1 for 'patch'
    $k =~ s|\\|/|g;
    $k =~ s|^[^/]*/(.*)$|$1|;
    $k = catfile($dir_to_be_patched, $k);
    print STDERR "- gonna patch '$k'\n";

    if (-f $k) {
      open(SRC, "<", $k) or die "###ERROR### Cannot open file: '$k'\n";
      $src  = <SRC>;
      close(SRC);
      $src =~ s/\r\n/\n/g; #normalise newlines
    }
    else {
      $src = "";
    }

    my $out = eval { Text::Patch::patch( $src, $p, { STYLE => "Unified" } ) };
    if ($out) {
      open(OUT, ">", $k) or die "###ERROR### Cannot open file for writing: '$k'\n";
      print(OUT $out);
      close(OUT);
    }
    else {
      warn "###WARN### Patching '$k' failed: $@";
    }
  }
}

sub run_output_on_error {
  my ($self, $limit, $cmd) = @_;
  my $output;
  my $c = ref($cmd) eq 'ARRAY' ? join(' ',@$cmd) : $cmd;
  print STDERR "CMD: $c\n";
  print STDERR "- running (stdout+stderr redirected)...\n";
  my $rv = run3($cmd, \undef, \$output, \$output, { return_if_system_error => 1 } );
  my $success = ($rv == 1 && $? == 0) ? 1 : 0;
  if ($success) {
    print STDERR "- finished successfully (output suppressed)\n";
  }
  else {
    $output = substr $output, -$limit if defined $limit; # we want just last N chars
    if (!defined($limit)) {
      print STDERR "OUTPUT:\n", $output, "\n";
    }
    elsif ($limit>0) {
      print STDERR "OUTPUT: (only last $limit chars)\n", $output, "\n";
    }
  }
  return $success;
}

sub run_output_std {
  my ($self, $cmd) = @_;
  my $c = ref($cmd) eq 'ARRAY' ? join(' ',@$cmd) : $cmd;
  print STDERR "CMD: $c\n";
  my $rv = run3($cmd, undef, undef, undef, { return_if_system_error => 1 } );
  my $success = ($rv == 1 && $? == 0) ? 1 : 0;
  print STDERR "- finished successfully\n" if ($success);
  return $success;
}

sub run_custom {
  my ($self, $cmd) = @_;
  my $rv;
  if ($self->notes('build_msgs')) {
    $rv = $self->run_output_std($cmd);
  }
  else {
    $rv = $self->run_output_on_error(undef, $cmd);
  }
  #warn "###WARN### error during run_custom()" unless $rv;
  return $rv;
}

sub run_stdout2str {
  my ($self, $cmd) = @_;
  my $output;
  my $rv = run3($cmd, \undef, \$output, \undef, { return_if_system_error => 1 } );
  $output =~ s/[\r\n]*$//;
  return $output;
}

1;



( run in 1.397 second using v1.01-cache-2.11-cpan-0c5ce583b80 )