blib/lib/File/Dir/Map.pm

filter: Statementstmt 38.8% 21 / 54 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 50.0% 1 / 2 totaltotal 36.3% 28 / 77 CC 10 SCAR 31.3
CC
10
Cov
32%
dirmap
42.0
__ANON__
30.0
inventory
2.0
CRAP 22.8
↑ Summary
1 package File::Dir::Map;
2 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our @EXPORT_OK = qw(dirmap);
10  
11 =head1 NAME
12  
13 File::Dir::Map - Map a directory recursively
14  
15 =head1 VERSION
16  
17 Version 0.01
18  
19 =cut
20  
21 1 our $VERSION = '0.02';
22  
23 =head1 SYNOPSIS
24  
25 The following snippet summarizes what File::Dir::Map does.
26  
27 Files are copied from src to build dirs such that files
28 with extension ignore will not be copied, files with extension
29 markdown will get processed by markdown and saved in build dir,
30 and files with extension raw are guaranteed to be copied without
31 processing but stripped of the raw extension.
32  
33 use File::Dir::Map qw(dirmap);
34 use Text::MultiMarkdown qw(markdown);
35
36 dirmap src => build => {
37 ignore => sub{ [] },
38 raw => sub{
39 my( $name, $content ) = @_;
40 [ $name, $content ];
41 },
42 markrown => sub{
43 my( $name, $contnet ) = @_;
44 [ "$name.html", markdown $content ]
45 },
46 };
47  
48 So this before the map:
49  
50 src/pages/todo.ignore
51 src/pages/index.markdown
52 src/pages/about.html
53 src/pages/index.markdown.raw
54 src/files/
55 build/some/junk/here.txt
56  
57 Will be this after the map:
58  
59 src/pages/todo.ignore
60 src/pages/index.markdown
61 src/pages/about.html
62 src/pages/index.markdown.raw
63 src/files/
64 build/pages/index.html
65 build/pages/about.html
66 build/pages/index.markdown
67 build/files/
68  
69 Note that old build directory is purged!
70  
71 =head1 EXPORT
72  
73 dirmap
74  
75 =head1 SUBROUTINES/METHODS
76  
77 =cut
78  
79 0 ▶sub inventory ($$) {
Subroutine inventory: not called CC=1 SCAR=6.9
Pod undocumented
80 0 my ( $dir, $familiar ) = @_;
81 0 my $inv;
82 find(
83 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
84 0 $File::Find::name =~ m{^$dir/(.*?)(?:\.(.*?))?$};
85 0 ▶ if (-d) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
elsif (grep {$_ eq $2;} @$familiar) { } 0 0 0
86 0 ▶ push @{ $inv->{dirs} }, $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
87 }
88 0 elsif ( grep { $_ eq $2 } @$familiar ) {
89 0 $inv->{items}->{$1}->{$2} = $File::Find::name;
90 }
91 else {
92 0 ▶ $inv->{files}->{ $1 . ( $2 ? ".$2" : '' ) } = $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 0 0
93 }
94 },
95 0 $dir
96 );
97 0 $inv;
98 }
99  
100 =head2 dirmap $dir_from, $dir_to, $funcs
101  
102 $funcs is a hashref with file extensions as keys and mapping functions as
103 values.
104  
105 Each mapping function takes two arguments: filename stripped of the extension
106 and file contents.
107  
108 Each mapping function should return a listref that's either empty or contains
109 two elements: new filename and new content. In case the listref is empty,
110 file will not be saved in the destination directory.
111  
112 =cut
113  
114 0 ▶sub dirmap ($$$) {
Subroutine dirmap: not called CC=6 SCAR=37.4
Pod documented
115 0 my ( $dir_from, $dir_to, $funcs ) = @_;
116 0 my $inv = inventory $dir_from => [ keys %$funcs ];
117 0 remove_tree( $dir_to, { keep_root => 1, safe => 1 } );
118 0 make_path( map { join '/', $dir_to, $_ } sort @{ $inv->{dirs} } );
119 copy( $inv->{files}->{$_}, join '/', $dir_to, $_ )
120 0 for keys %{ $inv->{files} };
121  
122 0 for my $item ( keys %{ $inv->{items} } ) {
123 0 for my $type ( keys %{ $inv->{items}->{$item} } ) {
124 0 open my $fh, '<', $inv->{items}->{$item}->{$type};
125 0 local $INPUT_RECORD_SEPARATOR = undef;
126 0 my $content_in = <$fh>;
127 0 close $fh;
128  
129 my ( $filename, $content_out ) =
130 0 @{ $funcs->{$type}->( $item, $content_in ) };
131  
132 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
133 0 ▶ open my $fh, '>', join '/', $dir_to, $filename or die $ERRNO;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", join("/", $dir_to, $filename) or die $File::Dir::Map::ERRNO 0 0 0
134 0 print $fh $content_out;
135 0 close $fh;
136 }
137 }
138 }
139 }
140  
141 =head1 AUTHOR
142  
143 Eugene Grigoriev, C<< <perl at sizur.com> >>
144  
145 =head1 BUGS
146  
147 Please report any bugs or feature requests to C<bug-file-dir-map at rt.cpan.org>, or through
148 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Dir-Map>. I will be notified, and then you'll
149 automatically be notified of progress on your bug as I make changes.
150  
151 =head1 SUPPORT
152  
153 You can find documentation for this module with the perldoc command.
154  
155 perldoc File::Dir::Map
156  
157  
158 You can also look for information at:
159  
160 =over 4
161  
162 =item * RT: CPAN's request tracker
163  
164 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Dir-Map>
165  
166 =item * AnnoCPAN: Annotated CPAN documentation
167  
168 L<http://annocpan.org/dist/File-Dir-Map>
169  
170 =item * CPAN Ratings
171  
172 L<http://cpanratings.perl.org/d/File-Dir-Map>
173  
174 =item * Search CPAN
175  
176 L<http://search.cpan.org/dist/File-Dir-Map/>
177  
178 =back
179  
180  
181 =head1 ACKNOWLEDGEMENTS
182  
183  
184 =head1 LICENSE AND COPYRIGHT
185  
186 Copyright 2010 Eugene Grigoriev.
187  
188 This program is free software; you can redistribute it and/or modify it
189 under the terms of either: the GNU General Public License as published
190 by the Free Software Foundation; or the Artistic License.
191  
192 See http://dev.perl.org/licenses/ for more information.
193  
194 =cut
195  
196 1 1; # End of File::Dir::Map
↑ Summary