-
-
Save xantus/adb15176e41d02ca4145 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AutoMojo; | |
# Auto install and Mojolicious | |
# (c) 2010 - David Davis - http://xant.us/ | |
# usage: | |
# | |
# use AutoMojo qw( Mojolicious::Lite ); | |
use strict; | |
use warnings; | |
use Carp; | |
use FindBin; | |
BEGIN { | |
my $mojodir = "$FindBin::Bin/mojo-lib"; | |
eval "use lib \"$mojodir/lib\""; | |
eval 'use Mojolicious'; | |
if ( $@ || !-d $mojodir) { | |
if ( -d $mojodir ) { | |
# already downloaded? | |
die "couldn't find mojo, but it's downloaded in $mojodir, giving up\n"; | |
} else { | |
warn "Mojolicious not found, installing in $mojodir\n"; | |
mkdir( $mojodir ); | |
unless ( -d $mojodir ) { | |
die "need permission to create $mojodir, fix your perms\n"; | |
} | |
if ( `tar --version` =~ m/GNU tar/ ) { | |
if ( `wget` =~ m/missing URL/ ) { | |
`wget -q http://github.com/kraih/mojo/tarball/master -O $mojodir/mojo.tar.gz`; | |
} elsif ( `curl -- version` =~ m/libcurl/ ) { | |
`curl -L http://github.com/kraih/mojo/tarball/master >$mojodir/mojo.tar.gz`; | |
} else { | |
die "Can't get mojo, wget or curl are not installed\n"; | |
} | |
`cd $mojodir; tar zxf mojo.tar.gz`; | |
unlink( "$mojodir/mojo.tar.gz" ); | |
} elsif ( `unzip` =~ m/UnZip/ ) { | |
if ( `wget` =~ m/missing URL/ ) { | |
`wget -q http://github.com/kraih/mojo/zipball/master -O $mojodir/mojo.zip`; | |
} elsif ( `curl -- version` =~ m/libcurl/ ) { | |
`curl -L http://github.com/kraih/mojo/zipball/master >$mojodir/mojo.zip`; | |
} else { | |
die "Can't get mojo, wget or curl are not installed\n"; | |
} | |
`cd $mojodir; unzip mojo.zip`; | |
unlink( "$mojodir/mojo.zip" ); | |
} else { | |
die "Can't get mojo, tar and unzip are not installed\n"; | |
} | |
my $dir = glob "$mojodir/kraih-mojo-*"; | |
unless ( $dir && -d $dir ) { | |
die "failed to download mojo\n"; | |
} | |
`mv $dir/* $mojodir/`; | |
unlink( $dir ); | |
eval 'use Mojolicious'; | |
if ( $@ ) { | |
die "Mojolicious still not found, AutoMojo failed.\n"; | |
} | |
} | |
} | |
} | |
sub import { | |
shift; | |
my @modules = @_; | |
unshift( @modules, 'Mojolicious' ); | |
my $package = caller(); | |
my @failed; | |
foreach my $module ( @modules ) { | |
my $code = "package $package; use $module;"; | |
eval( $code ); | |
if ( $@ ) { | |
warn $@; | |
push( @failed, $module ); | |
} | |
} | |
@failed and croak 'could not import (' . join( ' ', @failed ) . ')'; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment