| 1 | package Rapi::Demo::Chinook; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: PSGI version of the RapidApp "Chinook" demo | ||
| 7 | |||
| 8 | use RapidApp 1.0303; | ||
| 9 | |||
| 10 | use Moose; | ||
| 11 | extends 'RapidApp::Builder'; | ||
| 12 | |||
| 13 | use Types::Standard qw(:all); | ||
| 14 | |||
| 15 | use RapidApp::Util ':all'; | ||
| 16 | use File::ShareDir qw(dist_dir); | ||
| 17 | use FindBin; | ||
| 18 | use Path::Class qw(file dir); | ||
| 19 | use Module::Runtime; | ||
| 20 | |||
| 21 | our $VERSION = '1.03'; | ||
| 22 | |||
| 23 | has '+base_appname', default => sub { 'Rapi::Demo::Chinook::App' }; | ||
| 24 | has '+debug', default => sub {1}; | ||
| 25 | |||
| 26 | sub _build_plugins {[ 'RapidApp::RapidDbic' ]} | ||
| 27 | |||
| 28 | has 'share_dir', is => 'ro', isa => Str, lazy => 1, default => sub { | ||
| 29 | my $self = shift; | ||
| 30 | $ENV{RAPI_DEMO_CHINOOK_SHARE_DIR} || ( | ||
| 31 | try{dist_dir('Rapi-Demo-Chinook')} || ( | ||
| 32 | -d "$FindBin::Bin/share" ? "$FindBin::Bin/share" : "$FindBin::Bin/../share" | ||
| 33 | ) | ||
| 34 | ) | ||
| 35 | }; | ||
| 36 | |||
| 37 | has '_init_chinook_db', is => 'ro', isa => Str, lazy => 1, default => sub { | ||
| 38 | my $self = shift; | ||
| 39 | file( $self->share_dir, '_init_chinook.db' )->stringify | ||
| 40 | }, init_arg => undef; | ||
| 41 | |||
| 42 | has 'chinook_db', is => 'ro', isa => Str, lazy => 1, default => sub { | ||
| 43 | my $self = shift; | ||
| 44 | # Default to the cwd | ||
| 45 | file( 'chinook.db' )->stringify | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | has '+inject_components', default => sub { | ||
| 50 | my $self = shift; | ||
| 51 | my $model = 'Rapi::Demo::Chinook::Model::DB'; | ||
| 52 | |||
| 53 | my $db = file( $self->chinook_db ); | ||
| 54 | |||
| 55 | $self->init_db unless (-f $db); | ||
| 56 | |||
| 57 | # Make sure the path is valid/exists: | ||
| 58 | $db->resolve; | ||
| 59 | |||
| 60 | Module::Runtime::require_module($model); | ||
| 61 | $model->config->{connect_info}{dsn} = "dbi:SQLite:$db"; | ||
| 62 | |||
| 63 | return [ | ||
| 64 | [ $model => 'Model::DB' ] | ||
| 65 | ] | ||
| 66 | }; | ||
| 67 | |||
| 68 | |||
| 69 | sub init_db { | ||
| 70 | my ($self, $ovr) = @_; | ||
| 71 | |||
| 72 | my ($src,$dst) = (file($self->_init_chinook_db),file($self->chinook_db)); | ||
| 73 | |||
| 74 | die "init_db(): ERROR: init db file '$src' not found!" unless (-f $src); | ||
| 75 | |||
| 76 | if(-e $dst) { | ||
| 77 | if($ovr) { | ||
| 78 | $dst->remove; | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | die "init_db(): Destination file '$dst' already exists -- call with true arg to overwrite."; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | print STDERR "Initializing $dst\n" if ($self->debug); | ||
| 86 | #$src->copy_to( $dst ); | ||
| 87 | # Create as new file instead of copying to avoid perm issues in a cross-platform manner: | ||
| 88 | # (also not bothering to do this in chunks because the file is smaller than 1M) | ||
| 89 | $dst->spew( scalar $src->slurp ); | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | |||
| 95 | __END__ |