File Coverage

blib/lib/Dist/Metadata/Dir.pm
Criterion Covered Total %
statement 50 50 100.0
branch 8 8 100.0
condition n/a
subroutine 16 16 100.0
pod 8 8 100.0
total 82 82 100.0


line stmt bran cond sub pod time code
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 2     2   1736 use strict;
  2         5  
  2         83  
11 2     2   12 use warnings;
  2         5  
  2         135  
12              
13             package Dist::Metadata::Dir;
14             {
15             $Dist::Metadata::Dir::VERSION = '0.925';
16             }
17             BEGIN {
18 2     2   41 $Dist::Metadata::Dir::AUTHORITY = 'cpan:RWSTAUNER';
19             }
20             # ABSTRACT: Enable Dist::Metadata for a directory
21              
22 2     2   11 use Carp qw(croak carp); # core
  2         4  
  2         145  
23 2     2   11 use File::Find (); # core
  2         4  
  2         38  
24 2     2   11 use Path::Class 0.24 ();
  2         173  
  2         56  
25 2     2   11 use parent 'Dist::Metadata::Dist';
  2         4  
  2         18  
26              
27             push(@Dist::Metadata::CARP_NOT, __PACKAGE__);
28              
29              
30             sub new {
31 13     13 1 1952 my $class = shift;
32 13         102 my $self = $class->SUPER::new(@_);
33              
34             # fix up dir (for example chop trailing slash if present)
35 12         82 $self->{dir} = $self->path_class_dir->new($self->{dir})->stringify;
36              
37             # TODO: croak if not -d $self->dir
38              
39 12         1437 return $self;
40             }
41              
42 14     14 1 59 sub required_attribute { 'dir' }
43              
44              
45             sub determine_name_and_version {
46 10     10 1 919 my ($self) = @_;
47             # 'root' may be more accurate than 'dir'
48 10         68 $self->SUPER::determine_name_and_version();
49 10         39 $self->set_name_and_version( $self->parse_name_and_version( $self->dir ) );
50 10         28 return;
51             }
52              
53              
54             sub dir {
55 12     12 1 844 $_[0]->{dir};
56             }
57              
58             # this shouldn't be called
59             sub extract_into {
60 1     1 1 136 croak q[A directory doesn't need to be extracted];
61             }
62              
63              
64             sub file_content {
65 6     6 1 4184 my ($self, $file) = @_;
66             # This is a directory so file spec will always be native
67 6         28 my $path = $self->path_class_file
68             ->new( $self->{dir}, $self->full_path($file) )->stringify;
69              
70 6 100       3386 open(my $fh, '<', $path)
71             or croak "Failed to open file '$path': $!";
72              
73 4         9 return do { local $/; <$fh> };
  4         21  
  4         2850  
74             }
75              
76              
77             sub find_files {
78 13     13 1 1883 my ($self) = @_;
79              
80 13         33 my $dir = $self->{dir};
81 13         17 my @files;
82              
83             File::Find::find(
84             {
85             wanted => sub {
86 125 100   125   18757 push @files, $self->path_class_file->new($_)->relative($dir)->stringify
87             if -f $_;
88             },
89 13         1443 no_chdir => 1
90             },
91             $dir
92             );
93              
94 13         4452 return @files;
95             }
96              
97              
98             sub physical_directory {
99 11     11 1 892 my ($self, @files) = @_;
100              
101             # TODO: return absolute_path?
102 11         35 my @parts = $self->{dir};
103             # go into root dir if there is one
104 11 100       65 push @parts, $self->root
105             if $self->root;
106              
107 11         63 my $dir = $self->path_class_dir->new(@parts)->absolute;
108              
109 11 100       1964 return $dir->stringify unless wantarray;
110              
111 21         1624 return map { $_->stringify }
  12         643  
112 9         22 ($dir, map { $dir->file( $_ ) } @files);
113             }
114              
115             1;
116              
117             __END__