| 1 | package OrePAN2::Index; | ||||||||||||
| 2 | |||||||||||||
| 3 | 14 | ▶ | use autodie; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 14 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 14 | ▶ | use IO::Uncompress::Gunzip qw( $GunzipError ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 14 | ▶ | use OrePAN2 (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 14 | ▶ | use version; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 14 | ▶ | use OrePAN2::Logger; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 14 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 14 | with 'OrePAN2::Role::HasLogger'; | |||||||||||
| 13 | 14 | ▶ | use Types::Standard qw( HashRef ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 14 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 25 | ▶ | has index => ( is => 'ro', isa => HashRef, default => sub { +{} } ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | sub load { | ||||||||||||
| 19 | 9 | ▶ | my ( $self, $fname ) = @_; | ||||||||||
|
Subroutine
load: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 20 | |||||||||||||
| 21 | 9 | my $fh = do { | |||||||||||
| 22 | 9 | ▶ | if ( $fname =~ /\.gz\z/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 3 | ▶ | IO::Uncompress::Gunzip->new($fname) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | or die "gzip failed: $GunzipError\n"; | ||||||||||||
| 25 | } | ||||||||||||
| 26 | else { | ||||||||||||
| 27 | 6 | open my $fh, '<', $fname; | |||||||||||
| 28 | 6 | $fh; | |||||||||||
| 29 | } | ||||||||||||
| 30 | }; | ||||||||||||
| 31 | |||||||||||||
| 32 | # skip headers | ||||||||||||
| 33 | 9 | ▶ | while (<$fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 60 | ▶ | last unless /\S/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 9 | ▶ | while (<$fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 24 | ▶ | if (/^(\S+)\s+(\S+)\s+(.*)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 24 | ▶ | $self->add_index( $1, $2 eq 'undef' ? undef : $2, $3 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | } | ||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 9 | close $fh; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | sub lookup { | ||||||||||||
| 47 | 9 | ▶ | my ( $self, $package ) = @_; | ||||||||||
|
Subroutine
lookup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 48 | 9 | ▶ | if ( my $entry = $self->index->{$package} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 8 | return @$entry; | |||||||||||
| 50 | } | ||||||||||||
| 51 | 1 | return; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | sub packages { | ||||||||||||
| 55 | 19 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
packages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 56 | 19 | sort { lc $a cmp lc $b } keys %{ $self->index }; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | sub delete_index { | ||||||||||||
| 60 | 1 | ▶ | my ( $self, $package ) = @_; | ||||||||||
|
Subroutine
delete_index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 61 | 1 | delete $self->index->{$package}; | |||||||||||
| 62 | 1 | return; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | # Order of preference is last updated. So if some modules maintain the same | ||||||||||||
| 66 | # version number across multiple uploads, we'll point to the module in the | ||||||||||||
| 67 | # latest archive. | ||||||||||||
| 68 | |||||||||||||
| 69 | sub add_index { | ||||||||||||
| 70 | 84 | ▶ | my ( $self, $package, $version, $archive_file ) = @_; | ||||||||||
|
Subroutine
add_index: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 71 | |||||||||||||
| 72 | 84 | ▶ | if ( $self->index->{$package} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 13 | my ($orig_ver) = @{ $self->index->{$package} }; | |||||||||||
| 74 | |||||||||||||
| 75 | 13 | ▶ | if ( version->parse($orig_ver) > version->parse($version) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 2 | ▶ | $version //= 'undef'; | ||||||||||
|
Condition50%1/2
$version //= "undef"
MC/DC0%0/1
$version //= "undef"
$version
Truth table50%1/2
A
A= $version
| |||||||||||||
| 77 | 2 | $self->log->info("Not adding $package in $archive_file"); | |||||||||||
| 78 | 2 | $self->log->info( | |||||||||||
| 79 | "Existing version $orig_ver is greater than $version"); | ||||||||||||
| 80 | 2 | return; | |||||||||||
| 81 | } | ||||||||||||
| 82 | } | ||||||||||||
| 83 | 82 | $self->index->{$package} = [ $version, $archive_file ]; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | sub as_string { | ||||||||||||
| 87 | 16 | ▶ | my ( $self, $opts ) = @_; | ||||||||||
|
Subroutine
as_string: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 88 | 16 | ▶ | $opts ||= +{}; | ||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||
| 89 | 16 | ▶ | my $simple = $opts->{simple} || 0; | ||||||||||
|
Condition100%2/2
$opts->{'simple'} || 0
MC/DC100%1/1
$opts->{'simple'} || 0
$opts->{'simple'}
Truth table100%2/2
A
A= $opts->{'simple'}
| |||||||||||||
| 90 | |||||||||||||
| 91 | 16 | my @buf; | |||||||||||
| 92 | |||||||||||||
| 93 | 16 | ▶ | push @buf, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | ( | ||||||||||||
| 95 | 'File: 02packages.details.txt', | ||||||||||||
| 96 | 'URL: http://www.perl.com/CPAN/modules/02packages.details.txt', | ||||||||||||
| 97 | 'Description: DarkPAN', | ||||||||||||
| 98 | 'Columns: package name, version, path', | ||||||||||||
| 99 | 'Intended-For: Automated fetch routines, namespace documentation.', | ||||||||||||
| 100 | $simple | ||||||||||||
| 101 | ? () | ||||||||||||
| 102 | : ( | ||||||||||||
| 103 | "Written-By: OrePAN2 $OrePAN2::VERSION", | ||||||||||||
| 104 | 12 | "Line-Count: @{[ scalar(keys %{$self->index}) ]}", | |||||||||||
| 105 | 12 | "Last-Updated: @{[ scalar localtime ]}", | |||||||||||
| 106 | ), | ||||||||||||
| 107 | q{}, | ||||||||||||
| 108 | ); | ||||||||||||
| 109 | |||||||||||||
| 110 | 16 | for my $pkg ( $self->packages ) { | |||||||||||
| 111 | 51 | my $entry = $self->index->{$pkg}; | |||||||||||
| 112 | |||||||||||||
| 113 | # package name, version, path | ||||||||||||
| 114 | 51 | ▶ | push @buf, sprintf '%-22s %-22s %s', $pkg, $entry->[0] || 'undef', | ||||||||||
|
Condition100%2/2
$entry->[0] || 'undef'
MC/DC100%1/1
$entry->[0] || 'undef'
$entry->[0]
Truth table100%2/2
A
A= $entry->[0]
| |||||||||||||
| 115 | $entry->[1]; | ||||||||||||
| 116 | } | ||||||||||||
| 117 | 16 | return join( "\n", @buf ) . "\n"; | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | 14 | 1; | |||||||||||
| 121 | __END__ | ||||||||||||
| 122 | |||||||||||||
| 123 | =head1 NAME | ||||||||||||
| 124 | |||||||||||||
| 125 | OrePAN2::Index - Index | ||||||||||||
| 126 | |||||||||||||
| 127 | =head1 DESCRIPTION | ||||||||||||
| 128 | |||||||||||||
| 129 | This is a module to manipulate 02packages.details.txt. | ||||||||||||
| 130 | |||||||||||||
| 131 | =head1 METHODS | ||||||||||||
| 132 | |||||||||||||
| 133 | =over 4 | ||||||||||||
| 134 | |||||||||||||
| 135 | =item C<< my $index = OrePAN2::Index->new(%attr) >> | ||||||||||||
| 136 | |||||||||||||
| 137 | =item C<< $index->load($filename) >> | ||||||||||||
| 138 | |||||||||||||
| 139 | Load an existing 02.packages.details.txt | ||||||||||||
| 140 | |||||||||||||
| 141 | =item C<< my ($version, $path) = $index->lookup($package) >> | ||||||||||||
| 142 | |||||||||||||
| 143 | Perform a package lookup on the index. | ||||||||||||
| 144 | |||||||||||||
| 145 | =item C<< $index->delete_index($package) >> | ||||||||||||
| 146 | |||||||||||||
| 147 | Delete a package from the index. | ||||||||||||
| 148 | |||||||||||||
| 149 | =item C<< $index->add_index($package, $version, $path) >> | ||||||||||||
| 150 | |||||||||||||
| 151 | Add a new entry to the index. | ||||||||||||
| 152 | |||||||||||||
| 153 | =item C<< $index->as_string() >> | ||||||||||||
| 154 | |||||||||||||
| 155 | Returns the content of the index as a string. Some of the index metadata can | ||||||||||||
| 156 | cause merge conflicts when multiple developers are working on the same project. | ||||||||||||
| 157 | You can avoid this problem by using a paring down the metadata. "simple" | ||||||||||||
| 158 | defaults to 0. | ||||||||||||
| 159 | |||||||||||||
| 160 | $index->as_string( simple => 1 ); | ||||||||||||
| 161 | |||||||||||||
| 162 | Make index as string. | ||||||||||||
| 163 | |||||||||||||
| 164 | =back | ||||||||||||