1 | package Mojo::Home;
|
---|
2 | use Mojo::Base 'Mojo::File';
|
---|
3 |
|
---|
4 | use Mojo::Util 'class_to_path';
|
---|
5 |
|
---|
6 | sub detect {
|
---|
7 | my ($self, $class) = @_;
|
---|
8 |
|
---|
9 | # Environment variable
|
---|
10 | my $home;
|
---|
11 | if ($ENV{MOJO_HOME}) { $home = Mojo::File->new($ENV{MOJO_HOME})->to_array }
|
---|
12 |
|
---|
13 | # Location of the application class (Windows mixes backslash and slash)
|
---|
14 | elsif ($class && (my $path = $INC{my $file = class_to_path $class})) {
|
---|
15 | $home = Mojo::File->new($path)->to_array;
|
---|
16 | splice @$home, (my @dummy = split('/', $file)) * -1;
|
---|
17 | @$home && $home->[-1] eq $_ && pop @$home for qw(lib blib);
|
---|
18 | }
|
---|
19 |
|
---|
20 | $$self = Mojo::File->new(@$home)->to_abs->to_string if $home;
|
---|
21 | return $self;
|
---|
22 | }
|
---|
23 |
|
---|
24 | sub mojo_lib_dir { shift->new(__FILE__)->sibling('..') }
|
---|
25 |
|
---|
26 | sub rel_file { shift->child(split('/', shift)) }
|
---|
27 |
|
---|
28 | 1;
|
---|
29 |
|
---|
30 | =encoding utf8
|
---|
31 |
|
---|
32 | =head1 NAME
|
---|
33 |
|
---|
34 | Mojo::Home - Home sweet home
|
---|
35 |
|
---|
36 | =head1 SYNOPSIS
|
---|
37 |
|
---|
38 | use Mojo::Home;
|
---|
39 |
|
---|
40 | # Find and manage the project root directory
|
---|
41 | my $home = Mojo::Home->new;
|
---|
42 | $home->detect;
|
---|
43 | say $home->child('templates', 'layouts', 'default.html.ep');
|
---|
44 | say "$home";
|
---|
45 |
|
---|
46 | =head1 DESCRIPTION
|
---|
47 |
|
---|
48 | L<Mojo::Home> is a container for home directories based on L<Mojo::File>.
|
---|
49 |
|
---|
50 | =head1 METHODS
|
---|
51 |
|
---|
52 | L<Mojo::Home> inherits all methods from L<Mojo::File> and implements the
|
---|
53 | following new ones.
|
---|
54 |
|
---|
55 | =head2 detect
|
---|
56 |
|
---|
57 | $home = $home->detect;
|
---|
58 | $home = $home->detect('My::App');
|
---|
59 |
|
---|
60 | Detect home directory from the value of the C<MOJO_HOME> environment variable or
|
---|
61 | the location of the application class.
|
---|
62 |
|
---|
63 | =head2 mojo_lib_dir
|
---|
64 |
|
---|
65 | my $path = $home->mojo_lib_dir;
|
---|
66 |
|
---|
67 | Path to C<lib> directory in which L<Mojolicious> is installed as a L<Mojo::Home>
|
---|
68 | object.
|
---|
69 |
|
---|
70 | =head2 rel_file
|
---|
71 |
|
---|
72 | my $path = $home->rel_file('foo/bar.html');
|
---|
73 |
|
---|
74 | Return a new L<Mojo::Home> object relative to the home directory.
|
---|
75 |
|
---|
76 | =head1 OPERATORS
|
---|
77 |
|
---|
78 | L<Mojo::Home> inherits all overloaded operators from L<Mojo::File>.
|
---|
79 |
|
---|
80 | =head1 SEE ALSO
|
---|
81 |
|
---|
82 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
83 |
|
---|
84 | =cut
|
---|