Dist-Zilla-Plugin-Rinci-AbstractFromMeta

 view release on metacpan or  search on metacpan

lib/Dist/Zilla/Plugin/Rinci/AbstractFromMeta.pm  view on Meta::CPAN

        # function is embedded in script (/main/FOO), we need to load the
        # metadata in-process
        no warnings;
        %main::SPEC = (); # empty first to avoid mixing with other scripts'
        (undef, undef, undef) = Capture::Tiny::capture(sub {
            eval q{package main; use Perinci::CmdLine::Base::Patch::DumpAndExit -tag=>'$tag', -exit_method=>'die'; do "./$filename"};
        });
    }
    state $pa = do { require Perinci::Access; Perinci::Access->new };
    my $res = $pa->request(meta => $cli->{url});
    die "Can't meta $cli->{url}: $res->[0] - $res->[1]" unless $res->[0] == 200;
    my $meta = $res->[2];

    return $meta->{summary};
}

# either provide filename or filename+filecontent
sub _get_abstract_from_meta {
    my ($self, $filename, $filecontent) = @_;

    local @INC = @INC;
    unshift @INC, 'lib';

    unless (defined $filecontent) {
        $filecontent = do {
            open my($fh), "<", $filename or die "Can't open $filename: $!";
            local $/;
            ~~<$fh>;
        };
    }

    unless ($filecontent =~ m{^#[ \t]*ABSTRACT:[ \t]*([^\n]*)[ \t]*$}m) {
        $self->log_debug(["Skipping %s: no # ABSTRACT", $filename]);
        return undef;
    }

    my $abstract = $1;
    if ($abstract =~ /\S/) {
        $self->log_debug(["Skipping %s: Abstract already filled (%s)", $filename, $abstract]);
        return $abstract;
    }

    # XXX if script, do()
    if ($filename =~ m!^lib/!) {
        $self->log_debug(["Getting abstract for module %s", $filename]);
        $abstract = $self->_get_from_module($filename);
    } else {
        $self->log_debug(["Getting abstract for script %s", $filename]);
        $abstract = $self->_get_from_script($filename);
    }
    $abstract;
}

# dzil also wants to get abstract for main module to put in dist's
# META.{yml,json}
sub before_build {
   my $self  = shift;
   my $name  = $self->zilla->name;
   my $class = $name; $class =~ s{ [\-] }{::}gmx;
   my $filename = $self->zilla->_main_module_override ||
       catfile( 'lib', split m{ [\-] }mx, "${name}.pm" );

   $filename or die 'No main module specified';
   -f $filename or die "Path ${filename} does not exist or not a file";
   open my $fh, '<', $filename or die "File ${filename} cannot open: $!";

   my $abstract = $self->_get_abstract_from_meta($filename);
   return unless $abstract;

   $self->zilla->abstract($abstract);
   return;
}

sub munge_files {
    my $self = shift;
    $self->munge_file($_) for @{ $self->found_files };
}

sub munge_file {
    my ($self, $file) = @_;
    my $content = $file->content;

    unless ($file->isa("Dist::Zilla::File::OnDisk")) {
        $self->log_debug(["skipping %s: not an ondisk file, currently generated file is assumed to be OK", $file->name]);
        return;
    }

    my $abstract = $self->_get_abstract_from_meta($file->name, $file->content);
    return unless $abstract;

    $content =~ s{^#\s*ABSTRACT:.*}{# ABSTRACT: $abstract}m
        or die "Can't insert abstract for " . $file->name;
    $self->log(["inserting abstract for %s (%s)", $file->name, $abstract]);
    $file->content($content);
}

__PACKAGE__->meta->make_immutable;
1;
# ABSTRACT: Fill out abstract from Rinci metadata

__END__

=pod

=encoding UTF-8

=head1 NAME

Dist::Zilla::Plugin::Rinci::AbstractFromMeta - Fill out abstract from Rinci metadata

=head1 VERSION

This document describes version 0.10 of Dist::Zilla::Plugin::Rinci::AbstractFromMeta (from Perl distribution Dist-Zilla-Plugin-Rinci-AbstractFromMeta), released on 2017-07-07.

=head1 SYNOPSIS

In C<dist.ini>:

 [Rinci::AbstractFromMeta]

In your module/script:



( run in 1.230 second using v1.01-cache-2.11-cpan-71847e10f99 )