File Coverage

blib/lib/lib/projectroot.pm
Criterion Covered Total %
statement 62 78 79.4
branch 17 26 65.3
condition n/a
subroutine 11 12 91.6
pod 0 2 0.0
total 90 118 76.2


line stmt bran cond sub pod time code
1             package lib::projectroot;
2 8     8   292759 use strict;
  8         51  
  8         188  
3              
4 8     8   32 use strict;
  8         12  
  8         133  
5 8     8   32 use warnings;
  8         9  
  8         184  
6 8     8   136 use 5.010;
  8         21  
7              
8             # ABSTRACT: easier loading of a project's local libs
9             our $VERSION = "1.007";
10              
11 8     8   3062 use FindBin qw();
  8         7253  
  8         171  
12 8     8   73 use Carp qw(carp);
  8         12  
  8         325  
13 8     8   403 use File::Spec::Functions qw(catdir splitdir);
  8         689  
  8         301  
14 8     8   3626 use local::lib qw();
  8         40757  
  8         173  
15 8     8   472 use lib qw();
  8         590  
  8         4507  
16              
17             our $ROOT;
18              
19             sub import {
20 7     7   58 my $class = shift;
21 7         26 my @libdirs;
22             my @locallibs;
23 7         0 my @extra;
24 7         0 my @extra_with_local;
25 7         17 foreach my $d (@_) {
26 11 100       53 if ( $d =~ /^local::lib=([\S]+)/ ) {
    100          
    50          
27 2         7 push( @libdirs, $1 );
28 2         5 push( @locallibs, $1 );
29             }
30             elsif ( $d =~ /^extra=([\S]+)/ ) {
31 1         5 @extra = split( /[,;]/, $1 );
32             }
33             elsif ( $d =~ /^extra_with_local=([\S]+)/ ) {
34 0         0 @extra_with_local = split( /[,;]/, $1 );
35             }
36             else {
37 8         44 push( @libdirs, $d );
38             }
39             }
40              
41 7         33 my @searchdirs = splitdir("$FindBin::Bin");
42              
43 7 50       75 unless ($ROOT) {
44 7         31 SEARCH: while (@searchdirs) {
45 31         46 foreach my $dir (@libdirs) {
46 38 100       806 unless ( -d catdir( @searchdirs, $dir ) ) {
47 26         55 pop(@searchdirs);
48 26         75 next SEARCH;
49             }
50             }
51 5         26 $ROOT = catdir(@searchdirs);
52 5         11 last SEARCH;
53             }
54             }
55              
56 7 100       23 if ($ROOT) {
57 5 100       14 local::lib->import( map { catdir( $ROOT, $_ ) } @locallibs )
  1         10  
58             if @locallibs;
59 5 50       1583 lib->import( map { catdir( $ROOT, $_ ) } @libdirs ) if @libdirs;
  6         49  
60 5 100       590 __PACKAGE__->load_extra(@extra) if @extra;
61 5 50       6274 __PACKAGE__->load_extra_with_local(@extra_with_local)
62             if @extra_with_local;
63             }
64             else {
65 2         521 carp "Could not find root dir containing " . join( ', ', @libdirs );
66             }
67             }
68              
69             sub load_extra {
70 2     2 0 76 my $class = shift;
71 2         5 my @extras = @_;
72              
73 2         8 my @parts = splitdir($ROOT);
74 2         25 pop(@parts);
75 2         11 my $parent = catdir(@parts);
76              
77 2         12 foreach my $d (@extras) {
78 2         8 my $extra = catdir( $parent, $d, 'lib' );
79 2 50       32 if ( -d $extra ) {
80 2         11 push( @INC, $extra );
81             }
82             else {
83 0           carp "Cannot load_extra $d, directory $extra does not exist";
84             }
85             }
86             }
87              
88             sub load_extra_with_local {
89 0     0 0   my $class = shift;
90 0           my @extras = @_;
91              
92 0           my @parts = splitdir($ROOT);
93 0           pop(@parts);
94 0           my $parent = catdir(@parts);
95              
96 0           foreach my $d (@extras) {
97 0           my $extra = catdir( $parent, $d, 'lib' );
98 0 0         if ( -d $extra ) {
99 0           push( @INC, $extra );
100 0           my $extra_local = catdir( $parent, $d, 'local' );
101 0 0         if ( -d $extra_local ) {
102 0           local::lib->import($extra_local);
103             }
104             else {
105 0           carp
106             "Cannot load local::lib in extra $d, directory $extra_local does not exist";
107              
108             }
109             }
110             else {
111 0           carp "Cannot load_extra $d, directory $extra does not exist";
112             }
113             }
114             }
115              
116             42;
117              
118             __END__