File Coverage

blib/lib/Dist/Metadata/Dir.pm
Criterion Covered Total %
statement 49 49 100.0
branch 8 8 100.0
condition n/a
subroutine 15 15 100.0
pod 8 8 100.0
total 80 80 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   892 use strict;
  2         3  
  2         70  
11 2     2   8 use warnings;
  2         3  
  2         134  
12              
13             package Dist::Metadata::Dir;
14             our $AUTHORITY = 'cpan:RWSTAUNER';
15             # ABSTRACT: Enable Dist::Metadata for a directory
16             $Dist::Metadata::Dir::VERSION = '0.927';
17 2     2   9 use Carp qw(croak carp); # core
  2         2  
  2         126  
18 2     2   7 use File::Find (); # core
  2         2  
  2         32  
19 2     2   7 use Path::Class 0.24 ();
  2         44  
  2         40  
20 2     2   6 use parent 'Dist::Metadata::Dist';
  2         2  
  2         12  
21              
22             push(@Dist::Metadata::CARP_NOT, __PACKAGE__);
23              
24              
25             sub new {
26 13     13 1 1599 my $class = shift;
27 13         68 my $self = $class->SUPER::new(@_);
28              
29             # fix up dir (for example chop trailing slash if present)
30 12         52 $self->{dir} = $self->path_class_dir->new($self->{dir})->stringify;
31              
32             # TODO: croak if not -d $self->dir
33              
34 12         776 return $self;
35             }
36              
37 14     14 1 57 sub required_attribute { 'dir' }
38              
39              
40             sub determine_name_and_version {
41 10     10 1 614 my ($self) = @_;
42             # 'root' may be more accurate than 'dir'
43 10         44 $self->SUPER::determine_name_and_version();
44 10         25 $self->set_name_and_version( $self->parse_name_and_version( $self->dir ) );
45 10         18 return;
46             }
47              
48              
49             sub dir {
50 12     12 1 547 $_[0]->{dir};
51             }
52              
53             # this shouldn't be called
54             sub extract_into {
55 1     1 1 91 croak q[A directory doesn't need to be extracted];
56             }
57              
58              
59             sub file_content {
60 6     6 1 2612 my ($self, $file) = @_;
61             # This is a directory so file spec will always be native
62             my $path = $self->path_class_file
63 6         14 ->new( $self->{dir}, $self->full_path($file) )->stringify;
64              
65 6 100       1588 open(my $fh, '<', $path)
66             or croak "Failed to open file '$path': $!";
67              
68 4         6 return do { local $/; <$fh> };
  4         16  
  4         108  
69             }
70              
71              
72             sub find_files {
73 13     13 1 1141 my ($self) = @_;
74              
75 13         21 my $dir = $self->{dir};
76 13         14 my @files;
77              
78             File::Find::find(
79             {
80             wanted => sub {
81 125 100   125   9823 push @files, $self->path_class_file->new($_)->relative($dir)->stringify
82             if -f $_;
83             },
84 13         1089 no_chdir => 1
85             },
86             $dir
87             );
88              
89 13         2487 return @files;
90             }
91              
92              
93             sub physical_directory {
94 11     11 1 763 my ($self, @files) = @_;
95              
96             # TODO: return absolute_path?
97 11         25 my @parts = $self->{dir};
98             # go into root dir if there is one
99 11 100       33 push @parts, $self->root
100             if $self->root;
101              
102 11         31 my $dir = $self->path_class_dir->new(@parts)->absolute;
103              
104 11 100       1113 return $dir->stringify unless wantarray;
105              
106 21         642 return map { $_->stringify }
107 9         18 ($dir, map { $dir->file( $_ ) } @files);
  12         214  
108             }
109              
110             1;
111              
112             __END__