Debian Codegraph @

lib/Dancer2/Core/Role/HasLocation.pm

raw
1 package Dancer2::Core::Role::HasLocation;
2 # ABSTRACT: Role for application location "guessing"
3 $Dancer2::Core::Role::HasLocation::VERSION = '2.2.2';
4 use Carp ();
5 use Moo::Role;
6 use Sub::Quote 'quote_sub';
7 use Path::Tiny ();
8 use Dancer2::Core::Types;
9
10 # the path to the caller script/app
11 # Note: to remove any ambiguity between the accessor for the
12 # 'caller' attribute and the core function caller(), explicitly
13 # specify we want the function 'CORE::caller' as the default for
14 # the attribute.
15 has caller => (
16 is => 'ro',
17 isa => Str,
18 default => quote_sub( q{
19 require Path::Tiny;
20 my ( $caller, $script ) = CORE::caller;
21 Path::Tiny::path($script)->relative->stringify;
22 } ),
23 );
24
25 has location => (
26 is => 'ro',
27 isa => Str,
28 lazy => 1,
29 builder => '_build_location',
30 );
31
32 has _location_path => (
33 is => 'ro',
34 lazy => 1,
35 builder => '_build_location_path',
36 init_arg => undef,
37 );
38
39 sub _build_location_path {
40 my $self = shift;
41 return Path::Tiny::path( $self->location );
42 }
43
44 # Config file basenames a Dancer2 app root may hold. Keep this in sync with
45 # Config::Any->extensions, which is what Dancer2::ConfigReader actually reads.
46 my @_config_files = map "config.$_",
47 qw< cnf conf ini json jsn pl perl xml yml yaml >;
48
49 # Directories a Dancer2 app root may hold. These are precisely the things
50 # 'location' is used to resolve, so a directory holding one of them is a
51 # directory worth calling the app root.
52 my @_app_dirs = qw< environments views public >;
53
54 # Files and directories marking the root of a checkout or a distribution.
55 my @_project_markers =
56 qw< .git .hg .svn Makefile.PL Build.PL dist.ini cpanfile >;
57
58 # Is this directory a ceiling the walk must not cross? Whatever the app root
59 # is, it sits at or below its own project root, never in the home directory
60 # or the build directory above it. Only consulted for directories _is_app_root
61 # has already rejected, so a project root that is also an app root - which is
62 # the usual case, 'dancer2 gen' writes a cpanfile and a Makefile.PL - has been
63 # accepted long before this is reached.
64 sub _is_project_boundary {
65 my $dir = shift;
66 $dir->child($_)->exists and return 1 for @_project_markers;
67 return 0;
68 }
69
70 # Does this directory look like the root of a Dancer2 application?
71 #
72 # Historically the only test was "holds both lib/ and bin/". That is a poor
73 # proxy: it matches every Perl distribution root, most home directories, /usr
74 # and /usr/local. It went unnoticed for years because the upward walk in
75 # _build_location was effectively stalling, so it rarely got the chance to
76 # leave the script's own directory. Once the walk started working the weak
77 # test began selecting directories far above the real app. See GH #1781.
78 sub _is_app_root {
79 my $dir = shift;
80
81 # explicit marker, placed by 'dancer2 gen' and by hand; always definitive
82 $dir->child('.dancer')->is_file
83 and return 1;
84
85 # actual Dancer2 artifacts
86 $dir->child($_)->is_dir and return 1 for @_app_dirs;
87 $dir->child($_)->is_file and return 1 for @_config_files;
88
89 # Legacy heuristic, kept for apps that carry no Dancer2 artifacts at all.
90 # Checked last so anything above only wins when it is found closer to the
91 # script. blib/ is skipped: it holds lib/ and bin/, but never views/ or
92 # public/, so it is never the app root.
93 return $dir !~ m![\\/]blib[\\/]?$!
94 && $dir->child('lib')->is_dir
95 && $dir->child('bin')->is_dir;
96 }
97
98 # FIXME: i hate you most of all -- Sawyer X
99 sub _build_location {
100 my $self = shift;
101 my $script = $self->caller;
102
103 # default to the dir that contains the script...
104 my $location = Path::Tiny::path($script)->parent;
105
106 $location->is_dir
107 or Carp::croak("Caller $script is not an existing file");
108
109 # Walk up looking for the app root, closest match winning. Canonicalise
110 # first: the walk compares path strings, and a relative $subdir turns into
111 # a chain of '..' segments that no longer matches anything useful.
112 my $subdir = $location->realpath;
113 my $subdir_found = 0;
114
115 #maximum of 10 iterations, to prevent infinite loop
116 for ( 1 .. 10 ) {
117 if ( _is_app_root($subdir) ) {
118 $subdir_found = 1;
119 last;
120 }
121
122 # a checkout or distribution root that is not itself an app root:
123 # stop rather than escape into whatever happens to lie above it
124 last if _is_project_boundary($subdir);
125
126 # stop at the volume root, where parent() is its own fixed point
127 my $parent = $subdir->parent;
128 last if $parent->stringify eq $subdir->stringify;
129
130 $subdir = $parent;
131 }
132
133 my $path = $subdir_found ? $subdir : $location;
134
135 # convert relative to absolute
136 return $path->realpath->stringify;
137 }
138
139 1;
140
141 __END__
142
143 =pod
144
145 =encoding UTF-8
146
147 =head1 NAME
148
149 Dancer2::Core::Role::HasLocation - Role for application location "guessing"
150
151 =head1 VERSION
152
153 version 2.2.2
154
155 =head1 AUTHOR
156
157 Dancer Core Developers
158
159 =head1 COPYRIGHT AND LICENSE
160
161 This software is copyright (c) 2026 by Alexis Sukrieh.
162
163 This is free software; you can redistribute it and/or modify it under
164 the same terms as the Perl 5 programming language system itself.
165
166 =cut