Dist-Zilla-Plugin-Sorter
view release on metacpan or search on metacpan
lib/Dist/Zilla/Plugin/Sorter.pm view on Meta::CPAN
package Dist::Zilla::Plugin::Sorter;
use 5.010001;
use strict 'subs', 'vars';
use warnings;
use Moose;
use namespace::autoclean;
use File::Spec::Functions qw(catfile);
with (
#'Dist::Zilla::Role::RequireFromBuild', # not ready at before-build phase?
'Dist::Zilla::Role::BeforeBuild',
'Dist::Zilla::Role::FileMunger',
'Dist::Zilla::Role::FileFinderUser' => {
default_finders => [':InstallModules'],
},
);
# AUTHOR
our $DATE = '2024-04-24'; # DATE
our $DIST = 'Dist-Zilla-Plugin-Sorter'; # DIST
our $VERSION = '0.001'; # VERSION
sub _get_meta {
my ($self, $pkg) = @_;
$self->require_from_build($pkg);
$pkg->meta;
}
# 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 ($meta, $abstract);
#$meta = $self->_get_meta($class);
{
local @INC = ("lib", @INC);
(my $mod = $name) =~ s/-/::/g;
(my $mod_pm = "$mod.pm") =~ s!::!/!g;
require $mod_pm;
$meta = $mod->meta;
}
$abstract = $meta->{summary};
unless ($abstract) {
$self->log_debug("meta does not contain summary, skipping setting Abstract from meta's summary");
return;
}
$self->log("Setting Abstract from meta's summary: $abstract");
$self->zilla->abstract($abstract);
return;
}
sub munge_files {
no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
my $self = shift;
local @INC = ("lib", @INC);
# gather dist modules
my %distmodules;
for my $file (@{ $self->found_files }) {
next unless $file->name =~ m!\Alib/(.+)\.pm\z!;
my $mod = $1; $mod =~ s!/!::!g;
$distmodules{$mod}++;
}
for my $file (@{ $self->found_files }) {
next unless $file->name =~ m!\Alib/(Sort/Sub/.+)\.pm\z!;
(my $pkg = $1) =~ s!/!::!g;
my $meta = $self->_get_meta($pkg);
# fill-in ABSTRACT from scenario's summary
{
my $content = $file->content;
my $abstract = $meta->{summary};
last 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);
}
} # foreach file
return;
}
( run in 0.992 second using v1.01-cache-2.11-cpan-71847e10f99 )