Skip to content

Commit

Permalink
improved a few recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 13, 2012
1 parent 68fb541 commit 6f52bdc
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 170 deletions.
27 changes: 9 additions & 18 deletions lib/Mojo/Command.pm
@@ -1,12 +1,11 @@
package Mojo::Command;
use Mojo::Base -base;

require Cwd;
require File::Path;
require File::Spec;
require IO::File;

use Carp 'croak';
use Cwd 'getcwd';
use File::Path 'mkpath';
use File::Spec::Functions qw/catdir catfile splitdir/;
use IO::File;
use Mojo::Server;
use Mojo::Template;
use Mojo::Loader;
Expand Down Expand Up @@ -70,7 +69,7 @@ sub create_dir {
}
# Create
File::Path::mkpath($path) or croak qq/Can't make directory "$path": $!/;
mkpath $path or croak qq/Can't make directory "$path": $!/;
say " [mkdir] $path" unless $self->quiet;
return $self;
}
Expand Down Expand Up @@ -142,17 +141,9 @@ sub help {
exit 0;
}

sub rel_dir {
my ($self, $path) = @_;
my @parts = split /\//, $path;
return File::Spec->catdir(Cwd::getcwd(), @parts);
}
sub rel_dir { catdir(getcwd(), split /\//, pop) }

sub rel_file {
my ($self, $path) = @_;
my @parts = split /\//, $path;
return File::Spec->catfile(Cwd::getcwd(), @parts);
}
sub rel_file { catfile(getcwd(), split /\//, pop) }

sub render_data {
my $self = shift;
Expand Down Expand Up @@ -259,9 +250,9 @@ sub write_file {
my ($self, $path, $data) = @_;

# Directory
my @parts = File::Spec->splitdir($path);
my @parts = splitdir $path;
pop @parts;
my $dir = File::Spec->catdir(@parts);
my $dir = catdir @parts;
$self->create_dir($dir);

# Write unbuffered
Expand Down
41 changes: 13 additions & 28 deletions lib/Mojo/Home.pm
Expand Up @@ -8,7 +8,7 @@ use overload
use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Find 'find';
use File::Spec;
use File::Spec::Functions qw/abs2rel catdir catfile splitdir/;
use FindBin;
use List::Util 'first';
use Mojo::Asset::File;
Expand All @@ -30,8 +30,7 @@ sub detect {

# Environment variable
if ($ENV{MOJO_HOME}) {
my @parts = File::Spec->splitdir(abs_path $ENV{MOJO_HOME});
$self->{parts} = \@parts;
$self->{parts} = [splitdir(abs_path $ENV{MOJO_HOME})];
return $self;
}

Expand All @@ -49,7 +48,7 @@ sub detect {

# Directory
$path =~ s/$file$//;
my @home = File::Spec->splitdir($path);
my @home = splitdir $path;

# Remove "lib" and "blib"
while (@home) {
Expand All @@ -58,8 +57,7 @@ sub detect {
}

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

Expand All @@ -74,7 +72,7 @@ sub lib_dir {

# Directory found
my $parts = $self->{parts} || [];
my $path = File::Spec->catdir(@$parts, 'lib');
my $path = catdir @$parts, 'lib';
return $path if -d $path;

# No lib directory
Expand All @@ -86,14 +84,13 @@ sub list_files {

# Files relative to directory
my $parts = $self->{parts} || [];
my $root = File::Spec->catdir(@$parts);
$dir = File::Spec->catdir($root, split '/', ($dir || ''));
my $root = catdir @$parts;
$dir = catdir $root, split '/', ($dir || '');
return [] unless -d $dir;
my @files;
find {
wanted => sub {
my @parts =
File::Spec->splitdir(File::Spec->abs2rel($File::Find::name, $dir));
my @parts = splitdir(abs2rel($File::Find::name, $dir));
push @files, join '/', @parts unless first {/^\./} @parts;
},
no_chdir => 1
Expand All @@ -102,39 +99,27 @@ sub list_files {
return [sort @files];
}

sub mojo_lib_dir { File::Spec->catdir(dirname(__FILE__), '..') }
sub mojo_lib_dir { catdir(dirname(__FILE__), '..') }

# "Don't worry, son.
# I'm sure he's up in heaven right now laughing it up with all the other
# celebrities: John Dilinger, Ty Cobb, Joseph Stalin."
sub parse {
my ($self, $path) = @_;
return $self unless defined $path;
$self->{parts} = [File::Spec->splitdir($path)];
$self->{parts} = [splitdir $path];
return $self;
}

sub rel_dir {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catdir(@$parts, split '/', shift);
}
sub rel_dir { catdir(@{shift->{parts} || []}, split '/', shift) }

sub rel_file {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catfile(@$parts, split '/', shift);
}
sub rel_file { catfile(@{shift->{parts} || []}, split '/', shift) }

sub slurp_rel_file {
Mojo::Asset::File->new(path => shift->rel_file(@_))->slurp;
}

sub to_string {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catdir(@$parts);
}
sub to_string { catdir(@{shift->{parts} || []}) }

1;
__END__
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::IOLoop::Server;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use File::Spec;
use File::Spec::Functions qw/catfile tmpdir/;
use IO::File;
use IO::Socket::INET;
use Scalar::Util 'weaken';
Expand Down Expand Up @@ -206,8 +206,7 @@ sub _cert_file {
return $cert if $cert && -r $cert;

# Create temporary TLS cert file
$cert = File::Spec->catfile($ENV{MOJO_TMPDIR} || File::Spec->tmpdir,
"mojocert-$$.pem");
$cert = catfile $ENV{MOJO_TMPDIR} || tmpdir, "mojocert-$$.pem";
croak qq/Can't create temporary TLS cert file "$cert"/
unless my $file = IO::File->new("> $cert");
print $file CERT;
Expand All @@ -223,8 +222,7 @@ sub _key_file {
return $key if $key && -r $key;

# Create temporary TLS key file
$key = File::Spec->catfile($ENV{MOJO_TMPDIR} || File::Spec->tmpdir,
"mojokey-$$.pem");
$key = catfile $ENV{MOJO_TMPDIR} || tmpdir, "mojokey-$$.pem";
croak qq/Can't create temporary TLS key file "$key"/
unless my $file = IO::File->new("> $key");
print $file KEY;
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/Loader.pm
Expand Up @@ -4,8 +4,8 @@ use Mojo::Base -base;
# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
# Why, you could wake up dead tomorrow! Well, good night."
use File::Basename;
use File::Spec;
use File::Basename 'fileparse';
use File::Spec::Functions qw/catdir catfile splitdir/;
use Mojo::Command;
use Mojo::Exception;

Expand Down Expand Up @@ -42,7 +42,7 @@ sub search {
my $modules = [];
my %found;
foreach my $directory (exists $INC{'blib.pm'} ? grep {/blib/} @INC : @INC) {
my $path = File::Spec->catdir($directory, (split /::/, $namespace));
my $path = catdir $directory, (split /::/, $namespace);
next unless (-e $path && -d $path);

# Get files
Expand All @@ -52,10 +52,10 @@ sub search {

# Check files
for my $file (@files) {
next if -d File::Spec->catfile(File::Spec->splitdir($path), $file);
next if -d catfile splitdir($path), $file;

# Module found
my $name = File::Basename::fileparse($file, qr/\.pm/);
my $name = fileparse $file, qr/\.pm/;
my $class = "$namespace\::$name";
push @$modules, $class unless $found{$class};
$found{$class} ||= 1;
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -5,7 +5,7 @@ use Carp 'croak';
use Cwd 'abs_path';
use Fcntl ':flock';
use File::Basename 'dirname';
use File::Spec;
use File::Spec::Functions qw/catfile tmpdir/;
use IO::File;
use IO::Poll 'POLLIN';
use List::Util 'shuffle';
Expand Down Expand Up @@ -147,13 +147,10 @@ sub _config {
$c->{graceful_timeout} ||= 30;
$c->{heartbeat_interval} ||= 5;
$c->{heartbeat_timeout} ||= 10;
$c->{lock_file}
||= File::Spec->catfile($ENV{MOJO_TMPDIR} || File::Spec->tmpdir,
'hypnotoad.lock');
$c->{lock_file} ||= catfile($ENV{MOJO_TMPDIR} || tmpdir, 'hypnotoad.lock');
$c->{lock_file} .= ".$$";
$c->{lock_timeout} ||= '0.5';
$c->{pid_file}
||= File::Spec->catfile(dirname($ENV{HYPNOTOAD_APP}), 'hypnotoad.pid');
$c->{pid_file} ||= catfile(dirname($ENV{HYPNOTOAD_APP}), 'hypnotoad.pid');
$c->{upgrade_timeout} ||= 60;
$c->{workers} ||= 4;

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/generate/app.pm
Expand Up @@ -54,9 +54,9 @@ __DATA__
use Mojo::Base -strict;
use File::Basename 'dirname';
use File::Spec;
use File::Spec::Functions 'splitdir';
push @INC, join('/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib');
push @INC, join('/', splitdir(dirname(__FILE__)), '..', 'lib');
# Check if Mojolicious is installed;
die <<EOF unless eval 'use Mojolicious::Commands; 1';
Expand Down
38 changes: 12 additions & 26 deletions lib/Mojolicious/Command/test.pm
@@ -1,10 +1,11 @@
package Mojolicious::Command::test;
use Mojo::Base 'Mojo::Command';

use Cwd;
use Cwd 'realpath';
use FindBin;
use File::Spec;
use File::Spec::Functions qw/abs2rel catdir splitdir/;
use Getopt::Long qw/GetOptions :config no_auto_abbrev no_ignore_case/;
use Mojo::Home;

has description => "Run unit tests.\n";
has usage => <<"EOF";
Expand All @@ -26,36 +27,21 @@ sub run {

# Search tests
unless (@tests) {
my @base = File::Spec->splitdir(File::Spec->abs2rel($FindBin::Bin));
my @base = splitdir(abs2rel $FindBin::Bin);

# Test directory in the same directory as "mojo" (t)
my $path = File::Spec->catdir(@base, 't');
my $path = catdir @base, 't';

# Test dirctory in the directory above "mojo" (../t)
$path = File::Spec->catdir(@base, '..', 't') unless -d $path;
unless (-d $path) {
say "Can't find test directory.";
return;
}
$path = catdir @base, '..', 't' unless -d $path;
return say "Can't find test directory." unless -d $path;

# List test files
my @dirs = ($path);
while (my $dir = shift @dirs) {
opendir(my $fh, $dir);
for my $file (readdir($fh)) {
next if $file eq '.';
next if $file eq '..';
my $fpath = File::Spec->catfile($dir, $file);
push @dirs, File::Spec->catdir($dir, $file) if -d $fpath;
push @tests,
File::Spec->abs2rel(
Cwd::realpath(File::Spec->catfile(File::Spec->splitdir($fpath))))
if (-f $fpath) && ($fpath =~ /\.t$/);
}
closedir $fh;
}

$path = Cwd::realpath($path);
my $home = Mojo::Home->new($path);
$_ =~ /\.t$/ and push(@tests, $home->rel_file($_))
for @{$home->list_files};

$path = realpath $path;
say "Running tests from '$path'.";
}

Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1004,7 +1004,7 @@ get automatically installed with the modules.
use Mojo::Base 'Mojolicious';

use File::Basename 'dirname';
use File::Spec;
use File::Spec::Functions 'catdir';

# Every CPAN module needs a version
our $VERSION = '1.0';
Expand All @@ -1013,8 +1013,7 @@ get automatically installed with the modules.
my $self = shift;

# Switch to installable home directory
$self->home->parse(
File::Spec->catdir(dirname(__FILE__), 'MyMojoliciousApp'));
$self->home->parse(catdir(dirname(__FILE__), 'MyMojoliciousApp'));

# Switch to installable "public" directory
$self->static->paths->[0] = $self->home->rel_dir('public');
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Growing.pod
Expand Up @@ -654,9 +654,9 @@ environments.
use warnings;

use File::Basename 'dirname';
use File::Spec;
use File::Spec::Functions 'splitdir';

push @INC, join('/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib');
push @INC, join('/', splitdir(dirname(__FILE__)), '..', 'lib');

# Check if Mojolicious is installed
die <<EOF unless eval 'use Mojolicious::Commands; 1';
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -636,16 +636,15 @@ C<register> is called.
use Mojo::Base 'Mojolicious::Plugin';

use File::Basename 'dirname';
use File::Spec;
use Mojo::Home;
use File::Spec::Functions 'catdir';

sub register {
my ($self, $app) = @_;

# Append "templates" and "public" directories
my $home = Mojo::Home->new(File::Spec->catdir(dirname(__FILE__)));
push @{$app->renderer->paths}, $home->rel_dir('AlertAssets/templates');
push @{$app->static->paths}, $home->rel_dir('AlertAssets/public');
my $home = catdir(dirname(__FILE__), 'AlertAssets');
push @{$app->renderer->paths}, catdir($home, 'templates');
push @{$app->static->paths}, catdir($home, 'public');
}

1;
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious';
# "Since when is the Internet all about robbing people of their privacy?
# August 6, 1991."
use File::Basename 'dirname';
use File::Spec;
use File::Spec::Functions 'catdir';

# "It's the future, my parents, my co-workers, my girlfriend,
# I'll never see any of them ever again... YAHOOO!"
Expand All @@ -15,8 +15,7 @@ sub import {
$ENV{MOJO_EXE} ||= (caller)[1];

# Home
local $ENV{MOJO_HOME} =
File::Spec->catdir(split '/', dirname($ENV{MOJO_EXE}))
local $ENV{MOJO_HOME} = catdir(split '/', dirname($ENV{MOJO_EXE}))
unless $ENV{MOJO_HOME};

# Initialize app
Expand Down

0 comments on commit 6f52bdc

Please sign in to comment.