File Coverage

blib/lib/Bio/Gonzales/Util/Development/File.pm
Criterion Covered Total %
statement 41 62 66.1
branch 0 8 0.0
condition 0 9 0.0
subroutine 14 17 82.3
pod 1 1 100.0
total 56 97 57.7


line stmt bran cond sub pod time code
1             #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2              
3             package Bio::Gonzales::Util::Development::File;
4              
5 13     13   91 use warnings;
  13         110  
  13         467  
6 13     13   102 use strict;
  13         14  
  13         247  
7 13     13   64 use Carp;
  13         26  
  13         661  
8              
9 13     13   101 use File::Spec;
  13         27  
  13         272  
10 13     13   7069 use Path::Class;
  13         401936  
  13         1165  
11 13     13   140 use Path::Class::Dir;
  13         27  
  13         999  
12 13     13   114 use Path::Class::File;
  13         27  
  13         320  
13 13     13   89 use Bio::Gonzales::Util::Development::File;
  13         26  
  13         385  
14 13     13   141 use File::Find;
  13         26  
  13         1198  
15 13     13   8182 use List::MoreUtils qw/any all/;
  13         193410  
  13         79  
16 13     13   16119 use Cwd;
  13         29  
  13         889  
17 13     13   245 use 5.010;
  13         40  
18 13     13   65 use Data::Dumper;
  13         26  
  13         635  
19              
20 13     13   67 use base 'Exporter';
  13         26  
  13         6206  
21             our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
22             our $VERSION = '0.083'; # VERSION
23              
24             @EXPORT = qw();
25             %EXPORT_TAGS = ();
26             @EXPORT_OK = qw(find_root);
27              
28              
29             sub find_root {
30 0     0 1   my %default = (
31             location => '.',
32             dirs => [],
33             files => [],
34             );
35              
36 0           my %o = ( %default, %{ $_[0] } );
  0            
37              
38 0           my $filesystem_root = dir('');
39 0           my $module_root;
40              
41 0 0         if ( -f $o{location} ) {
    0          
42             #take absolute directory where the given file resides in
43 0           $module_root = file( $o{location} )->dir->absolute('');
44             } elsif ( -d $o{location} ) {
45             #take absolute directory if location is a dir
46 0           $module_root = file( $o{location} )->absolute('');
47             } else {
48 0           return;
49             }
50              
51 0           while (1) {
52             #stop if at / dir
53             return
54 0 0         if ( $module_root eq $filesystem_root );
55              
56             #check if all 'directory criterions' are fullfilled
57 0     0     my $status_dirs = all { -d dir( $module_root, $_ ) } @{ $o{dirs} };
  0            
  0            
58              
59             #check if all 'file criterions' are fullfilled
60 0     0     my $status_files = all { -f file( $module_root, $_ ) } @{ $o{files} };
  0            
  0            
61              
62 0 0 0       if ( ( !defined($status_dirs) || $status_dirs )
      0        
      0        
63             && ( !defined($status_files) || $status_files ) )
64             {
65 0           last;
66             }
67              
68             #nothing found, go to next parent dir
69 0           $module_root = $module_root->parent();
70             }
71              
72 0           return $module_root;
73             }
74              
75             1;
76              
77             __END__
78              
79             =head1 NAME
80              
81             Bio::Gonzales::Util::Development::File - Helper functions for all filesystem related tasks
82              
83             =head1 SYNOPSIS
84              
85             use Bio::Gonzales::Util::Development::File qw/find_root/;
86              
87             # find git root dir
88             my $root = find_root({location => '.', dirs => [ '.git ]});
89              
90              
91             =head1 SUBROUTINES
92              
93             =over 4
94              
95             =item B<< $project_root_directory = find_root({location => $location, dirs => \@dirs, files => \@files}) >>
96              
97             Starts at C<$location> and stops if the current or parent dir contains all of
98             the directories specified by C<@dirs> and all of the files specified by
99             C<@files>. Returns the dir where stopped or nothing/undef if not successful
100              
101             =back
102              
103             =head1 SEE ALSO
104              
105             -
106              
107             =head1 AUTHOR
108              
109             jw bargsten, C<< <joachim.bargsten at wur.nl> >>
110              
111             =cut