Skip to content

Commit

Permalink
fixed FindBin support in Mojolicious applications
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 11, 2012
1 parent 7baf988 commit b9a80d8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 56 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
3.40 2012-09-11
- Improved tests.
- Fixed Perl 5.10.1 compatibility.
- Fixed FindBin support in Mojolicious applications.

3.39 2012-09-10
- Improved Mojo::URL and Mojo::Parameters performance.
Expand Down
35 changes: 21 additions & 14 deletions lib/Mojo/Server.pm
Expand Up @@ -2,6 +2,7 @@ package Mojo::Server;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use FindBin;
use Mojo::Loader;
use Mojo::Util 'md5_sum';
use Scalar::Util 'blessed';
Expand All @@ -27,21 +28,27 @@ sub build_tx { shift->app->build_tx }
sub load_app {
my ($self, $path) = @_;

# Clean up environment
local $ENV{MOJO_APP_LOADER} = 1;
local $ENV{MOJO_EXE};

# Try to load application from script into sandbox
my $app = eval <<EOF;
package Mojo::Server::SandBox::@{[md5_sum($path . $$)]};
my \$app = do \$path;
if (!\$app && (my \$e = \$@ || \$!)) { die \$e }
\$app;
# Clean environment
{
local $0 = $path;
FindBin::again();
local $ENV{MOJO_APP_LOADER} = 1;
local $ENV{MOJO_EXE};

# Try to load application from script into sandbox
$self->app(my $app = eval sprintf <<'EOF', md5_sum($path . $$));
package Mojo::Server::SandBox::%s;
my $app = do $path;
if (!$app && (my $e = $@ || $!)) { die $e }
$app;
EOF
die qq{Couldn't load application from file "$path": $@} if !$app && $@;
die qq{File "$path" did not return an application object.\n}
unless blessed $app && $app->isa('Mojo');
return $self->app($app)->app;
die qq{Couldn't load application from file "$path": $@} if !$app && $@;
die qq{File "$path" did not return an application object.\n}
unless blessed $app && $app->isa('Mojo');
};
FindBin::again();

return $self->app;
}

sub run { croak 'Method "run" not implemented by subclass' }
Expand Down
11 changes: 4 additions & 7 deletions lib/Mojolicious/Command/generate/app.pm
Expand Up @@ -52,15 +52,12 @@ __DATA__
@@ mojo
% my $class = shift;
#!/usr/bin/env perl
use Mojo::Base -strict;
use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);
use strict;
use warnings;
# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use FindBin;
use lib "$FindBin::Bin/../lib";
# Start commands for application
require Mojolicious::Commands;
Expand Down
9 changes: 2 additions & 7 deletions lib/Mojolicious/Guides/Growing.pod
Expand Up @@ -652,13 +652,8 @@ environments.
use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);

# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use FindBin;
use lib "$FindBin::Bin/../lib";

# Start commands for application
require Mojolicious::Commands;
Expand Down
10 changes: 3 additions & 7 deletions script/hypnotoad
Expand Up @@ -3,14 +3,10 @@
use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use FindBin;
use lib "$FindBin::Bin/../lib";

# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);

GetOptions(
'f|foreground' => sub { $ENV{HYPNOTOAD_FOREGROUND} = 1 },
Expand Down
9 changes: 2 additions & 7 deletions script/mojo
Expand Up @@ -3,13 +3,8 @@
use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);

# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use FindBin;
use lib "$FindBin::Bin/../lib";

require Mojolicious::Commands;
Mojolicious::Commands->start;
Expand Down
10 changes: 3 additions & 7 deletions script/morbo
Expand Up @@ -3,14 +3,10 @@
use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use FindBin;
use lib "$FindBin::Bin/../lib";

# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);

GetOptions(
'h|help' => \my $help,
Expand Down
9 changes: 2 additions & 7 deletions t/mojolicious/external/script/my_app
Expand Up @@ -3,13 +3,8 @@
use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec::Functions qw(catdir splitdir);

# Source directory has precedence
my @base = (splitdir(dirname(__FILE__)), '..');
my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);
use FindBin;
use lib "$FindBin::Bin/../lib";

# Start commands for application
require Mojolicious::Commands;
Expand Down

0 comments on commit b9a80d8

Please sign in to comment.