File Coverage

blib/lib/Daje/Plugin/Apploader.pm
Criterion Covered Total %
statement 20 156 12.8
branch 0 24 0.0
condition n/a
subroutine 7 13 53.8
pod 1 1 100.0
total 28 194 14.4


line stmt bran cond sub pod time code
1             package Daje::Plugin::Apploader;
2 1     1   256058 use Mojo::Base 'Mojolicious::Plugin', -signatures;
  1         9129  
  1         6  
3 1     1   3404 use v5.42;
  1         3  
4              
5 1     1   512 use Data::Dumper;
  1         7152  
  1         57  
6 1     1   5 use List::Util;
  1         1  
  1         46  
7 1     1   377 use Mojo::Loader qw {load_class find_modules};
  1         213899  
  1         107  
8 1     1   806 use Daje::Database::Migrator;
  1         1774  
  1         8  
9 1     1   1519 use CPAN;
  1         301846  
  1         2852  
10              
11             # NAME
12             # ====
13             #
14             # Daje::Plugin::Apploader - Loads stuff from a config file
15             # during starup of the Daje server
16             #
17             # SYNOPSIS
18             # ========
19             #
20             # load_ables => {
21             # namespaces => {
22             # plugins => [
23             # {
24             # name => ''
25             # }
26             # ],
27             # routes => [
28             # {
29             # name => ''
30             # }
31             # ]
32             # },
33             # plugin => [
34             # {
35             # name => '',
36             # options => [
37             # {
38             # name => '',
39             # option => ''
40             # }
41             # ]
42             # }
43             # ],
44             # helper => [
45             # {
46             # name => '',
47             # class => '',
48             # options => ''
49             # }
50             # ],
51             # routes => [
52             # {
53             # route => '',
54             # target => '',
55             # method => ''
56             # }
57             # ]
58             # }
59             # use Daje::Plugin::Apploader;
60             #
61             # sub register ($self, $app)
62             #
63             # DESCRIPTION
64             # ===========
65             #
66             # Daje::Plugin::Apploader is a simple apploader for the Daje server.
67             # It automatically installs missing / updates to newer versions of
68             # modules from cpan if told to.
69             #
70             # LICENSE
71             # =======
72             #
73             # Copyright (C) janeskil1525.
74             #
75             # This library is free software; you can redistribute it and/or modify
76             # it under the same terms as Perl itself.
77             #
78             # AUTHOR
79             # ======
80             #
81             # janeskil1525 Ejaneskil1525@gmail.comE
82             #
83              
84             our $VERSION = "0.35";
85              
86 0     0 1   sub register ($self, $app, $config) {
  0            
  0            
  0            
  0            
87              
88 0           my $modules;
89 0           $app->log->debug("Daje::Plugin::Apploader::register start");
90 0           $self->_setup_database($app);
91              
92 0           $app->log->debug("_setup_database done");
93              
94 0           try {
95 0           my $pg = $app->pg;
96 0           my $loadables = $app->config("load_ables");
97              
98 0 0         if(exists $loadables->{namespaces}->{plugins}) {
99 0           my $length = scalar @{$loadables->{namespaces}->{plugins}};
  0            
100 0           for(my $i = 0; $i < $length; $i++) {
101 0           my $module;
102 0           push @{$app->plugins->namespaces}, @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
  0            
103 0           @{$module->{modules}} = find_modules @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
  0            
104 0           $module->{namespace} = @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
105 0           push @{$modules}, $module;
  0            
106             }
107             }
108              
109 0           $app->log->debug("plugin namespace done");
110 0 0         if(exists $loadables->{namespaces}->{routes}) {
111 0           my $length = scalar @{$loadables->{namespaces}->{routes}};
  0            
112 0           for(my $i = 0; $i < $length; $i++) {
113 0           push @{$app->routes->namespaces}, @{$loadables->{namespaces}->{routes}}[$i]->{name}
  0            
114 0           }
115             }
116              
117 0           $app->log->debug("routes namespace done");
118 0 0         if(exists $loadables->{plugin}) {
119 0           my $length = scalar @{$loadables->{plugin}};
  0            
120 0           my @install = ();
121 0           for(my $i = 0; $i < $length; $i++) {
122 0           $self->_find_missing_modules(@{$loadables->{plugin}}[$i]->{name}, $modules, \@install);
  0            
123             }
124 0           for(my $i = 0; $i < $length; $i++) {
125 0 0         if (exists @{$loadables->{plugin}}[$i]->{options}) {
  0            
126             my %plugin = $self->_plugin_options(
127             $app,
128 0           @{$loadables->{plugin}}[$i]->{name},
129 0           @{$loadables->{plugin}}[$i]->{options}
130 0           );
131 0           $app->plugin(
132             %plugin
133             );
134             } else {
135 0           $app->plugin(@{$loadables->{plugin}}[$i]->{name});
  0            
136             }
137             }
138             }
139              
140 0           $app->log->debug("plugin loading done");
141 0 0         if(exists $loadables->{helper}) {
142 0           my $length = scalar @{$loadables->{helper}};
  0            
143 0           for(my $i = 0; $i < $length; $i++) {
144 0           my $class = @{$loadables->{helper}}[$i]->{class};
  0            
145 0 0         if (my $e = load_class $class) {
146 0           $app->log->fatal($e);
147             }
148 0           my $name = @{$loadables->{helper}}[$i]->{name};
  0            
149 0     0     $app->helper($name => sub {state $var = $class->new()});
  0            
150             }
151             }
152              
153 0           $app->log->debug("helper loading done");
154 0 0         if(exists $loadables->{routes}) {
155 0           my $length = scalar @{$loadables->{routes}};
  0            
156 0           for(my $i = 0; $i < $length; $i++) {
157             # ???
158             }
159             }
160              
161 0           $app->log->debug("route loading done");
162 0           my $test = 1;
163             } catch ($e) {
164 0           my $error = $e;
165 0           $app->log->fatal($e);
166             }
167             }
168              
169 0     0     sub _find_missing_modules($self, $plugin, $modules, $install) {
  0            
  0            
  0            
  0            
  0            
170 0           my $length = scalar @{$modules};
  0            
171              
172 0           for(my $i = 0; $i < $length; $i++) {
173 0           my $class = @{$modules}[$i]->{namespace} . "::" . $plugin;
  0            
174 0 0         if (!grep(/^$class$/, @{$modules})) {
  0            
175 0 0         if (!grep(/^$class$/, @{$install})) {
  0 0          
176 0           push @{$install}, $class;
  0            
177 0           } elsif (grep(/^$class$/, @{$install})) {
178 0           my $index = 0;
179 0           $index++ until @{$install}[$index] eq $class;
  0            
180 0           splice(@{$install}, $index, 1);
  0            
181             }
182             }
183             }
184             }
185              
186 0     0     sub _install_modules($self, $install) {
  0            
  0            
  0            
187 0           my $length = scalar @{$install};
  0            
188 0 0         if( $length > 0 ) {
189 0           CPAN::HandleConfig->load;
190 0           CPAN::Shell::setup_output;
191 0           CPAN::Index->reload;
192 0           for (my $i = 0; $i < $length; $i++) {
193 0           CPAN::Shell->install(@{$install}[$i]);
  0            
194             }
195             }
196             }
197              
198 0     0     sub _setup_database($self, $app) {
  0            
  0            
  0            
199              
200 0 0         if($app->config('migrations')) {
201 0           try {
202 0           Daje::Database::Migrator->new(
203             pg => $app->pg,
204             migrations => $app->config('migrations'),
205             )->migrate();
206             }
207             catch($e) {
208 0           $app->log->error($e);
209             };
210             }
211             }
212              
213 0     0     sub _plugin_options($self, $app, $name, $options) {
  0            
  0            
  0            
  0            
  0            
214              
215 0           my %plugin = ();
216 0           my %result = ();
217              
218 0           my $length = scalar @{$options};
  0            
219 0           for (my $i = 0; $i < $length; $i++) {
220 0           my $plug = @{$options}[$i]->{name};
  0            
221 0           my $data = @{$options}[$i]->{option};
  0            
222 0           %plugin = ($plug => eval $data);
223             }
224              
225 0           $result{$name} = \%plugin;
226              
227 0           return %result;
228             }
229             1;
230             __END__