| 1 | package Poet::Environment; | ||||||||||||||||||||||||||||||||||||||
| 2 | 12 | $Poet::Environment::VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Poet::Moose; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Poet::Tools qw(can_load catdir); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 12 | has 'app_name' => ( required => 1 ); | |||||||||||||||||||||||||||||||||||||
| 9 | 12 | has 'conf' => (); | |||||||||||||||||||||||||||||||||||||
| 10 | 12 | has 'importer' => (); | |||||||||||||||||||||||||||||||||||||
| 11 | 12 | has 'log_manager' => (); | |||||||||||||||||||||||||||||||||||||
| 12 | 12 | has 'root_dir' => ( required => 1 ); | |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 12 | my ($current_env); | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | method subdirs () { [qw(bin comps conf data db lib logs static t)] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
subdirs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | method app_class ($class_name) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
app_class: called CC=3 SCAR=11.0
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 63 | my $app_class_name = join( "::", $self->app_name, $class_name ); | |||||||||||||||||||||||||||||||||||||
| 20 | 63 | my $env_class_name = join( "::", "Poet", $class_name ); | |||||||||||||||||||||||||||||||||||||
| 21 | return | ||||||||||||||||||||||||||||||||||||||
| 22 | 63 | ▶ | can_load($app_class_name) ? $app_class_name | ||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||
| 23 | : can_load($env_class_name) ? $env_class_name | ||||||||||||||||||||||||||||||||||||||
| 24 | : die "cannot load $app_class_name or $class_name"; | ||||||||||||||||||||||||||||||||||||||
| 25 | 12 | } | |||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 12 | ▶ | method generate_subdir_methods ($class:) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
generate_subdir_methods: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 28 | 7 | foreach my $subdir ( 'root', @{ $class->subdirs() } ) { | |||||||||||||||||||||||||||||||||||||
| 29 | 70 | my $dir_method = $subdir . "_dir"; | |||||||||||||||||||||||||||||||||||||
| 30 | 70 | ▶ | has $dir_method => () if $subdir ne 'root'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 31 | 70 | ▶ | my $path_method = $subdir eq 'root' ? "path" : $subdir . "_path"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 32 | __PACKAGE__->meta->add_method( | ||||||||||||||||||||||||||||||||||||||
| 33 | $path_method, | ||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
data_path: called CC=2 SCAR=7.2
Subroutine
conf_path: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my ( $self, $relpath ) = @_; | |||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | croak "$path_method expects relative path as argument" | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 37 | unless defined($relpath); | ||||||||||||||||||||||||||||||||||||||
| 38 | 3 | return $self->$dir_method . "/" . $relpath; | |||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||
| 40 | 70 | ); | |||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||
| 42 | 12 | } | |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 12 | ▶ | method initialize_current_environment ($class: %params) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
initialize_current_environment: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||
| 45 | 6 | ▶ | if ( defined($current_env) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 46 | 0 | die sprintf( "initialize_current_environment called when current_env already set (%s)", | |||||||||||||||||||||||||||||||||||||
| 47 | $current_env->root_dir() ); | ||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | $current_env = $params{env} || $class->new(%params); | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$params{'env'} || $class->new(%params)
$params{'env'} || $class->new(%params)
MC/DC0%0/2
$params{'env'} || $class->new(%params)
$params{'env'}
$class->new(%params)
MC/DC0%0/2
$params{'env'} || $class->new(%params)
$params{'env'}
$class->new(%params)
Truth table66%2/3
A || B
A= $params{'env'}B= $class->new(%params)
Truth table66%2/3
A || B
A= $params{'env'}B= $class->new(%params)
| |||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | # Initialize logging and caching | ||||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||||
| 53 | 6 | $current_env->app_class('Log')->initialize_logging(); | |||||||||||||||||||||||||||||||||||||
| 54 | 6 | $current_env->app_class('Cache')->initialize_caching(); | |||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | 6 | return $current_env; | |||||||||||||||||||||||||||||||||||||
| 57 | 12 | } | |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | 12 | ▶ | method current_env ($class:) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
current_env: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 60 | 92 | return $current_env; | |||||||||||||||||||||||||||||||||||||
| 61 | 12 | } | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | 12 | ▶ | method BUILD () { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BUILD: called CC=3 SCAR=11.0
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 64 | 19 | my $root_dir = $self->root_dir(); | |||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | # Unshift lib dir onto @INC | ||||||||||||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||||||||||||
| 68 | 19 | my $lib_dir = "$root_dir/lib"; | |||||||||||||||||||||||||||||||||||||
| 69 | 19 | ▶ | unless ( $INC[0] eq $lib_dir ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 70 | 19 | unshift( @INC, $lib_dir ); | |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | # Initialize configuration | ||||||||||||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||||||||||||
| 75 | $self->{conf} = | ||||||||||||||||||||||||||||||||||||||
| 76 | 19 | $self->app_class('Conf') | |||||||||||||||||||||||||||||||||||||
| 77 | ->new( root_dir => $root_dir, conf_dir => catdir( $root_dir, "conf" ) ); | ||||||||||||||||||||||||||||||||||||||
| 78 | 16 | my $conf = $self->{conf}; | |||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | # Initialize importer | ||||||||||||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||||||||||||
| 82 | 16 | $self->{importer} = $self->app_class('Import')->new( env => $self ); | |||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | # Determine where our standard subdirectories (bin, comps, etc.) | ||||||||||||||||||||||||||||||||||||||
| 85 | # are. Can override in configuration with env.bin_dir, env.comps_dir, | ||||||||||||||||||||||||||||||||||||||
| 86 | # etc. Otherwise use obvious defaults. | ||||||||||||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||||||||||||
| 88 | 0 | foreach my $subdir ( @{ $self->subdirs() } ) { | |||||||||||||||||||||||||||||||||||||
| 89 | 144 | my $method = $subdir . "_dir"; | |||||||||||||||||||||||||||||||||||||
| 90 | 144 | $self->{$method} = $conf->get( "env.$method" => "$root_dir/$subdir" ); | |||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||
| 92 | 12 | } | |||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | 12 | __PACKAGE__->generate_subdir_methods(); | |||||||||||||||||||||||||||||||||||||
| 95 | 12 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 12 | 1; | |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||||||||