copy gitweblite soruce code
|
1 |
package Mojo::Home; |
2 |
use Mojo::Base -base; |
|
update Mojolicious to 4.57
|
3 |
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; |
copy gitweblite soruce code
|
4 | |
5 |
use Cwd 'abs_path'; |
|
6 |
use File::Basename 'dirname'; |
|
7 |
use File::Find 'find'; |
|
upgraded Mojolicious to v3.7...
|
8 |
use File::Spec::Functions qw(abs2rel catdir catfile splitdir); |
copy gitweblite soruce code
|
9 |
use FindBin; |
upgraded Mojolicious to v3.7...
|
10 |
use Mojo::Util qw(class_to_path slurp); |
copy gitweblite soruce code
|
11 | |
update Mojolicious to 4.57
|
12 |
has parts => sub { [] }; |
13 | ||
copy gitweblite soruce code
|
14 |
sub new { shift->SUPER::new->parse(@_) } |
15 | ||
16 |
sub detect { |
|
upgraded Mojolicious to v3.7...
|
17 |
my $self = shift; |
copy gitweblite soruce code
|
18 | |
19 |
# Environment variable |
|
update Mojolicious to 4.57
|
20 |
return $self->parts([splitdir(abs_path $ENV{MOJO_HOME})]) if $ENV{MOJO_HOME}; |
copy gitweblite soruce code
|
21 | |
22 |
# Try to find home from lib directory |
|
upgraded Mojolicious to v3.7...
|
23 |
if (my $class = @_ ? shift : 'Mojo::HelloWorld') { |
24 |
my $file = class_to_path $class; |
|
copy gitweblite soruce code
|
25 |
if (my $path = $INC{$file}) { |
26 |
$path =~ s/$file$//; |
|
27 |
my @home = splitdir $path; |
|
28 | ||
29 |
# Remove "lib" and "blib" |
|
upgraded Mojolicious to v3.7...
|
30 |
pop @home while @home && ($home[-1] =~ /^b?lib$/ || $home[-1] eq ''); |
copy gitweblite soruce code
|
31 | |
32 |
# Turn into absolute path |
|
update Mojolicious to 4.57
|
33 |
return $self->parts([splitdir(abs_path(catdir(@home) || '.'))]); |
copy gitweblite soruce code
|
34 |
} |
35 |
} |
|
36 | ||
37 |
# FindBin fallback |
|
update Mojolicious to 4.57
|
38 |
return $self->parts([split /\//, $FindBin::Bin]); |
copy gitweblite soruce code
|
39 |
} |
40 | ||
41 |
sub lib_dir { |
|
update Mojolicious to 4.57
|
42 |
my $path = catdir @{shift->parts}, 'lib'; |
copy gitweblite soruce code
|
43 |
return -d $path ? $path : undef; |
44 |
} |
|
45 | ||
46 |
sub list_files { |
|
47 |
my ($self, $dir) = @_; |
|
48 | ||
update Mojolicious to 4.57
|
49 |
$dir = catdir @{$self->parts}, split '/', (defined $dir ? $dir : ''); |
copy gitweblite soruce code
|
50 |
return [] unless -d $dir; |
51 |
my @files; |
|
52 |
find { |
|
53 |
wanted => sub { |
|
54 |
my @parts = splitdir(abs2rel($File::Find::name, $dir)); |
|
upgraded Mojolicious to v3.7...
|
55 |
push @files, join '/', @parts unless grep {/^\./} @parts; |
copy gitweblite soruce code
|
56 |
}, |
57 |
no_chdir => 1 |
|
58 |
}, $dir; |
|
59 | ||
60 |
return [sort @files]; |
|
61 |
} |
|
62 | ||
63 |
sub mojo_lib_dir { catdir(dirname(__FILE__), '..') } |
|
64 | ||
65 |
sub parse { |
|
66 |
my ($self, $path) = @_; |
|
update Mojolicious to 4.57
|
67 |
return defined $path ? $self->parts([splitdir $path]) : $self; |
copy gitweblite soruce code
|
68 |
} |
69 | ||
update Mojolicious to 4.57
|
70 |
sub rel_dir { catdir(@{shift->parts}, split '/', shift) } |
71 |
sub rel_file { catfile(@{shift->parts}, split '/', shift) } |
|
copy gitweblite soruce code
|
72 | |
update Mojolicious to 4.57
|
73 |
sub to_string { catdir(@{shift->parts}) } |
copy gitweblite soruce code
|
74 | |
75 |
1; |
|
76 | ||
update Mojolicious to 4.57
|
77 |
=encoding utf8 |
78 | ||
copy gitweblite soruce code
|
79 |
=head1 NAME |
80 | ||
upgraded Mojolicious to v3.7...
|
81 |
Mojo::Home - Home sweet home! |
copy gitweblite soruce code
|
82 | |
83 |
=head1 SYNOPSIS |
|
84 | ||
85 |
use Mojo::Home; |
|
86 | ||
upgraded Mojolicious to v3.7...
|
87 |
# Find and manage the project root directory |
copy gitweblite soruce code
|
88 |
my $home = Mojo::Home->new; |
89 |
$home->detect; |
|
upgraded Mojolicious to v3.7...
|
90 |
say $home->lib_dir; |
91 |
say $home->rel_file('templates/layouts/default.html.ep'); |
|
92 |
say "$home"; |
|
copy gitweblite soruce code
|
93 | |
94 |
=head1 DESCRIPTION |
|
95 | ||
96 |
L<Mojo::Home> is a container for home directories. |
|
97 | ||
update Mojolicious to 4.57
|
98 |
=head1 ATTRIBUTES |
99 | ||
100 |
L<Mojo::Home> implements the following attributes. |
|
101 | ||
102 |
=head2 parts |
|
103 | ||
104 |
my $parts = $home->parts; |
|
105 |
$home = $home->parts([]); |
|
106 | ||
107 |
Home directory parts. |
|
108 | ||
copy gitweblite soruce code
|
109 |
=head1 METHODS |
110 | ||
111 |
L<Mojo::Home> inherits all methods from L<Mojo::Base> and implements the |
|
112 |
following new ones. |
|
113 | ||
update Mojolicious and added...
|
114 |
=head2 new |
copy gitweblite soruce code
|
115 | |
116 |
my $home = Mojo::Home->new; |
|
upgraded Mojolicious to v3.7...
|
117 |
my $home = Mojo::Home->new('/home/sri/myapp'); |
copy gitweblite soruce code
|
118 | |
update Mojolicious to 4.57
|
119 |
Construct a new L<Mojo::Home> object and L</"parse"> home directory if |
120 |
necessary. |
|
copy gitweblite soruce code
|
121 | |
update Mojolicious and added...
|
122 |
=head2 detect |
copy gitweblite soruce code
|
123 | |
124 |
$home = $home->detect; |
|
125 |
$home = $home->detect('My::App'); |
|
126 | ||
update Mojolicious 4.07
|
127 |
Detect home directory from the value of the MOJO_HOME environment variable or |
128 |
application class. |
|
copy gitweblite soruce code
|
129 | |
update Mojolicious and added...
|
130 |
=head2 lib_dir |
copy gitweblite soruce code
|
131 | |
132 |
my $path = $home->lib_dir; |
|
133 | ||
134 |
Path to C<lib> directory of application. |
|
135 | ||
update Mojolicious and added...
|
136 |
=head2 list_files |
copy gitweblite soruce code
|
137 | |
138 |
my $files = $home->list_files; |
|
139 |
my $files = $home->list_files('foo/bar'); |
|
140 | ||
upgraded Mojolicious to v3.7...
|
141 |
Portably list all files recursively in directory relative to the home |
update Mojolicious 4.07
|
142 |
directory. |
copy gitweblite soruce code
|
143 | |
update Mojolicious 4.07
|
144 |
say $home->rel_file($home->list_files('templates/layouts')->[1]); |
copy gitweblite soruce code
|
145 | |
update Mojolicious and added...
|
146 |
=head2 mojo_lib_dir |
copy gitweblite soruce code
|
147 | |
148 |
my $path = $home->mojo_lib_dir; |
|
149 | ||
150 |
Path to C<lib> directory in which L<Mojolicious> is installed. |
|
151 | ||
update Mojolicious and added...
|
152 |
=head2 parse |
copy gitweblite soruce code
|
153 | |
upgraded Mojolicious to v3.7...
|
154 |
$home = $home->parse('/home/sri/myapp'); |
copy gitweblite soruce code
|
155 | |
156 |
Parse home directory. |
|
157 | ||
update Mojolicious and added...
|
158 |
=head2 rel_dir |
copy gitweblite soruce code
|
159 | |
160 |
my $path = $home->rel_dir('foo/bar'); |
|
161 | ||
upgraded Mojolicious to v3.7...
|
162 |
Portably generate an absolute path for a directory relative to the home |
163 |
directory. |
|
copy gitweblite soruce code
|
164 | |
update Mojolicious and added...
|
165 |
=head2 rel_file |
copy gitweblite soruce code
|
166 | |
167 |
my $path = $home->rel_file('foo/bar.html'); |
|
168 | ||
upgraded Mojolicious to v3.7...
|
169 |
Portably generate an absolute path for a file relative to the home directory. |
copy gitweblite soruce code
|
170 | |
update Mojolicious and added...
|
171 |
=head2 to_string |
copy gitweblite soruce code
|
172 | |
update Mojolicious 4.07
|
173 |
my $str = $home->to_string; |
174 |
my $str = "$home"; |
|
copy gitweblite soruce code
|
175 | |
176 |
Home directory. |
|
177 | ||
178 |
=head1 SEE ALSO |
|
179 | ||
180 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
181 | ||
182 |
=cut |