| 1 | package CPAN::Index::API; | ||
| 2 | |||
| 3 | our $VERSION = '0.008'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Path::Class qw(dir); | ||
| 9 | use Carp qw(croak); | ||
| 10 | use Class::Load qw(load_class); | ||
| 11 | use namespace::autoclean; | ||
| 12 | use Moose; | ||
| 13 | use Moose::Util::TypeConstraints qw(find_type_constraint); | ||
| 14 | |||
| 15 | has files => ( | ||
| 16 | is => 'bare', | ||
| 17 | isa => 'HashRef', | ||
| 18 | traits => ['Hash'], | ||
| 19 | handles => { files => 'values', file => 'get', file_names => 'keys' }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has repo_path => | ||
| 23 | ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Str', | ||
| 26 | ); | ||
| 27 | |||
| 28 | has repo_uri => | ||
| 29 | ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Str', | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub BUILDARGS { | ||
| 35 | my ( $class, %args ) = @_; | ||
| 36 | |||
| 37 | croak "Please specifiy which files to load" unless $args{files}; | ||
| 38 | |||
| 39 | my $constraint = find_type_constraint('ArrayRef[Str]'); | ||
| 40 | |||
| 41 | if ( $constraint->check($args{files}) ) | ||
| 42 | { | ||
| 43 | my %files; | ||
| 44 | |||
| 45 | foreach my $file ( @{ $args{files} } ) | ||
| 46 | { | ||
| 47 | my $package_name = "CPAN::Index::API::File::$file"; | ||
| 48 | load_class $package_name; | ||
| 49 | $files{$file} = $package_name->new( | ||
| 50 | repo_path => $args{repo_path}, | ||
| 51 | repo_uri => $args{repo_uri}, | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | $args{files} = \%files; | ||
| 56 | } | ||
| 57 | |||
| 58 | if ( $args{repo_path} and not $args{repo_uri} ) | ||
| 59 | { | ||
| 60 | $args{repo_uri} = URI::file->new( | ||
| 61 | dir($args{repo_path})->absolute | ||
| 62 | )->as_string; | ||
| 63 | } | ||
| 64 | |||
| 65 | return \%args; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub new_from_repo_path | ||
| 69 | { | ||
| 70 | my ($class, %args) = @_; | ||
| 71 | |||
| 72 | if ( $args{repo_path} and not $args{repo_uri} ) | ||
| 73 | { | ||
| 74 | $args{repo_uri} = URI::file->new( | ||
| 75 | dir($args{repo_path})->absolute | ||
| 76 | )->as_string; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $files = delete $args{files}; | ||
| 80 | my %files; | ||
| 81 | |||
| 82 | croak "Please specifiy which files to load" unless $files; | ||
| 83 | |||
| 84 | foreach my $file ( @$files ) | ||
| 85 | { | ||
| 86 | my $package_name = "CPAN::Index::API::File::$file"; | ||
| 87 | load_class $package_name; | ||
| 88 | |||
| 89 | $files{$file} = $package_name->read_from_repo_path( | ||
| 90 | $args{repo_path} | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | return $class->new( %args, files => \%files ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub new_from_repo_uri | ||
| 98 | { | ||
| 99 | my ($class, %args) = @_; | ||
| 100 | |||
| 101 | my $files = delete $args{files}; | ||
| 102 | my %files; | ||
| 103 | |||
| 104 | croak "Please specifiy which files to load" unless $files; | ||
| 105 | |||
| 106 | foreach my $file ( @$files ) | ||
| 107 | { | ||
| 108 | my $package_name = "CPAN::Index::API::File::$file"; | ||
| 109 | load_class $package_name; | ||
| 110 | |||
| 111 | $files{$file} = $package_name->read_from_repo_uri( | ||
| 112 | $args{repo_uri} | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | return $class->new( %args, files => \%files ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub write_all_files | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | $_->write_to_default_location for $self->files; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub clone { | ||
| 126 | my ($self, %args) = @_; | ||
| 127 | |||
| 128 | my %new_files; | ||
| 129 | foreach my $file_name ( $self->file_names ) | ||
| 130 | { | ||
| 131 | my $new_file = $self->file($file_name)->clone(%args); | ||
| 132 | $new_files{$file_name} = $new_file; | ||
| 133 | } | ||
| 134 | |||
| 135 | return (blessed $self)->meta->clone_object( | ||
| 136 | $self, files => \%new_files, %args | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | __PACKAGE__->meta->make_immutable; | ||
| 141 | |||
| 142 | =pod | ||
| 143 | |||
| 144 | =encoding UTF-8 | ||
| 145 | |||
| 146 | =head1 NAME | ||
| 147 | |||
| 148 | CPAN::Index::API - Read and write CPAN index files | ||
| 149 | |||
| 150 | =head1 SYNOPSIS | ||
| 151 | |||
| 152 | my $index = CPAN::Index::API->new_from_repo_uri( | ||
| 153 | repo_uri => 'http://cpan.perl.org/', | ||
| 154 | files => [qw(PackagesDetails ModList MailRc)], | ||
| 155 | ); | ||
| 156 | |||
| 157 | my $packages = $index->file('PackagesDetails'); | ||
| 158 | |||
| 159 | =head1 DESCRIPTION | ||
| 160 | |||
| 161 | C<CPAN::Index::API> is a library to read and write CPAN index files. See the | ||
| 162 | modules in the C<CPAN::Index::API::File> namespace for documentation on the | ||
| 163 | individual files supported. | ||
| 164 | |||
| 165 | This class provides a convenient interface for working with multiple files | ||
| 166 | from the same location at the same time. | ||
| 167 | |||
| 168 | =head1 CONSTRUCTION | ||
| 169 | |||
| 170 | =head2 new | ||
| 171 | |||
| 172 | Creates a new index object. Accepts the following parameters: | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item files | ||
| 177 | |||
| 178 | Required. Hashrefs whose values are C<CPAN::Index::API::File> objects. The | ||
| 179 | individual objects can later be accessed by their respective hash key via the | ||
| 180 | L</file> method. | ||
| 181 | |||
| 182 | =item repo_path | ||
| 183 | |||
| 184 | Optional. Path to the root of the repository to which the index files belong. | ||
| 185 | |||
| 186 | =item repo_uri | ||
| 187 | |||
| 188 | Optional. Base uri of the repository to which the index files belong. | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =head2 new_from_repo_path | ||
| 193 | |||
| 194 | Creates a new index object by reading one or more index files from a local | ||
| 195 | repository. Accepts the following parameters: | ||
| 196 | |||
| 197 | =over | ||
| 198 | |||
| 199 | =item files | ||
| 200 | |||
| 201 | Required. Arrayref of names of index files to be read. Each name must be the | ||
| 202 | name of a plugin under the C<CPAN::Index::API::File::> namespace, e.g. | ||
| 203 | C<PackagesDetails>, C<ModList>, etc. | ||
| 204 | |||
| 205 | =item repo_path | ||
| 206 | |||
| 207 | Required. Path to the root of the local repository. | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =head2 new_from_repo_uri | ||
| 212 | |||
| 213 | Creates a new index object by reading one or more index files from a remote | ||
| 214 | repository. Accepts the following parameters: | ||
| 215 | |||
| 216 | =over | ||
| 217 | |||
| 218 | =item files | ||
| 219 | |||
| 220 | Required. Arrayref of names of index files to be read. Each name must be the | ||
| 221 | name of a plugin under the C<CPAN::Index::API::File::> namespace, e.g. | ||
| 222 | C<PackagesDetails>, C<ModList>, etc. | ||
| 223 | |||
| 224 | =item repo_uri | ||
| 225 | |||
| 226 | Required. Path to the base uri of the remote repository. | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | =head1 METHODS | ||
| 231 | |||
| 232 | =head2 file | ||
| 233 | |||
| 234 | Given the name of a file plugin loaded within the index, returns the object | ||
| 235 | corresponding to this index file. | ||
| 236 | |||
| 237 | =head2 repo_path | ||
| 238 | |||
| 239 | Returns the path to the repository. | ||
| 240 | |||
| 241 | =head2 repo_uri | ||
| 242 | |||
| 243 | Returns the base uri of the repository. | ||
| 244 | |||
| 245 | =head2 write_all_files | ||
| 246 | |||
| 247 | Writes all index files to their default locations under C<repo_path>. | ||
| 248 | |||
| 249 | =head2 clone | ||
| 250 | |||
| 251 | Creates a new instance of this object, overloading any of the existing | ||
| 252 | attributes with any arguments passed. | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Peter Shangov <F<pshangov@yahoo.com>> | ||
| 257 | |||
| 258 | =head1 COPYRIGHT AND LICENSE | ||
| 259 | |||
| 260 | This software is copyright (c) 2019 by Venda, Inc. | ||
| 261 | |||
| 262 | This is free software; you can redistribute it and/or modify it under | ||
| 263 | the same terms as the Perl 5 programming language system itself. | ||
| 264 | |||
| 265 | =cut |