| 1 | use 5.006; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Gentoo::Overlay::Package; | ||
| 6 | |||
| 7 | our $VERSION = '2.001000'; | ||
| 8 | |||
| 9 | # ABSTRACT: Class for Package's in Gentoo Overlays | ||
| 10 | |||
| 11 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 12 | |||
| 13 | use Moo qw( has ); | ||
| 14 | use MooX::HandlesVia; | ||
| 15 | use MooseX::Has::Sugar qw( ro required lazy lazy_build); | ||
| 16 | use Types::Standard qw( HashRef Str ); | ||
| 17 | use Types::Path::Tiny qw( Path ); | ||
| 18 | use MooX::ClassAttribute qw( class_has ); | ||
| 19 | use Gentoo::Overlay::Types qw( Gentoo__Overlay_PackageName Gentoo__Overlay_Category ); | ||
| 20 | use Gentoo::Overlay::Types qw( Gentoo__Overlay_RepositoryName Gentoo__Overlay_Category Gentoo__Overlay_Ebuild ); | ||
| 21 | use Gentoo::Overlay::Exceptions qw( exception); | ||
| 22 | use namespace::clean -except => 'meta'; | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | has name => ( isa => Gentoo__Overlay_PackageName, required, ro, ); | ||
| 69 | has category => ( isa => Gentoo__Overlay_Category, required, ro, handles => [qw( overlay )], ); | ||
| 70 | has path => ( | ||
| 71 | isa => Path, | ||
| 72 | ro, | ||
| 73 | lazy, | ||
| 74 | default => sub { | ||
| 75 | my ($self) = shift; | ||
| 76 | return $self->overlay->default_path( 'package', $self->category->name, $self->name ); | ||
| 77 | }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | class_has _scan_blacklist => ( | ||
| 108 | isa => HashRef [Str], | ||
| 109 | ro, | ||
| 110 | lazy, | ||
| 111 | default => sub { | ||
| 112 | return { map { $_ => 1 } qw( . .. metadata.xml ) }; | ||
| 113 | }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | sub _scan_blacklisted { | ||
| 117 | my ( $self, $what ) = @_; | ||
| 118 | return exists $self->_scan_blacklist->{$what}; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | has _ebuilds => ( | ||
| 173 | isa => HashRef [Gentoo__Overlay_Ebuild], | ||
| 174 | lazy, | ||
| 175 | builder => 1, | ||
| 176 | ro, | ||
| 177 | handles_via => 'Hash', | ||
| 178 | handles => { | ||
| 179 | _has_ebuild => exists =>, | ||
| 180 | ebuild_names => keys =>, | ||
| 181 | ebuilds => elements =>, | ||
| 182 | get_ebuild => get =>, | ||
| 183 | }, | ||
| 184 | ); | ||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | sub _build__ebuilds { | ||
| 195 | my ($self) = shift; | ||
| 196 | require Gentoo::Overlay::Ebuild; | ||
| 197 | my $it = $self->path->iterator(); | ||
| 198 | my %out; | ||
| 199 | while ( defined( my $entry = $it->() ) ) { | ||
| 200 | my $ebuild = $entry->basename; | ||
| 201 | next if Gentoo::Overlay::Ebuild->is_blacklisted($ebuild); | ||
| 202 | next if -d $entry; | ||
| 203 | ## no critic ( RegularExpressions ) | ||
| 204 | next if $entry !~ /\.ebuild$/; | ||
| 205 | my $e = Gentoo::Overlay::Ebuild->new( | ||
| 206 | name => $ebuild, | ||
| 207 | package => $self, | ||
| 208 | ); | ||
| 209 | next unless $e->exists; | ||
| 210 | $out{$ebuild} = $e; | ||
| 211 | } | ||
| 212 | return \%out; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | ## no critic ( ProhibitBuiltinHomonyms ) | ||
| 225 | sub exists { | ||
| 226 | my $self = shift; | ||
| 227 | return if q{.} eq $self->name; | ||
| 228 | return if q{..} eq $self->name; | ||
| 229 | return if not -e $self->path; | ||
| 230 | return if not -d $self->path; | ||
| 231 | return 1; | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | sub is_blacklisted { | ||
| 243 | my ( $self, $name ) = @_; | ||
| 244 | if ( not defined $name ) { | ||
| 245 | $name = $self->name; | ||
| 246 | } | ||
| 247 | return $self->_scan_blacklisted($name); | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | sub pretty_name { | ||
| 259 | my $self = shift; | ||
| 260 | return $self->category->name . q{/} . $self->name . q{::} . $self->overlay->name; | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | sub iterate { | ||
| 290 | my ( $self, $what, $callback ) = @_; ## no critic (Variables::ProhibitUnusedVarsStricter) | ||
| 291 | my %method_map = ( ebuilds => _iterate_ebuilds =>, ); | ||
| 292 | if ( exists $method_map{$what} ) { | ||
| 293 | goto $self->can( $method_map{$what} ); | ||
| 294 | } | ||
| 295 | return exception( | ||
| 296 | ident => 'bad iteration method', | ||
| 297 | message => 'The iteration method %{what_method}s is not a known way to iterate.', | ||
| 298 | payload => { what_method => $what }, | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | # ebuilds = {/ebuilds } | ||
| 313 | sub _iterate_ebuilds { | ||
| 314 | my ( $self, undef, $callback ) = @_; | ||
| 315 | my %ebuilds = $self->ebuilds(); | ||
| 316 | my $num_ebuilds = scalar keys %ebuilds; | ||
| 317 | my $last_ebuild = $num_ebuilds - 1; | ||
| 318 | my $offset = 0; | ||
| 319 | for my $ename ( sort keys %ebuilds ) { | ||
| 320 | local $_ = $ebuilds{$ename}; | ||
| 321 | $self->$callback( | ||
| 322 | { | ||
| 323 | ebuild_name => $ename, | ||
| 324 | ebuild => $ebuilds{$ename}, | ||
| 325 | num_ebuilds => $num_ebuilds, | ||
| 326 | last_ebuild => $last_ebuild, | ||
| 327 | ebuild_num => $offset, | ||
| 328 | } | ||
| 329 | ); | ||
| 330 | $offset++; | ||
| 331 | } | ||
| 332 | return; | ||
| 333 | |||
| 334 | } | ||
| 335 | no Moo; | ||
| 336 | 1; | ||
| 337 | |||
| 338 | __END__ | ||
| 339 | |||
| 340 | =pod | ||
| 341 | |||
| 342 | =encoding UTF-8 | ||
| 343 | |||
| 344 | =head1 NAME | ||
| 345 | |||
| 346 | Gentoo::Overlay::Package - Class for Package's in Gentoo Overlays | ||
| 347 | |||
| 348 | =head1 VERSION | ||
| 349 | |||
| 350 | version 2.001000 | ||
| 351 | |||
| 352 | =head1 SYNOPSIS | ||
| 353 | |||
| 354 | my $package = Overlay::Package->new( | ||
| 355 | name => 'Moose', | ||
| 356 | category => $category_object, | ||
| 357 | ); | ||
| 358 | |||
| 359 | $package->exists() # Moose exists | ||
| 360 | |||
| 361 | print $package->pretty_name() # dev-perl/Moose::gentoo | ||
| 362 | |||
| 363 | print $package->path() # /usr/portage/dev-perl/Moose | ||
| 364 | |||
| 365 | ::Package->is_blacklisted("..") # '..' is not a valid package name | ||
| 366 | ::Package->is_blacklisted('metadata.xml') # is not a valid directory | ||
| 367 | |||
| 368 | =head1 METHODS | ||
| 369 | |||
| 370 | =head2 exists | ||
| 371 | |||
| 372 | Does the Package exist, and is it a directory? | ||
| 373 | |||
| 374 | $package->exists(); | ||
| 375 | |||
| 376 | =head2 is_blacklisted | ||
| 377 | |||
| 378 | Does the package name appear on a blacklist meaning auto-scan should ignore this? | ||
| 379 | |||
| 380 | ::Package->is_blacklisted('..') # true | ||
| 381 | |||
| 382 | =head2 pretty_name | ||
| 383 | |||
| 384 | A pretty form of the name | ||
| 385 | |||
| 386 | $package->pretty_name # dev-perl/Moose::gentoo | ||
| 387 | |||
| 388 | =head2 iterate | ||
| 389 | |||
| 390 | $overlay->iterate( $what, sub { | ||
| 391 | my ( $context_information ) = shift; | ||
| 392 | |||
| 393 | } ); | ||
| 394 | |||
| 395 | The iterate method provides a handy way to do walking across the whole tree stopping at each of a given type. | ||
| 396 | |||
| 397 | =over 4 | ||
| 398 | |||
| 399 | =item * C<$what = 'ebuilds'> | ||
| 400 | |||
| 401 | $overlay->iterate( ebuilds => sub { | ||
| 402 | my ( $self, $c ) = shift; | ||
| 403 | # $c->{ebuild_name} # String | ||
| 404 | # $c->{ebuild} # Ebuild Object | ||
| 405 | # $c->{num_ebuilds} # How many ebuild are there to iterate | ||
| 406 | # $c->{last_ebuild} # Index ID of the last ebuild. | ||
| 407 | # $c->{ebuild_num} # Index ID of the current ebuild. | ||
| 408 | } ); | ||
| 409 | |||
| 410 | =back | ||
| 411 | |||
| 412 | =head1 ATTRIBUTES | ||
| 413 | |||
| 414 | =head2 name | ||
| 415 | |||
| 416 | The packages Short name. | ||
| 417 | |||
| 418 | isa => Gentoo__Overlay_PackageName, required, ro | ||
| 419 | |||
| 420 | L<< C<PackageName>|Gentoo::Overlay::Types/Gentoo__Overlay_PackageName >> | ||
| 421 | |||
| 422 | =head2 category | ||
| 423 | |||
| 424 | The category object that this package is in. | ||
| 425 | |||
| 426 | isa => Gentoo__Overlay_Category, required, ro | ||
| 427 | |||
| 428 | accessors => overlay | ||
| 429 | |||
| 430 | L<< C<Category>|Gentoo::Overlay::Types/Gentoo__Overlay_Category >> | ||
| 431 | |||
| 432 | L</overlay> | ||
| 433 | |||
| 434 | =head2 path | ||
| 435 | |||
| 436 | The full path to the package. | ||
| 437 | |||
| 438 | isa => Dir, lazy, ro | ||
| 439 | |||
| 440 | L<MooseX::Types::Path::Tiny/Dir> | ||
| 441 | |||
| 442 | =head1 ATTRIBUTE ACCESSORS | ||
| 443 | |||
| 444 | =head2 overlay | ||
| 445 | |||
| 446 | $package->overlay -> Gentoo::Overlay::Category->overlay | ||
| 447 | |||
| 448 | L<Gentoo::Overlay::Category/overlay> | ||
| 449 | |||
| 450 | L</category> | ||
| 451 | |||
| 452 | =head2 ebuild_names | ||
| 453 | |||
| 454 | for( $package->ebuild_names ){ | ||
| 455 | print $_; | ||
| 456 | } | ||
| 457 | |||
| 458 | L</_ebuilds> | ||
| 459 | |||
| 460 | =head2 ebuilds | ||
| 461 | |||
| 462 | my %ebuilds = $package->ebuilds; | ||
| 463 | |||
| 464 | L</_ebuilds> | ||
| 465 | |||
| 466 | =head2 get_ebuild | ||
| 467 | |||
| 468 | my $ebuild = $package->get_ebuild('Moose-2.0.0.ebuild'); | ||
| 469 | |||
| 470 | L</_ebuilds> | ||
| 471 | |||
| 472 | =head1 PRIVATE ATTRIBUTES | ||
| 473 | |||
| 474 | =head2 _ebuilds | ||
| 475 | |||
| 476 | isa => HashRef[ Gentoo__Overlay_Ebuild ], lazy_build, ro | ||
| 477 | |||
| 478 | accessors => _has_ebuild , ebuild_names, | ||
| 479 | ebuilds, get_ebuild | ||
| 480 | |||
| 481 | L</_has_ebuild> | ||
| 482 | |||
| 483 | L</ebuild_names> | ||
| 484 | |||
| 485 | L</ebuilds> | ||
| 486 | |||
| 487 | L</get_ebuild> | ||
| 488 | |||
| 489 | =head1 PRIVATE ATTRIBUTE ACCESSORS | ||
| 490 | |||
| 491 | =head2 _has_ebuild | ||
| 492 | |||
| 493 | $package->_has_ebuild('Moose-2.0.0.ebuild'); | ||
| 494 | |||
| 495 | L</_ebuilds> | ||
| 496 | |||
| 497 | =head1 PRIVATE CLASS ATTRIBUTES | ||
| 498 | |||
| 499 | =head2 _scan_blacklist | ||
| 500 | |||
| 501 | Class-Wide list of blacklisted package names. | ||
| 502 | |||
| 503 | isa => HashRef[ Str ], ro, lazy, | ||
| 504 | |||
| 505 | accessors => _scan_blacklisted | ||
| 506 | |||
| 507 | L</_scan_blacklisted> | ||
| 508 | |||
| 509 | L<< C<MooseX::Types::Moose>|MooseX::Types::Moose >> | ||
| 510 | |||
| 511 | =head1 PRIVATE CLASS ATTRIBUTE ACCESSORS | ||
| 512 | |||
| 513 | =head2 _scan_blacklisted | ||
| 514 | |||
| 515 | is C<$arg> blacklisted in the Class Wide Blacklist? | ||
| 516 | |||
| 517 | ::Package->_scan_blacklisted( $arg ) | ||
| 518 | -> | ||
| 519 | exists ::Package->_scan_blacklist->{$arg} | ||
| 520 | |||
| 521 | L</_scan_blacklist> | ||
| 522 | |||
| 523 | =head1 PRIVATE METHODS | ||
| 524 | |||
| 525 | =head2 _build__ebuilds | ||
| 526 | |||
| 527 | Generates the ebuild Hash-Table, by scanning the package directory. | ||
| 528 | |||
| 529 | L</_packages> | ||
| 530 | |||
| 531 | =head2 _iterate_ebuilds | ||
| 532 | |||
| 533 | $object->_iterate_ebuilds( ignored_value => sub { } ); | ||
| 534 | |||
| 535 | Handles dispatch call for | ||
| 536 | |||
| 537 | $object->iterate( ebuilds => sub { } ); | ||
| 538 | |||
| 539 | =head1 AUTHOR | ||
| 540 | |||
| 541 | Kent Fredric <kentnl@cpan.org> | ||
| 542 | |||
| 543 | =head1 COPYRIGHT AND LICENSE | ||
| 544 | |||
| 545 | This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>. | ||
| 546 | |||
| 547 | This is free software; you can redistribute it and/or modify it under | ||
| 548 | the same terms as the Perl 5 programming language system itself. | ||
| 549 | |||
| 550 | =cut |