| 1 | package BioSAILs::Utils::LoadConfigs; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use MooseX::App::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use Config::Any; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use File::HomeDir; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Cwd; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use MooseX::Types::Path::Tiny qw/Path Paths AbsPaths AbsFile/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use Path::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 1 | ▶ | use Hash::Merge qw( merge ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 1 | option 'no_configs' => ( | |||||||||||||
| 18 | is => 'rw', | ||||||||||||||
| 19 | isa => 'Bool', | ||||||||||||||
| 20 | default => 0, | ||||||||||||||
| 21 | documentation => '--no_configs tells HPC::Runner not to load any configs', | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | option 'config' => ( | |||||||||||||
| 25 | isa => 'Str', | ||||||||||||||
| 26 | is => 'rw', | ||||||||||||||
| 27 | required => 0, | ||||||||||||||
| 28 | documentation => 'Override the search paths and supply your own config.', | ||||||||||||||
| 29 | isa => AbsFile, | ||||||||||||||
| 30 | coerce => 1, | ||||||||||||||
| 31 | predicate => 'has_config', | ||||||||||||||
| 32 | ); | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | option 'config_base' => ( | |||||||||||||
| 35 | is => 'rw', | ||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||
| 37 | default => '.config', | ||||||||||||||
| 38 | documentation => 'Basename of config files', | ||||||||||||||
| 39 | ); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 1 | option 'search' => ( | |||||||||||||
| 42 | traits => ['Bool'], | ||||||||||||||
| 43 | is => 'rw', | ||||||||||||||
| 44 | isa => 'Bool', | ||||||||||||||
| 45 | default => 1, | ||||||||||||||
| 46 | handles => { | ||||||||||||||
| 47 | 'no_search' => 'unset', | ||||||||||||||
| 48 | }, | ||||||||||||||
| 49 | documentation => | ||||||||||||||
| 50 | 'Search for config files in ~/.config.(ext) and in your current working directory.' | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | option 'search_path' => ( | ||||||||||||||
| 54 | traits => ['Array'], | ||||||||||||||
| 55 | is => 'rw', | ||||||||||||||
| 56 | isa => 'ArrayRef[Str]', | ||||||||||||||
| 57 | 0 | ▶ | default => sub { [ File::HomeDir->my_home, getcwd() ] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 58 | 1 | handles => { | |||||||||||||
| 59 | all_search_path => 'elements', | ||||||||||||||
| 60 | append_search_path => 'push', | ||||||||||||||
| 61 | prepend_search_path => 'unshift', | ||||||||||||||
| 62 | map_search_path => 'map', | ||||||||||||||
| 63 | has_search_path => 'count', | ||||||||||||||
| 64 | }, | ||||||||||||||
| 65 | documentation => | ||||||||||||||
| 66 | 'Enable a search path for configs. Default is the home dir and your cwd.' | ||||||||||||||
| 67 | ); | ||||||||||||||
| 68 | |||||||||||||||
| 69 | has 'filter_keys' => ( | ||||||||||||||
| 70 | is => 'rw', | ||||||||||||||
| 71 | traits => ['Array'], | ||||||||||||||
| 72 | is => 'rw', | ||||||||||||||
| 73 | isa => 'ArrayRef[Str]', | ||||||||||||||
| 74 | 0 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 75 | 1 | handles => { | |||||||||||||
| 76 | all_filter_keys => 'elements', | ||||||||||||||
| 77 | append_filter_keys => 'push', | ||||||||||||||
| 78 | prepend_filter_keys => 'unshift', | ||||||||||||||
| 79 | map_filter_keys => 'map', | ||||||||||||||
| 80 | has_filter_keys => 'count', | ||||||||||||||
| 81 | }, | ||||||||||||||
| 82 | ); | ||||||||||||||
| 83 | |||||||||||||||
| 84 | has 'config_files' => ( | ||||||||||||||
| 85 | traits => ['Array'], | ||||||||||||||
| 86 | is => 'rw', | ||||||||||||||
| 87 | isa => AbsPaths, | ||||||||||||||
| 88 | coerce => 1, | ||||||||||||||
| 89 | 0 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 90 | 1 | handles => { | |||||||||||||
| 91 | all_config_files => 'elements', | ||||||||||||||
| 92 | append_config_files => 'push', | ||||||||||||||
| 93 | prepend_config_files => 'unshift', | ||||||||||||||
| 94 | map_config_files => 'map', | ||||||||||||||
| 95 | has_config_files => 'count', | ||||||||||||||
| 96 | count_config_files => 'count', | ||||||||||||||
| 97 | }, | ||||||||||||||
| 98 | ); | ||||||||||||||
| 99 | |||||||||||||||
| 100 | has '_config_data' => ( | ||||||||||||||
| 101 | is => 'rw', | ||||||||||||||
| 102 | isa => 'ArrayRef', | ||||||||||||||
| 103 | predicate => 'has_config_data', | ||||||||||||||
| 104 | 0 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 105 | 1 | ); | |||||||||||||
| 106 | |||||||||||||||
| 107 | has '_merged_config_data' => ( | ||||||||||||||
| 108 | is => 'rw', | ||||||||||||||
| 109 | isa => 'HashRef', | ||||||||||||||
| 110 | predicate => 'has_merged_config_data', | ||||||||||||||
| 111 | 0 | ▶ | default => sub { return {} }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 112 | 1 | ); | |||||||||||||
| 113 | |||||||||||||||
| 114 | ▶ | sub BUILD { } | |||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | before 'BUILD' => sub { | ||||||||||||||
| 117 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | ▶ | return if $self->no_configs; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | $self->decide_search_configs; | |||||||||||||
| 121 | 1 | }; | |||||||||||||
| 122 | |||||||||||||||
| 123 | sub decide_search_configs { | ||||||||||||||
| 124 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
decide_search_configs: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 125 | |||||||||||||||
| 126 | 0 | ▶ | if ( $self->has_config ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 127 | 0 | $self->append_config_files( $self->config ); | |||||||||||||
| 128 | 0 | $self->load_configs; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | elsif ( $self->search ) { | ||||||||||||||
| 131 | 0 | $self->search_configs; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | sub search_configs { | ||||||||||||||
| 136 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
search_configs: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 137 | 0 | my $extension = shift; | |||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | foreach my $extension ( Config::Any->extensions ) { | |||||||||||||
| 140 | 0 | foreach my $dir ( $self->all_search_path ) { | |||||||||||||
| 141 | 0 | my $dir = $dir . '/'; | |||||||||||||
| 142 | |||||||||||||||
| 143 | #We will check with and without extensions | ||||||||||||||
| 144 | 0 | my $check_file; | |||||||||||||
| 145 | 0 | $check_file = File::Spec->catfile( $dir . $self->config_base ); | |||||||||||||
| 146 | 0 | ▶ | $self->append_config_files($check_file) if -f $check_file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | $check_file = | |||||||||||||
| 149 | File::Spec->catfile( | ||||||||||||||
| 150 | $dir . $self->config_base . '.' . $extension ); | ||||||||||||||
| 151 | 0 | ▶ | $self->append_config_files($check_file) if -f $check_file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | } | ||||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | $self->load_configs; | |||||||||||||
| 156 | |||||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | sub load_configs { | ||||||||||||||
| 160 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
load_configs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | my $command_name = $self->{_original_class_name}; | |||||||||||||
| 163 | 0 | my $cfg = | |||||||||||||
| 164 | Config::Any->load_files( { files => $self->config_files, use_ext => 1 } ); | ||||||||||||||
| 165 | |||||||||||||||
| 166 | 0 | $self->_config_data($cfg); | |||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | $self->apply_configs; | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | sub apply_configs { | ||||||||||||||
| 172 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
apply_configs: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 173 | |||||||||||||||
| 174 | 0 | my $command_name = $self->{_original_class_name}; | |||||||||||||
| 175 | 0 | ▶ | for ( my $x = 0 ; $x < $self->count_config_files ; $x++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | 0 | my $c = $self->_config_data->[$x]->{ $self->config_files->[$x] }; | |||||||||||||
| 177 | 0 | my $old = $self->_merged_config_data; | |||||||||||||
| 178 | 0 | my $new = merge($c, $old); | |||||||||||||
| 179 | 0 | $self->_merged_config_data($new); | |||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | my @keys = keys %{$c}; | |||||||||||||
| 182 | |||||||||||||||
| 183 | 0 | ▶ | if ( !$self->has_filter_keys ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | 0 | my @filter_keys = grep { $_ ne 'global' } @keys; | |||||||||||||
| 185 | 0 | $self->filter_keys( \@filter_keys ); | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | #Global is always applied | ||||||||||||||
| 189 | 0 | ▶ | $self->apply_attributes( $c, 'global' ) if exists $c->{global}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | |||||||||||||||
| 191 | 0 | map { $self->apply_attributes( $c, $_ ) } $self->all_filter_keys; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | sub apply_attributes { | ||||||||||||||
| 197 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
apply_attributes: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 198 | 0 | my $conf = shift; | |||||||||||||
| 199 | 0 | my $attr = shift; | |||||||||||||
| 200 | |||||||||||||||
| 201 | 0 | ▶ | return unless ref($conf) eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 202 | 0 | ▶ | return unless ref( $conf->{$attr} ) eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 203 | |||||||||||||||
| 204 | 0 | ▶ | while ( my ( $key, $value ) = each %{ $conf->{$attr} } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | |||||||||||||||
| 206 | 0 | ▶ | if ( $self->can($key) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | try { | ||||||||||||||
| 208 | 0 | ▶ | $self->$key($value); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 209 | } | ||||||||||||||
| 210 | catch { | ||||||||||||||
| 211 | 0 | ▶ | warn 'You tried to assign ' . $key . ' to ' . $value . "\n"; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 212 | 0 | }; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | } | ||||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | 1 | 1; | |||||||||||||