Skip to content

Commit

Permalink
fixed home detection bug in Mojo (closes #559)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 6, 2013
1 parent 6a9315d commit 8a5c06c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@

4.54 2013-11-02
4.54 2013-11-06
- Added parts attribute to Mojo::Home.
- Fixed support for links within a page in Mojolicious::Plugin::PODRenderer.
- Fixed home detection bug in Mojo.

4.53 2013-10-30
- Fixed a few unsubscribe and error event bugs in Mojo::EventEmitter.
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojo.pm
Expand Up @@ -27,7 +27,8 @@ sub new {
my $self = shift->SUPER::new(@_);

# Check if we have a log directory
my $home = $self->home->detect(ref $self);
my $home = $self->home;
$home->detect(ref $self) unless @{$home->parts};
$self->log->path($home->rel_file('log/mojo.log'))
if -w $home->rel_file('log');

Expand Down Expand Up @@ -144,7 +145,8 @@ new ones.
my $app = Mojo->new;
Construct a new L<Mojo> application. Will automatically detect your home
directory and set up logging to C<log/mojo.log> if there's a C<log> directory.
directory if necessary and set up logging to C<log/mojo.log> if there's a
C<log> directory.
=head2 build_tx
Expand Down
37 changes: 22 additions & 15 deletions lib/Mojo/Home.pm
Expand Up @@ -9,16 +9,15 @@ use File::Spec::Functions qw(abs2rel catdir catfile splitdir);
use FindBin;
use Mojo::Util qw(class_to_path slurp);

has parts => sub { [] };

sub new { shift->SUPER::new->parse(@_) }

sub detect {
my $self = shift;

# Environment variable
if ($ENV{MOJO_HOME}) {
$self->{parts} = [splitdir(abs_path $ENV{MOJO_HOME})];
return $self;
}
return $self->parts([splitdir(abs_path $ENV{MOJO_HOME})]) if $ENV{MOJO_HOME};

# Try to find home from lib directory
if (my $class = @_ ? shift : 'Mojo::HelloWorld') {
Expand All @@ -31,25 +30,23 @@ sub detect {
pop @home while @home && ($home[-1] =~ /^b?lib$/ || $home[-1] eq '');

# Turn into absolute path
$self->{parts} = [splitdir(abs_path(catdir(@home) || '.'))];
return $self->parts([splitdir(abs_path(catdir(@home) || '.'))]);
}
}

# FindBin fallback
$self->{parts} = [split /\//, $FindBin::Bin] unless $self->{parts};

return $self;
return $self->parts([split /\//, $FindBin::Bin]);
}

sub lib_dir {
my $path = catdir @{shift->{parts} || []}, 'lib';
my $path = catdir @{shift->parts}, 'lib';
return -d $path ? $path : undef;
}

sub list_files {
my ($self, $dir) = @_;

$dir = catdir @{$self->{parts} || []}, split '/', ($dir // '');
$dir = catdir @{$self->parts}, split '/', ($dir // '');
return [] unless -d $dir;
my @files;
find {
Expand All @@ -67,14 +64,13 @@ sub mojo_lib_dir { catdir(dirname(__FILE__), '..') }

sub parse {
my ($self, $path) = @_;
$self->{parts} = [splitdir $path] if defined $path;
return $self;
return defined $path ? $self->parts([splitdir $path]) : $self;
}

sub rel_dir { catdir(@{shift->{parts} || []}, split '/', shift) }
sub rel_file { catfile(@{shift->{parts} || []}, split '/', shift) }
sub rel_dir { catdir(@{shift->parts}, split '/', shift) }
sub rel_file { catfile(@{shift->parts}, split '/', shift) }

sub to_string { catdir(@{shift->{parts} || []}) }
sub to_string { catdir(@{shift->parts}) }

1;

Expand All @@ -99,6 +95,17 @@ Mojo::Home - Home sweet home!
L<Mojo::Home> is a container for home directories.
=head1 ATTRIBUTES
L<Mojo::Home> implements the following attributes.
=head2 parts
my $parts = $home->parts;
$home = $home->parts([]);
Home directory parts.
=head1 METHODS
L<Mojo::Home> inherits all methods from L<Mojo::Base> and implements the
Expand Down
7 changes: 3 additions & 4 deletions t/mojo/daemon.t
Expand Up @@ -39,10 +39,9 @@ use Socket qw(SO_REUSEPORT SOL_SOCKET);
);
}

# Logger
my $logger = Mojo::Log->new;
my $app = Mojo->new({log => $logger});
is $app->log, $logger, 'right logger';
# Optional home detection
my $app = Mojo->new(home => Mojo::Home->new('/will/never-ever/exist'));
is $app->home, '/will/never-ever/exist', 'right home directory';

# Config
is $app->config('foo'), undef, 'no value';
Expand Down

0 comments on commit 8a5c06c

Please sign in to comment.