]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blobdiff - lib/CPANPLUS/Dist/Gentoo.pm
Add the _abort(), _notify() and _skip() diagnostics methods
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo.pm
index 177c70f91df1e6db4d1c5b94442fa504d3d2b9be..af542eed5f6858177d1ba180674f78595ad7ab55 100644 (file)
@@ -4,13 +4,13 @@ use strict;
 use warnings;
 
 use Cwd qw/abs_path/;
-use File::Copy qw/copy/;
-use File::Path qw/mkpath/;
-use File::Spec::Functions qw/catdir catfile/;
+use File::Copy ();
+use File::Path ();
+use File::Spec;
 
 use IPC::Cmd qw/run can_run/;
 
-use CPANPLUS::Error;
+use CPANPLUS::Error ();
 
 use base qw/CPANPLUS::Dist::Base/;
 
@@ -64,12 +64,12 @@ my $main_portdir;
 
 my %forced;
 
-sub _unquote {
+my $unquote = sub {
  my $s = shift;
  $s =~ s/^["']*//;
  $s =~ s/["']*$//;
  return $s;
-}
+};
 
 my $format_available;
 
@@ -78,7 +78,7 @@ sub format_available {
 
  for my $prog (qw/emerge ebuild/) {
   unless (can_run($prog)) {
-   error "$prog is required to write ebuilds -- aborting";
+   __PACKAGE__->_abort("$prog is required to write ebuilds");
    return $format_available = 0;
   }
  }
@@ -90,19 +90,19 @@ sub format_available {
                                buffer  => \$buffers;
   if ($success) {
    if ($buffers =~ /^PORTDIR_OVERLAY=(.*)$/m) {
-    $overlays = [ map abs_path($_), split ' ', _unquote($1) ];
+    $overlays = [ map abs_path($_), split ' ', $unquote->($1) ];
    }
    if ($buffers =~ /^ACCEPT_KEYWORDS=(.*)$/m) {
-    $default_keywords = [ split ' ', _unquote($1) ];
+    $default_keywords = [ split ' ', $unquote->($1) ];
    }
    if ($buffers =~ /^DISTDIR=(.*)$/m) {
-    $default_distdir = abs_path(_unquote($1));
+    $default_distdir = abs_path($unquote->($1));
    }
    if ($buffers =~ /^PORTDIR=(.*)$/m) {
-    $main_portdir = abs_path(_unquote($1));
+    $main_portdir = abs_path($unquote->($1));
    }
   } else {
-   error $errmsg;
+   __PACKAGE__->_abort($errmsg);
   }
  }
 
@@ -138,7 +138,8 @@ sub prepare {
 
  my %opts = @_;
 
- $stat->prepared(0);
+ my $OK   = sub { $stat->prepared(1); 1 };
+ my $FAIL = sub { $stat->prepared(0); $self->_abort(@_) if @_; 0 };
 
  my $keywords = delete $opts{'keywords'};
  if (defined $keywords) {
@@ -179,15 +180,14 @@ sub prepare {
  $stat->distdir($distdir);
 
  if ($stat->do_manifest && !-w $stat->distdir) {
-  error 'distdir isn\'t writable -- aborting';
-  return 0;
+  return $FAIL->('distdir isn\'t writable');
  }
  $stat->fetched_arch($mod->status->fetch);
 
- my $cur = File::Spec::Functions::curdir();
+ my $cur = File::Spec->curdir();
  my $portdir_overlay;
  for (@$overlays) {
-  if ($_ eq $overlay or File::Spec::Functions::abs2rel($overlay, $_) eq $cur) {
+  if ($_ eq $overlay or File::Spec->abs2rel($overlay, $_) eq $cur) {
    $portdir_overlay = [ @$overlays ];
    last;
   }
@@ -210,10 +210,16 @@ sub prepare {
 
  $stat->eb_name(CPANPLUS::Dist::Gentoo::Maps::name_c2g($name));
 
- $stat->eb_dir(catdir($stat->overlay, CATEGORY, $stat->eb_name));
+ $stat->eb_dir(File::Spec->catdir(
+  $stat->overlay,
+  CATEGORY,
+  $stat->eb_name,
+ ));
 
- my $file = catfile($stat->eb_dir,
-                    $stat->eb_name . '-' . $stat->eb_version . '.ebuild');
+ my $file = File::Spec->catfile(
+  $stat->eb_dir,
+  $stat->eb_name . '-' . $stat->eb_version . '.ebuild',
+ );
  $stat->eb_file($file);
 
  if (-e $file) {
@@ -224,10 +230,10 @@ sub prepare {
     $forced{$file} = 1;
     $skip = 0;
    } else {
-    error "Can't force rewriting of $file -- skipping";
+    $self->_skip("Can't force rewriting of $file");
    }
   } else {
-   msg 'Ebuild already generated for ' . $stat->distribution . ' -- skipping';
+   $self->_skip('Ebuild already generated for', $stat->distribution);
   }
   if ($skip) {
    $stat->prepared(1);
@@ -248,8 +254,7 @@ sub prepare {
  $stat->uri('http://search.cpan.org/dist/' . $name);
 
  unless ($author =~ /^(.)(.)/) {
-  error 'Wrong author name -- aborting';
-  return 0;
+  return $FAIL->('Wrong author name');
  }
  $stat->src("mirror://cpan/modules/by-authors/id/$1/$1$2/$author/"
             . $mod->package);
@@ -261,10 +266,7 @@ sub prepare {
  for my $prereq (sort keys %$prereqs) {
   next if $prereq =~ /^perl(?:-|\z)/;
   my $obj = $int->module_tree($prereq);
-  unless ($obj) {
-   error 'Wrong module object -- aborting';
-   return 0;
-  }
+  return $FAIL->('Wrong module object') unless $obj;
   next if $obj->package_is_perl_core;
   {
    my $version;
@@ -280,34 +282,31 @@ sub prepare {
  }
  $stat->deps(\@depends);
 
- $stat->prepared(1);
- return 1;
+ return $OK->();
 }
 
 sub create {
  my $self = shift;
  my $stat = $self->status;
 
+ my $OK   = sub { $stat->created(1); $stat->dist($stat->eb_file); 1 };
+ my $FAIL = sub { $stat->created(0); $stat->dist(undef); $self->_abort(@_) if @_; 0 };
+
  unless ($stat->prepared) {
-  error 'Can\'t create ' . $stat->distribution . ' since it was never prepared -- aborting';
-  $stat->created(0);
-  $stat->dist(undef);
-  return 0;
+  return $FAIL->(
+   'Can\'t create', $stat->distribution, 'since it was never prepared'
+  );
  }
 
  if ($stat->created) {
-  msg $stat->distribution . ' was already created -- skipping';
-  $stat->dist($stat->eb_file);
-  return 1;
+  $self->_skip($stat->distribution, 'was already created');
+  return $OK->();
  }
 
  my $dir = $stat->eb_dir;
  unless (-d $dir) {
-  eval { mkpath $dir };
-  if ($@) {
-   error "mkpath($dir): $@";
-   return 0;
-  }
+  eval { File::Path::mkpath($dir) };
+  return $FAIL->("mkpath($dir): $@") if $@;
  }
 
  my %seen;
@@ -330,10 +329,7 @@ sub create {
  $d   .= $stat->footer;
 
  my $file = $stat->eb_file;
- open my $eb, '>', $file or do {
-  error "open($file): $! -- aborting";
-  return 0;
- };
+ open my $eb, '>', $file or return $FAIL->("open($file): $!");
  print $eb $d;
  close $eb;
 
@@ -342,26 +338,36 @@ sub create {
 
  $self->SUPER::create(@_);
 
- $stat->created(0);
- $stat->dist(undef);
+ if ($stat->do_manifest and not $self->update_manifest) {
+  1 while unlink $file;
+  return $FAIL->();
+ }
 
- if ($stat->do_manifest) {
-  unless (copy $stat->fetched_arch, $stat->distdir) {
-   error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
-   1 while unlink $file;
-   return 0;
-  }
+ return $OK->();
+}
 
-  msg 'Adding Manifest entry for ' . $stat->distribution;
-  unless ($self->_run([ 'ebuild', $file, 'manifest' ], 0)) {
-   1 while unlink $file;
-   return 0;
-  }
+=head2 C<update_manifest>
+
+Update the F<Manifest> file for the ebuild associated to the current dist object.
+
+=cut
+
+sub update_manifest {
+ my $self = shift;
+ my $stat = $self->status;
+
+ my $file = $stat->eb_file;
+ unless ($file and -e $file) {
+  return $self->_abort('The ebuild file is invalid or does not exist');
  }
 
- $stat->created(1);
- $stat->dist($file);
- return 1;
+ unless (File::Copy::copy($stat->fetched_arch => $stat->distdir)) {
+  return $self->_abort("Couldn\'t copy the distribution file to distdir ($!)");
+ }
+
+ $self->_notify('Adding Manifest entry for', $stat->distribution);
+
+ return $self->_run([ 'ebuild', $stat->eb_file, 'manifest' ], 0);
 }
 
 sub _cpan2portage {
@@ -377,8 +383,12 @@ sub _cpan2portage {
   my $atom = ($category eq 'virtual' ? 'perl-' : '') . $name;
 
   for my $portdir (@portdirs) {
-   my @ebuilds = glob catfile($portdir, $category, $atom,"$atom-*.ebuild");
-   next unless @ebuilds;
+   my @ebuilds = glob File::Spec->catfile(
+    $portdir,
+    $category,
+    $atom,
+    "$atom-*.ebuild",
+   ) or next;
 
    if (defined $ver) { # implies that $version is defined
     for (@ebuilds) {
@@ -395,7 +405,10 @@ sub _cpan2portage {
 
  }
 
- error "Couldn't find an appropriate ebuild for $name in the portage tree -- skipping";
+ $self->_skip(
+  "Couldn't find an appropriate ebuild for $name in the portage tree"
+ );
+
  return '';
 }
 
@@ -440,24 +453,42 @@ sub _run {
  };
 
  unless ($success) {
-  error "$errmsg -- aborting";
+  $self->_abort($errmsg);
   if (not $verbose and defined $output and $stat->verbose) {
    my $msg = join '', @$output;
    1 while chomp $msg;
-   error $msg;
+   CPANPLUS::Error::error($msg);
   }
  }
 
  return $success;
 }
 
+sub _abort {
+ my $self = shift;
+
+ CPANPLUS::Error::error("@_ -- aborting");
+
+ return 0;
+}
+
+sub _notify {
+ my $self = shift;
+
+ CPANPLUS::Error::msg("@_");
+
+ return 1;
+}
+
+sub _skip { shift->_notify(@_, '-- skipping') }
+
 =head1 DEPENDENCIES
 
 Gentoo (L<http://gentoo.org>).
 
 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
 
-L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
+L<Cwd>, L<Carp> (since perl 5), L<File::Path> (5.001), L<File::Copy> (5.002), L<File::Spec> (5.00405).
 
 =head1 SEE ALSO