File Coverage

blib/lib/Daje/Plugin/Apploader.pm
Criterion Covered Total %
statement 23 160 14.3
branch 0 24 0.0
condition n/a
subroutine 8 14 57.1
pod 1 1 100.0
total 32 199 16.0


line stmt bran cond sub pod time code
1             package Daje::Plugin::Apploader;
2 1     1   315507 use Mojo::Base 'Mojolicious::Plugin', -signatures;
  1         8810  
  1         5  
3 1     1   2618 use v5.42;
  1         4  
4              
5 1     1   502 use Data::Dumper;
  1         5768  
  1         60  
6 1     1   6 use List::Util;
  1         1  
  1         49  
7 1     1   424 use Mojo::Loader qw {load_class find_modules};
  1         161002  
  1         73  
8 1     1   465 use Daje::Database::Migrator;
  1         1234  
  1         26  
9 1     1   1384 use CPAN;
  1         279615  
  1         400  
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.45";
85              
86 1     1   32 use Data::Dumper;
  1         4  
  1         2249  
87             has 'pg';
88              
89 0     0 1   sub register ($self, $app, $config) {
  0            
  0            
  0            
  0            
90              
91 0           my $modules;
92 0           $app->log->debug("Daje::Plugin::Apploader::register start");
93 0           $self->_setup_database($app);
94              
95 0           $app->log->debug("_setup_database done");
96              
97 0           try {
98 0           my $pg = $app->pg;
99 0           my $loadables = $app->config("load_ables");
100              
101 0 0         if(exists $loadables->{namespaces}->{plugins}) {
102 0           my $length = scalar @{$loadables->{namespaces}->{plugins}};
  0            
103 0           for(my $i = 0; $i < $length; $i++) {
104 0           my $module;
105 0           push @{$app->plugins->namespaces}, @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
  0            
106 0           @{$module->{modules}} = find_modules @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
  0            
107 0           $module->{namespace} = @{$loadables->{namespaces}->{plugins}}[$i]->{name};
  0            
108 0           push @{$modules}, $module;
  0            
109             }
110             }
111              
112 0           $app->log->debug("plugin namespace done");
113 0 0         if(exists $loadables->{namespaces}->{routes}) {
114 0           my $length = scalar @{$loadables->{namespaces}->{routes}};
  0            
115 0           for(my $i = 0; $i < $length; $i++) {
116 0           push @{$app->routes->namespaces}, @{$loadables->{namespaces}->{routes}}[$i]->{name}
  0            
117 0           }
118             }
119              
120 0           $app->log->debug("routes namespace done");
121 0 0         if(exists $loadables->{plugin}) {
122              
123 0           my $length = scalar @{$loadables->{plugin}};
  0            
124 0           my @install = ();
125 0           for(my $i = 0; $i < $length; $i++) {
126 0           $self->_find_missing_modules(@{$loadables->{plugin}}[$i]->{name}, $modules, \@install);
  0            
127             }
128 0           for(my $i = 0; $i < $length; $i++) {
129 0 0         if (exists @{$loadables->{plugin}}[$i]->{options}) {
  0            
130             my $plugin = $self->_plugin_options(
131             $app,
132 0           @{$loadables->{plugin}}[$i]->{name},
133 0           @{$loadables->{plugin}}[$i]->{options}
134 0           );
135             } else {
136 0           $app->plugin(@{$loadables->{plugin}}[$i]->{name});
  0            
137             }
138             }
139             }
140              
141 0           $app->log->debug("plugin loading done");
142 0 0         if(exists $loadables->{helper}) {
143 0           my $length = scalar @{$loadables->{helper}};
  0            
144 0           for(my $i = 0; $i < $length; $i++) {
145 0           my $class = @{$loadables->{helper}}[$i]->{class};
  0            
146 0 0         if (my $e = load_class $class) {
147 0           $app->log->fatal($e);
148             }
149 0           my $name = @{$loadables->{helper}}[$i]->{name};
  0            
150 0     0     $app->helper($name => sub {state $var = $class->new()});
  0            
151             }
152             }
153              
154 0           $app->log->debug("helper loading done");
155 0 0         if(exists $loadables->{routes}) {
156 0           my $length = scalar @{$loadables->{routes}};
  0            
157 0           for(my $i = 0; $i < $length; $i++) {
158             # ???
159             }
160             }
161              
162 0           $app->log->debug("route loading done");
163 0           my $test = 1;
164             } catch ($e) {
165 0           my $error = $e;
166 0           $app->log->fatal($e);
167             }
168             }
169              
170 0     0     sub _find_missing_modules($self, $plugin, $modules, $install) {
  0            
  0            
  0            
  0            
  0            
171 0           my $length = scalar @{$modules};
  0            
172              
173 0           for(my $i = 0; $i < $length; $i++) {
174 0           my $class = @{$modules}[$i]->{namespace} . "::" . $plugin;
  0            
175 0 0         if (!grep(/^$class$/, @{$modules})) {
  0            
176 0 0         if (!grep(/^$class$/, @{$install})) {
  0 0          
177 0           push @{$install}, $class;
  0            
178 0           } elsif (grep(/^$class$/, @{$install})) {
179 0           my $index = 0;
180 0           $index++ until @{$install}[$index] eq $class;
  0            
181 0           splice(@{$install}, $index, 1);
  0            
182             }
183             }
184             }
185             }
186              
187 0     0     sub _install_modules($self, $install) {
  0            
  0            
  0            
188 0           my $length = scalar @{$install};
  0            
189 0 0         if( $length > 0 ) {
190 0           CPAN::HandleConfig->load;
191 0           CPAN::Shell::setup_output;
192 0           CPAN::Index->reload;
193 0           for (my $i = 0; $i < $length; $i++) {
194 0           CPAN::Shell->install(@{$install}[$i]);
  0            
195             }
196             }
197             }
198              
199 0     0     sub _setup_database($self, $app) {
  0            
  0            
  0            
200              
201 0 0         if($app->config('migrations')) {
202 0           try {
203 0           Daje::Database::Migrator->new(
204             pg => $app->pg,
205             migrations => $app->config('migrations'),
206             )->migrate();
207             }
208             catch($e) {
209 0           $app->log->error($e);
210             };
211             }
212             }
213              
214 0     0     sub _plugin_options($self, $app, $name, $options) {
  0            
  0            
  0            
  0            
  0            
215              
216 0           my %plugin = ();
217 0           my %result = ();
218 0           try {
219 0           my $length = scalar @{$options};
  0            
220 0           for (my $i = 0; $i < $length; $i++) {
221 0           my $plug = @{$options}[$i]->{name};
  0            
222 0           my $data = @{$options}[$i]->{option};
  0            
223 0           %plugin = ($plug => $data );
224             }
225             } catch($e) {
226 0           say "_plugin_options " . $e;
227             };
228              
229 0           $result{$name} = \%plugin;
230 0           return \%plugin;;
231             }
232             1;
233             __END__