File Coverage

blib/lib/Dancer2/Core/Role/HasLocation.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition 11 11 100.0
subroutine 6 6 100.0
pod n/a
total 57 57 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Role for application location "guessing"
2             $Dancer2::Core::Role::HasLocation::VERSION = '0.400000';
3             use Moo::Role;
4 143     143   80931 use Dancer2::Core::Types;
  143         371  
  143         904  
5 143     143   59085 use Dancer2::FileUtils;
  143         425  
  143         1183  
6 143     143   1162566 use File::Spec;
  143         350  
  143         6254  
7 143     143   849 use Sub::Quote 'quote_sub';
  143         363  
  143         4559  
8 143     143   786  
  143         311  
  143         47773  
9             # the path to the caller script/app
10             # Note: to remove any ambiguity between the accessor for the
11             # 'caller' attribute and the core function caller(), explicitly
12             # specify we want the function 'CORE::caller' as the default for
13             # the attribute.
14             has caller => (
15             is => 'ro',
16             isa => Str,
17             default => quote_sub( q{
18             my ( $caller, $script ) = CORE::caller;
19             $script = File::Spec->abs2rel( $script ) if File::Spec->file_name_is_absolute( $script );
20             $script;
21             } ),
22             );
23              
24             has location => (
25             is => 'ro',
26             builder => '_build_location',
27             );
28              
29             # FIXME: i hate you most of all -- Sawyer X
30             my $self = shift;
31             my $script = $self->caller;
32 238     238   21120  
33 238         1124 # default to the dir that contains the script...
34             my $location = Dancer2::FileUtils::dirname($script);
35              
36 238         1245 #we try to find bin and lib
37             my $subdir = $location;
38             my $subdir_found = 0;
39 238         848  
40 238         627 #maximum of 10 iterations, to prevent infinite loop
41             for ( 1 .. 10 ) {
42              
43 238         1014 #try to find libdir and bindir to determine the root of dancer app
44             my $libdir = Dancer2::FileUtils::path( $subdir, 'lib' );
45             my $bindir = Dancer2::FileUtils::path( $subdir, 'bin' );
46 2309         7189  
47 2309         4473 #try to find .dancer_app file to determine the root of dancer app
48             my $dancerdir = Dancer2::FileUtils::path( $subdir, '.dancer' );
49              
50 2309         4360 # if one of them is found, keep that; but skip ./blib since both lib and bin exist
51             # under it, but views and public do not.
52             if (
53             ( $subdir !~ m![\\/]blib[\\/]?$! && -d $libdir && -d $bindir ) ||
54 2309 100 100     57220 ( -f $dancerdir )
      100        
      100        
55             ) {
56             $subdir_found = 1;
57             last;
58 8         30 }
59 8         23  
60             $subdir = Dancer2::FileUtils::path( $subdir, '..' ) || '.';
61             last if File::Spec->rel2abs($subdir) eq File::Spec->rootdir;
62 2301   100     7662  
63 2301 100       45420 }
64              
65             my $path = $subdir_found ? $subdir : $location;
66              
67 238 100       1400 # return if absolute
68             File::Spec->file_name_is_absolute($path)
69             and return $path;
70 238 100       1473  
71             # convert relative to absolute
72             return File::Spec->rel2abs($path);
73             }
74 233         8424  
75             1;
76              
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             Dancer2::Core::Role::HasLocation - Role for application location "guessing"
85              
86             =head1 VERSION
87              
88             version 0.400000
89              
90             =head1 AUTHOR
91              
92             Dancer Core Developers
93              
94             =head1 COPYRIGHT AND LICENSE
95              
96             This software is copyright (c) 2022 by Alexis Sukrieh.
97              
98             This is free software; you can redistribute it and/or modify it under
99             the same terms as the Perl 5 programming language system itself.
100              
101             =cut