Created
May 21, 2012 08:45
-
-
Save aanoaa/2761233 to your computer and use it in GitHub Desktop.
project initialize tool
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use DBI; | |
use Pod::Usage; | |
use File::Slurp; | |
use Getopt::Long; | |
use Config::ZOMG; | |
use lib qw/lib/; | |
my %options; | |
GetOptions(\%options, "--help", "--db-init"); | |
run(\%options, @ARGV); | |
sub run { | |
my ($opts, @args) = @_; | |
pod2usage(0, -noperldoc => 1) if $opts->{help}; | |
if ($opts->{'db-init'}) { | |
init_db(); | |
exit(0); | |
} | |
eval "require Carton::CLI; 1"; | |
die "Carton not found. Install it with this command `cpanm Carton`" if $@; | |
my $checksum = `cksum Makefile.PL`; | |
$checksum =~ s/ .*$//s; | |
my $installed = -e '.carton/checksum' ? read_file('.carton/checksum') : 0; | |
if ($checksum == $installed) { | |
print "up-to-date.\n" | |
} else { | |
Carton::CLI->new->run('install'); | |
write_file('.carton/checksum', $checksum); | |
} | |
} | |
sub init_db { | |
my $config = Config::ZOMG->new( | |
path => 'evid_web.pl' | |
)->load; | |
my $dbconn = $config->{'Model::API'}{args}{connect_info}; | |
my $dbh = DBI->connect($dbconn->{dsn}, $dbconn->{user}, $dbconn->{password}); | |
$dbh->do("SET NAMES utf8"); | |
print ".. rebuilding schema\n"; | |
query($dbh, 'db/schema.sql'); | |
print ".. importing default data\n"; | |
query($dbh, 'db/data.sql'); | |
$dbh->disconnect; | |
print ".. done\n"; | |
} | |
sub query { | |
my ($dbh, $file) = @_; | |
return unless $dbh; | |
return unless -e $file; | |
my $sql = read_file($file); | |
for my $query (split /;/, $sql) { | |
$query =~ s/(^\s*|\s*$)//s; | |
next unless $query; | |
$dbh->do($query); | |
} | |
} | |
__END__ | |
=pod | |
=encoding utf-8 | |
=head1 NAME | |
bootstrap.pl - simply setup to work | |
=head1 SYNOPSIS | |
bootstrap.pl [<options>] | |
$ script/bootstrap.pl | |
$ script/bootstrap.pl --db-init # remake all tables and import init data | |
=head1 LICENSE | |
same as Perl. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment