| 1 | # vim: set ts=2 sts=2 sw=2 expandtab smarttab: | ||||||||||
| 2 | # | ||||||||||
| 3 | # This file is part of Dist-Metadata | ||||||||||
| 4 | # | ||||||||||
| 5 | # This software is copyright (c) 2011 by Randy Stauner. | ||||||||||
| 6 | # | ||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 9 | # | ||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | package Dist::Metadata::Dir; | ||||||||||
| 14 | 3 | our $AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||
| 15 | # ABSTRACT: Enable Dist::Metadata for a directory | ||||||||||
| 16 | 3 | $Dist::Metadata::Dir::VERSION = '0.927'; | |||||||||
| 17 | 3 | ▶ | use Carp qw(croak carp); # core | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 3 | ▶ | use File::Find (); # core | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 3 | ▶ | use Path::Class 0.24 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 3 | ▶ | use parent 'Dist::Metadata::Dist'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 3 | push(@Dist::Metadata::CARP_NOT, __PACKAGE__); | |||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 13 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | 13 | my $class = shift; | |||||||||
| 27 | 13 | my $self = $class->SUPER::new(@_); | |||||||||
| 28 | |||||||||||
| 29 | # fix up dir (for example chop trailing slash if present) | ||||||||||
| 30 | 12 | $self->{dir} = $self->path_class_dir->new($self->{dir})->stringify; | |||||||||
| 31 | |||||||||||
| 32 | # TODO: croak if not -d $self->dir | ||||||||||
| 33 | |||||||||||
| 34 | 12 | return $self; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 14 | ▶ | sub required_attribute { 'dir' } | ||||||||
|
Subroutine
required_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | 10 | ▶ | sub determine_name_and_version { | ||||||||
|
Subroutine
determine_name_and_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 10 | my ($self) = @_; | |||||||||
| 42 | # 'root' may be more accurate than 'dir' | ||||||||||
| 43 | 10 | $self->SUPER::determine_name_and_version(); | |||||||||
| 44 | 10 | $self->set_name_and_version( $self->parse_name_and_version( $self->dir ) ); | |||||||||
| 45 | 10 | return; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 12 | ▶ | sub dir { | ||||||||
|
Subroutine
dir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 12 | $_[0]->{dir}; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | # this shouldn't be called | ||||||||||
| 54 | 1 | ▶ | sub extract_into { | ||||||||
|
Subroutine
extract_into: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | 1 | croak q[A directory doesn't need to be extracted]; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | 6 | ▶ | sub file_content { | ||||||||
|
Subroutine
file_content: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 6 | my ($self, $file) = @_; | |||||||||
| 61 | # This is a directory so file spec will always be native | ||||||||||
| 62 | my $path = $self->path_class_file | ||||||||||
| 63 | 6 | ->new( $self->{dir}, $self->full_path($file) )->stringify; | |||||||||
| 64 | |||||||||||
| 65 | 6 | ▶ | open(my $fh, '<', $path) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | or croak "Failed to open file '$path': $!"; | ||||||||||
| 67 | |||||||||||
| 68 | 4 | return do { local $/; <$fh> }; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 13 | ▶ | sub find_files { | ||||||||
|
Subroutine
find_files: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 13 | my ($self) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 13 | my $dir = $self->{dir}; | |||||||||
| 76 | 13 | my @files; | |||||||||
| 77 | |||||||||||
| 78 | File::Find::find( | ||||||||||
| 79 | { | ||||||||||
| 80 | 125 | ▶ | wanted => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 81 | 125 | ▶ | push @files, $self->path_class_file->new($_)->relative($dir)->stringify | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | if -f $_; | ||||||||||
| 83 | }, | ||||||||||
| 84 | 13 | no_chdir => 1 | |||||||||
| 85 | }, | ||||||||||
| 86 | $dir | ||||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | 13 | return @files; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | 11 | ▶ | sub physical_directory { | ||||||||
|
Subroutine
physical_directory: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 94 | 11 | my ($self, @files) = @_; | |||||||||
| 95 | |||||||||||
| 96 | # TODO: return absolute_path? | ||||||||||
| 97 | 11 | my @parts = $self->{dir}; | |||||||||
| 98 | # go into root dir if there is one | ||||||||||
| 99 | 11 | ▶ | push @parts, $self->root | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | if $self->root; | ||||||||||
| 101 | |||||||||||
| 102 | 11 | my $dir = $self->path_class_dir->new(@parts)->absolute; | |||||||||
| 103 | |||||||||||
| 104 | 11 | ▶ | return $dir->stringify unless wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 21 | return map { $_->stringify } | |||||||||
| 107 | 9 | ($dir, map { $dir->file( $_ ) } @files); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 3 | 1; | |||||||||
| 111 | |||||||||||
| 112 | __END__ | ||||||||||