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   359523 use strict;
  8         69  
  8         227  
3              
4 8     8   42 use strict;
  8         15  
  8         130  
5 8     8   36 use warnings;
  8         15  
  8         204  
6 8     8   158 use 5.010;
  8         26  
7              
8             # ABSTRACT: easier loading of a project's local libs
9             our $VERSION = "1.006";
10              
11 8     8   3728 use FindBin qw();
  8         8823  
  8         206  
12 8     8   53 use Carp qw(carp);
  8         16  
  8         377  
13 8     8   511 use File::Spec::Functions qw(catdir splitdir);
  8         834  
  8         353  
14 8     8   4554 use local::lib qw();
  8         53193  
  8         202  
15 8     8   528 use lib qw();
  8         691  
  8         5597  
16              
17             our $ROOT;
18              
19             sub import {
20 7     7   66 my $class = shift;
21 7         34 my @libdirs;
22             my @locallibs;
23 7         0 my @extra;
24 7         0 my @extra_with_local;
25 7         21 foreach my $d (@_) {
26 11 100       66 if ( $d =~ /^local::lib=([\S]+)/ ) {
    100          
    50          
27 2         6 push( @libdirs, $1 );
28 2         6 push( @locallibs, $1 );
29             }
30             elsif ( $d =~ /^extra=([\S]+)/ ) {
31 1         7 @extra = split( /[,;]/, $1 );
32             }
33             elsif ( $d =~ /^extra_with_local=([\S]+)/ ) {
34 0         0 @extra_with_local = split( /[,;]/, $1 );
35             }
36             else {
37 8         46 push( @libdirs, $d );
38             }
39             }
40              
41 7         41 my @searchdirs = splitdir("$FindBin::Bin");
42              
43 7 50       86 unless ($ROOT) {
44 7         35 SEARCH: while (@searchdirs) {
45 31         60 foreach my $dir (@libdirs) {
46 38 100       841 unless ( -d catdir( @searchdirs, $dir ) ) {
47 26         71 pop(@searchdirs);
48 26         83 next SEARCH;
49             }
50             }
51 5         34 $ROOT = catdir(@searchdirs);
52 5         15 last SEARCH;
53             }
54             }
55              
56 7 100       26 if ($ROOT) {
57 5 100       19 local::lib->import( map { catdir( $ROOT, $_ ) } @locallibs )
  1         10  
58             if @locallibs;
59 5 50       1712 lib->import( map { catdir( $ROOT, $_ ) } @libdirs ) if @libdirs;
  6         55  
60 5 100       672 __PACKAGE__->load_extra(@extra) if @extra;
61 5 50       7063 __PACKAGE__->load_extra_with_local(@extra_with_local)
62             if @extra_with_local;
63             }
64             else {
65 2         539 carp "Could not find root dir containing " . join( ', ', @libdirs );
66             }
67             }
68              
69             sub load_extra {
70 2     2 0 88 my $class = shift;
71 2         6 my @extras = @_;
72              
73 2         10 my @parts = splitdir($ROOT);
74 2         17 pop(@parts);
75 2         13 my $parent = catdir(@parts);
76              
77 2         14 foreach my $d (@extras) {
78 2         10 my $extra = catdir( $parent, $d, 'lib' );
79 2 50       43 if ( -d $extra ) {
80 2         14 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__