File Coverage

blib/lib/DDG/Meta/ShareDir.pm
Criterion Covered Total %
statement 43 52 82.6
branch 5 18 27.7
condition 1 3 33.3
subroutine 10 12 83.3
pod 1 3 33.3
total 60 88 68.1


line stmt bran cond sub pod time code
1             package DDG::Meta::ShareDir;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: Installing functions for easy access to the module sharedir
4             $DDG::Meta::ShareDir::VERSION = '1018';
5 11     11   75 use strict;
  11         29  
  11         338  
6 11     11   87 use warnings;
  11         25  
  11         377  
7 11     11   70 use Carp qw( croak );
  11         27  
  11         722  
8 11     11   3465 use Module::Data;
  11         73100  
  11         404  
9 11     11   4119 use Path::Class;
  11         396954  
  11         776  
10 11     11   135 use Package::Stash;
  11         40  
  11         334  
11 11     11   4994 use File::ShareDir ':ALL';
  11         65420  
  11         7587  
12              
13             require Moo::Role;
14              
15              
16             my %applied;
17              
18             sub apply_keywords {
19 45     45 1 153 my ( $class, $target ) = @_;
20              
21 45 50       188 return if exists $applied{$target};
22 45         171 $applied{$target} = undef;
23              
24 45         241 my @parts = split('::',$target);
25 45         112 shift @parts;
26 45         152 unshift @parts, 'share';
27 45         129 my $share_path = join('/',map { s/([a-z])([A-Z])/$1_$2/g; lc; } @parts);
  135         621  
  135         428  
28              
29 45         1015 my $moddata = Module::Data->new($target);
30 45         29297 my $basedir = $moddata->root->parent;
31              
32 45         196779 my $share;
33              
34 45 50 33     191 if ( get_lib($basedir) and get_share($basedir) ) {
35 0         0 my $dir = dir($basedir,$share_path);
36 0 0       0 $share = $dir if -d $dir;
37             } else {
38 45         2232 eval {
39 45         243 my $dir = module_dir($target);
40 0 0       0 $share = dir($dir) if -d $dir;
41             }
42             }
43              
44 45 50       26017 if ($share) {
45 0         0 my $stash = Package::Stash->new($target);
46              
47              
48             $stash->add_symbol('&share', sub {
49 0 0   0   0 @_ ? -d dir($share,@_)
    0          
50             ? $share->subdir(@_)
51             : $share->file(@_)
52             : $share
53 0         0 });
54              
55              
56 0     0   0 $stash->add_symbol('&module_share_dir', sub { $share_path });
  0         0  
57              
58             #
59             # apply role
60             #
61              
62 0         0 Moo::Role->apply_role_to_package($target,'DDG::HasShareDir');
63             }
64              
65             }
66              
67             # Temporarily add support for both `subdir` and `child` methods
68             # to ensure backwards compatibilty while we upgrade Module::Data
69             # throughout our stack
70             # More info: https://github.com/duckduckgo/duckduckgo/pull/244
71             sub get_lib {
72 45     45 0 114 my $basedir = shift;
73 45 50       391 return -e $basedir->subdir('lib') if $basedir->can('subdir');
74 45         199 return -e $basedir->child('lib');
75             }
76              
77             sub get_share {
78 45     45 0 2446 my $basedir = shift;
79 45 50       239 return -e $basedir->subdir('share') if $basedir->can('subdir');
80 45         166 return -e $basedir->child('share');
81             }
82             1;
83              
84             __END__