| 1 | package BioSAILs::Utils::Plugin; | ||
| 2 | |||
| 3 | use MooseX::App::Role; | ||
| 4 | use IO::File; | ||
| 5 | use File::Path qw(make_path remove_tree); | ||
| 6 | |||
| 7 | use IPC::Cmd; | ||
| 8 | use Cwd qw(getcwd); | ||
| 9 | use Try::Tiny; | ||
| 10 | use List::MoreUtils qw(uniq); | ||
| 11 | |||
| 12 | with 'MooseX::Object::Pluggable'; | ||
| 13 | |||
| 14 | =head1 HPC::Runner::Command::Utils::Plugin | ||
| 15 | |||
| 16 | Take care of all file operations | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head2 Attributes | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head3 plugins | ||
| 25 | |||
| 26 | Load plugins that are used both by the submitter and executor such as logging pluggins | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | option 'plugins' => ( | ||
| 31 | traits => ['Array'], | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'ArrayRef[Str]', | ||
| 34 | documentation => 'Load aplication plugins', | ||
| 35 | cmd_split => qr/,/, | ||
| 36 | required => 0, | ||
| 37 | default => sub { [] }, | ||
| 38 | handles => { | ||
| 39 | has_plugins => 'count', | ||
| 40 | join_plugins => 'join', | ||
| 41 | }, | ||
| 42 | ); | ||
| 43 | |||
| 44 | option 'plugins_opts' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'HashRef', | ||
| 47 | documentation => 'Options for application plugins', | ||
| 48 | required => 0, | ||
| 49 | default => sub { {} }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | |||
| 53 | =head2 Subroutines | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | =head3 gen_load_plugins | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub gen_load_plugins { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | return unless $self->plugins; | ||
| 65 | |||
| 66 | $self->app_load_plugins( $self->plugins ); | ||
| 67 | $self->parse_plugin_opts( $self->plugins_opts ); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | =head2 Subroutines | ||
| 72 | |||
| 73 | =head3 after *load_plugins | ||
| 74 | |||
| 75 | After loading the plugins make sure to reload the configs - to get any options we didn't get the first time around | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | after 'gen_load_plugins' => sub { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | if ( $self->has_config_files ) { | ||
| 83 | $self->load_configs; | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | =head3 app_load_plugin | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub app_load_plugins { | ||
| 92 | my $self = shift; | ||
| 93 | my $plugins = shift; | ||
| 94 | |||
| 95 | return unless $plugins; | ||
| 96 | |||
| 97 | foreach my $plugin ( @{$plugins} ) { | ||
| 98 | try { | ||
| 99 | $self->load_plugin($plugin); | ||
| 100 | } | ||
| 101 | catch { | ||
| 102 | $self->app_log->warn("Could not load plugin $plugin!\n$_"); | ||
| 103 | return; | ||
| 104 | }; | ||
| 105 | $self->app_log->info( 'Loaded plugin ' . $plugin ); | ||
| 106 | } | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | =head3 parse_plugin_opts | ||
| 111 | |||
| 112 | parse the opts from --plugin_opts | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub parse_plugin_opts { | ||
| 117 | my $self = shift; | ||
| 118 | my $opt_href = shift; | ||
| 119 | |||
| 120 | return unless $opt_href; | ||
| 121 | while ( my ( $k, $v ) = each %{$opt_href} ) { | ||
| 122 | $self->$k($v) if $self->can($k); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; |