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 = '1017';
5 11     11   66 use strict;
  11         23  
  11         279  
6 11     11   54 use warnings;
  11         26  
  11         258  
7 11     11   47 use Carp qw( croak );
  11         61  
  11         501  
8 11     11   4048 use Module::Data;
  11         53392  
  11         321  
9 11     11   4340 use Path::Class;
  11         290060  
  11         511  
10 11     11   83 use Package::Stash;
  11         27  
  11         218  
11 11     11   4856 use File::ShareDir ':ALL';
  11         47258  
  11         4949  
12              
13             require Moo::Role;
14              
15              
16             my %applied;
17              
18             sub apply_keywords {
19 45     45 1 118 my ( $class, $target ) = @_;
20              
21 45 50       165 return if exists $applied{$target};
22 45         109 $applied{$target} = undef;
23              
24 45         165 my @parts = split('::',$target);
25 45         92 shift @parts;
26 45         119 unshift @parts, 'share';
27 45         109 my $share_path = join('/',map { s/([a-z])([A-Z])/$1_$2/g; lc; } @parts);
  135         506  
  135         380  
28              
29 45         806 my $moddata = Module::Data->new($target);
30 45         21726 my $basedir = $moddata->root->parent;
31              
32 45         145688 my $share;
33              
34 45 50 33     188 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         2090 eval {
39 45         198 my $dir = module_dir($target);
40 0 0       0 $share = dir($dir) if -d $dir;
41             }
42             }
43              
44 45 50       22627 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 87 my $basedir = shift;
73 45 50       285 return -e $basedir->subdir('lib') if $basedir->can('subdir');
74 45         209 return -e $basedir->child('lib');
75             }
76              
77             sub get_share {
78 45     45 0 2329 my $basedir = shift;
79 45 50       208 return -e $basedir->subdir('share') if $basedir->can('subdir');
80 45         159 return -e $basedir->child('share');
81             }
82             1;
83              
84             __END__