Skip to content

Commit

Permalink
refactor and improve file sorting on release page
Browse files Browse the repository at this point in the history
Move all file sorting into one routine.  Only link one one of a matching
pod/pm pair.  Allow for other interesting files that aren't in the root
directory.
  • Loading branch information
haarg committed Jul 6, 2017
1 parent 8ccb288 commit a86ff1f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 67 deletions.
116 changes: 53 additions & 63 deletions lib/MetaCPAN/Web/Controller/Release.pm
Expand Up @@ -83,22 +83,6 @@ sub view : Private {
$self->stash_api_results( $c, $reqs, $out );
$self->add_favorites_data( $out, $reqs->{favorites}, $out );

# shortcuts
my ( $files, $modules ) = @{$reqs}{qw(files modules)};

my @root_files = (
sort { $a->{name} cmp $b->{name} }
grep { $_->{path} !~ m{/} } @{ $files->{files} }
);

my @examples = (
sort { $a->{path} cmp $b->{path} }
grep {
$_->{path} =~ m{\b(?:eg|ex|examples?|samples?)\b}i
and not $_->{path} =~ m{^x?t/}
} @{ $files->{files} }
);

$c->res->last_modified( $out->{date} );
$c->cdn_max_age('1y');
$c->add_dist_key($distribution);
Expand All @@ -118,10 +102,8 @@ sub view : Private {
$c->stash(
$c->model('API::Favorite')->find_plussers($distribution)->get );

# Simplify the file data we pass to the template.
my $view_files = $modules->{files};

my $categories = $self->_files_to_categories( $out, $view_files );
my $categories = $self->_files_to_categories( map @{ $_->{files} },
@{$reqs}{qw(files modules)} );

my $changes
= $c->model('API::Changes')->last_version( $reqs->{changes}, $out );
Expand All @@ -130,18 +112,8 @@ sub view : Private {
$c->stash(
template => 'release.html',
release => $out,
total => $modules->{total},
took => List::Util::max(
$modules->{took}, $files->{took}, $reqs->{versions}{took}
),
root => \@root_files,
examples => \@examples,
files => $view_files,

documentation => $categories->{documentation},
documentation_raw => $categories->{documentation_raw},
provides => $categories->{provides},
modules => $categories->{modules},
%$categories,

# TODO: Put this in a more general place.
# Maybe make a hash for feature flags?
Expand All @@ -162,54 +134,53 @@ sub view : Private {
}

sub _files_to_categories {
my ( $self, $release, $files ) = @_;
my $self = shift;
my %files = map +( $_->{path} => $_ ), @_;
my @files = sort keys %files;

my $ret = +{
provides => [],
documentation => [],
documentation_raw => [],
modules => [],
provides => [],
documentation => [],
modules => [],
xs => [],
other => [],
examples => [],
};

my %skip;

for my $f (@$files) {
next if $f->{documentation};
my @modules
= is_arrayref( $f->{module} ) ? @{ $f->{module} } : $f->{module};
for my $module ( grep {defined} @modules ) {
for my $f ( @files{@files} ) {
my $modules = $f->{module}
= is_arrayref( $f->{module} )
? $f->{module}
: [ $f->{module} || () ];
for my $module (@$modules) {
my $assoc = $module->{associated_pod} or next;
$assoc =~ s{^\Q$f->{author}/$f->{release}/}{};
if ( $assoc ne $f->{path}
&& $assoc eq $f->{path} =~ s{\.pm$}{\.pod}r )
{
my ($assoc_file) = grep $_->{path} eq $assoc, @$files;
my $assoc_file = $files{$assoc}
or next;
$f->{$_} ||= $assoc_file->{$_} for qw(
abstract
documentation
);
$skip{$assoc}++;
$assoc_file->{skip}++;
}
}
}

for my $f ( @{$files} ) {
for my $f ( @files{@files} ) {
next
if $skip{ $f->{path} };
my %info = (
status => $f->{status},
path => $f->{path},
release => $f->{release},
author => $f->{author},
);

my @modules
= is_arrayref( $f->{module} ) ? @{ $f->{module} } : $f->{module};
if $f->{skip};
my $path = $f->{path};
my @modules = @{ $f->{module} };

if ( $f->{documentation} and @modules ) {
push @{ $ret->{modules} }, $f;
push @{ $ret->{provides} },
my %s;
push @{ $ret->{modules} }, $f;
push @{ $ret->{provides} }, grep !$s{ $_->{package} }++,
map +{
%info,
%$f,
package => $_->{name},
authorized => $_->{authorized}
},
Expand All @@ -221,21 +192,40 @@ sub _files_to_categories {
} @modules;
}
elsif (@modules) {
push @{ $ret->{provides} },
my %s;
push @{ $ret->{provides} }, grep !$s{ $_->{package} }++,
map +{
%info,
%$f,
package => $_->{name},
authorized => $_->{authorized}
}, @modules;
}
elsif ( $f->{documentation} ) {
elsif ( $f->{documentation} && $path =~ m/\.pm$/ ) {
push @{ $ret->{modules} }, $f;
}
elsif ( $path =~ /\.xs$/ ) {
push @{ $ret->{xs} }, $f;
}
elsif ( $path =~ m{^(?:eg|ex|examples?|samples?)\b}i ) {
push @{ $ret->{examples} }, $f;
}
elsif ( $f->{documentation} || $path =~ m/\.pod$/ ) {
push @{ $ret->{documentation} }, $f;
}
elsif ( $path =~ m{\b(?:eg|ex|examples?|samples?)\b}i ) {
push @{ $ret->{examples} }, $f;
}
else {
push @{ $ret->{documentation_raw} }, $f;
push @{ $ret->{other} }, $f;
}
}

$ret->{provides}
= [
sort { $a->{package} cmp $b->{package} || $a->{path} cmp $b->{path} }
@{ $ret->{provides} }
];

return $ret;
}

Expand Down
8 changes: 4 additions & 4 deletions root/release.html
Expand Up @@ -94,7 +94,7 @@ <h2 id="modules">Modules</h2>
<div class="file-group release-provides">
<h2 id="provides">Provides</h2>
<ul>
<%- FOREACH module IN provides.sort('package') %>
<%- FOREACH module IN provides %>
<li>
<% link_to_source(module) | none %> in <% module.path %>
<% IF module.indexed AND NOT module.authorized %><em class="unauthorized">UNAUTHORIZED</em><% END %>
Expand All @@ -108,7 +108,7 @@ <h2 id="provides">Provides</h2>
<div class="file-group release-examples">
<h2 id="examples">Examples</h2>
<ul>
<%- FOREACH file IN examples.sort('path') %>
<%- FOREACH file IN examples %>
<li>
<%- IF file.path.match('\.pod$') %>
<% link_to_file(file, file.path) | none %>
Expand All @@ -124,11 +124,11 @@ <h2 id="examples">Examples</h2>
</div>
<%- END %>

<%- IF root.size %>
<%- IF other.size %>
<div class="file-group release-other-files">
<h2 id="other">Other files</h2>
<ul>
<%- FOREACH file IN root %>
<%- FOREACH file IN other %>
<li><% link_to_file(file, file.name) | none %></li>
<%- END %>
</ul>
Expand Down

0 comments on commit a86ff1f

Please sign in to comment.