| 1 | package Bot::Cobalt::Conf::File::Plugins; | ||
| 2 | $Bot::Cobalt::Conf::File::Plugins::VERSION = '0.021001'; | ||
| 3 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Scalar::Util 'blessed'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Bot::Cobalt::Common ':types'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Bot::Cobalt::Conf::File::PerPlugin; | ||
| 11 | |||
| 12 | use Path::Tiny; | ||
| 13 | use Types::Path::Tiny -types; | ||
| 14 | |||
| 15 | use List::Objects::WithUtils; | ||
| 16 | use List::Objects::Types -types; | ||
| 17 | |||
| 18 | use Moo; | ||
| 19 | extends 'Bot::Cobalt::Conf::File'; | ||
| 20 | |||
| 21 | |||
| 22 | has etcdir => ( | ||
| 23 | required => 1, | ||
| 24 | is => 'rwp', | ||
| 25 | isa => Path, | ||
| 26 | coerce => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | has _per_plug_objs => ( | ||
| 31 | lazy => 1, | ||
| 32 | is => 'ro', | ||
| 33 | isa => HashObj, | ||
| 34 | coerce => 1, | ||
| 35 | builder => '_build_per_plugin_objs', | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub _build_per_plugin_objs { | ||
| 39 | my ($self) = @_; | ||
| 40 | |||
| 41 | ## Create PerPlugin cf objs for each plugin | ||
| 42 | hash( | ||
| 43 | $self->cfg_as_hash->keys->map(sub { | ||
| 44 | ( $_ => $self->_create_perplugin_obj($_) ) | ||
| 45 | })->all | ||
| 46 | ) | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _create_perplugin_obj { | ||
| 50 | my ($self, $alias) = @_; | ||
| 51 | |||
| 52 | my $this_cfg = $self->cfg_as_hash->get($alias) | ||
| 53 | || confess "_create_perplugin_obj passed unknown alias $alias"; | ||
| 54 | |||
| 55 | my %new_opts; | ||
| 56 | |||
| 57 | $new_opts{module} = $this_cfg->{Module} | ||
| 58 | || confess "No Module defined for plugin $alias"; | ||
| 59 | |||
| 60 | if (defined $this_cfg->{Config}) { | ||
| 61 | my $this_cf_path = path($this_cfg->{Config}); | ||
| 62 | unless ( $this_cf_path->is_absolute ) { | ||
| 63 | $this_cf_path = path( $self->etcdir .'/'. $this_cf_path ); | ||
| 64 | } | ||
| 65 | |||
| 66 | $new_opts{config_file} = $this_cf_path | ||
| 67 | } | ||
| 68 | |||
| 69 | $new_opts{autoload} = 0 | ||
| 70 | if $this_cfg->{NoAutoLoad}; | ||
| 71 | |||
| 72 | $new_opts{priority} = $this_cfg->{Priority} | ||
| 73 | if defined $this_cfg->{Priority}; | ||
| 74 | |||
| 75 | if (defined $this_cfg->{Opts}) { | ||
| 76 | confess "Opts: directive for plugin $alias is not a hash" | ||
| 77 | unless ref $this_cfg->{Opts} eq 'HASH'; | ||
| 78 | |||
| 79 | $new_opts{extra_opts} = $this_cfg->{Opts}; | ||
| 80 | } | ||
| 81 | |||
| 82 | Bot::Cobalt::Conf::File::PerPlugin->new( | ||
| 83 | %new_opts | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub plugin { | ||
| 88 | my ($self, $plugin) = @_; | ||
| 89 | |||
| 90 | confess "plugin() requires a plugin alias" | ||
| 91 | unless defined $plugin; | ||
| 92 | |||
| 93 | $self->_per_plug_objs->{$plugin} | ||
| 94 | } | ||
| 95 | |||
| 96 | sub list_plugins { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | [ keys %{ $self->_per_plug_objs } ] | ||
| 100 | } | ||
| 101 | |||
| 102 | sub clear_plugin { | ||
| 103 | my ($self, $plugin) = @_; | ||
| 104 | |||
| 105 | confess "clear_plugin requires a plugin alias" | ||
| 106 | unless defined $plugin; | ||
| 107 | |||
| 108 | delete $self->_per_plug_objs->{$plugin} | ||
| 109 | } | ||
| 110 | |||
| 111 | sub load_plugin { | ||
| 112 | my ($self, $plugin) = @_; | ||
| 113 | |||
| 114 | confess "load_plugin requires a plugin alias" | ||
| 115 | unless defined $plugin; | ||
| 116 | |||
| 117 | $self->_per_plug_objs->{$plugin} | ||
| 118 | = $self->_create_perplugin_obj($plugin) | ||
| 119 | } | ||
| 120 | |||
| 121 | sub install_plugin { | ||
| 122 | my ($self, $plugin, $plugin_obj) = @_; | ||
| 123 | |||
| 124 | unless (defined $plugin_obj) { | ||
| 125 | confess | ||
| 126 | "install_plugin requires a plugin alias and Conf object" | ||
| 127 | } | ||
| 128 | |||
| 129 | unless (blessed $plugin_obj && | ||
| 130 | $plugin_obj->isa('Bot::Cobalt::Conf::File::PerPlugin') ) { | ||
| 131 | |||
| 132 | confess | ||
| 133 | "install_plugin requires a Bot::Cobalt::Conf::File::PerPlugin object" | ||
| 134 | } | ||
| 135 | |||
| 136 | $self->_per_plug_objs->{$plugin} = $plugin_obj | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | around 'validate' => sub { | ||
| 141 | my ($orig, $self, $cfg) = @_; | ||
| 142 | |||
| 143 | 1 | ||
| 144 | }; | ||
| 145 | |||
| 146 | |||
| 147 | 1; | ||
| 148 | __END__ | ||