lib/Mojolicious/Plugin/Fondation/Command/fondation.pm

filter: Statementstmt 48.4% 61 / 126 Branchbran 21.4% 9 / 42 Conditioncond 43.4% 10 / 23 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 52.6% 10 / 19 Podpod 100% 1 / 1 totaltotal 41.7% 93 / 223 CC 46 SCAR 33.9
CC
46
Cov
42%
run
110.0
_run_plan
56.0
_show_plan_init
20.0
CRAP 29.7
« lib/Mojolicious/Plugin/Fondation/API.pm ↑ Summary blib/lib/Mojolicious/Plugin/Fondation/Controller/Welcome.pm »
1 package Mojolicious::Plugin::Fondation::Command::fondation;
2 1 $Mojolicious::Plugin::Fondation::Command::fondation::VERSION = '0.08';
3 # ABSTRACT: Fondation orchestration commands -- init, upgrade, clean, refresh
4  
5 1 ▶use Mojo::Base 'Mojolicious::Command', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has description => 'Orchestrate Fondation plugins: init, upgrade, clean, refresh';
12 0 ▶has usage => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 <<"USAGE";
14 Usage: APPLICATION fondation COMMAND [OPTIONS]
15  
16 myapp.pl db bootstrap-schema Create the schema class (run once)
17 myapp.pl fondation plan init Preview init steps (dry-run)
18 myapp.pl fondation plan upgrade Preview upgrade steps (dry-run)
19 myapp.pl fondation plan clean Preview clean steps (dry-run)
20 myapp.pl fondation plan refresh Preview refresh steps (dry-run)
21 myapp.pl fondation init First-time setup for all plugins
22 myapp.pl fondation upgrade Detect drift, upgrade, regenerate
23 myapp.pl fondation clean Clean all generated artifacts
24 myapp.pl fondation refresh Clean all generated artifacts and regenerate
25  
26 USAGE
27 1 };
28  
29 # ---------------------------------------------------------------------------
30 # Entry point
31 # ---------------------------------------------------------------------------
32  
33 0 ▶sub run ($self, @args) {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
34 0 my $app = $self->app;
35 0 ▶ my $subcommand = shift @args || '';
Condition0%0/2
shift @args || ''
l!l
00
MC/DC0%0/1
shift @args || ''
shift @args
Truth table0%0/2
A
A= shift @args
Aresult
11
00
36  
37 0 ▶ die $self->usage unless $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless $subcommand 0 0 0
38  
39 0 for ($subcommand) {
40 0 ▶ /^plan$/ and return $self->_run_plan($app, shift @args || '');
Branch: 1 branch
BranchTrueFalseTotal
/^plan$/u and return $self->_run_plan($app, shift @args || "") 0 0 0
Condition0%0/2
shift @args || ''
l!l
00
MC/DC0%0/1
shift @args || ''
shift @args
Truth table0%0/2
A
A= shift @args
Aresult
11
00
41 0 ▶ /^init$/ and return $self->_run_init($app);
Branch: 1 branch
BranchTrueFalseTotal
/^init$/u and return $self->_run_init($app) 0 0 0
42 0 ▶ /^upgrade$/ and return $self->_run_upgrade($app);
Branch: 1 branch
BranchTrueFalseTotal
/^upgrade$/u and return $self->_run_upgrade($app) 0 0 0
43 0 ▶ /^clean$/ and return $self->_run_clean($app);
Branch: 1 branch
BranchTrueFalseTotal
/^clean$/u and return $self->_run_clean($app) 0 0 0
44 0 ▶ /^refresh$/ and return $self->_run_refresh($app);
Branch: 1 branch
BranchTrueFalseTotal
/^refresh$/u and return $self->_run_refresh($app) 0 0 0
45 0 die $self->usage;
46 }
47 }
48  
49 # ---------------------------------------------------------------------------
50 # Run a list of steps through $app->commands->run()
51 # ---------------------------------------------------------------------------
52  
53 2 ▶sub _run_steps ($self, $app, $step_name, $long_name, $steps) {
Subroutine _run_steps: called CC=3 SCAR=11.2
54 2 for my $step (@$steps) {
55 4 ▶ my @cmd = ref $step eq 'ARRAY' ? @$step : ($step);
Branch: 1 branch
BranchTrueFalseTotal
ref $step eq 'ARRAY' ? : 4 0 4
56 4 $app->commands->run(@cmd);
57 }
58 }
59  
60 # ---------------------------------------------------------------------------
61 # Collect steps from all plugins in load order
62 # ---------------------------------------------------------------------------
63  
64 5 ▶sub _collect_steps ($self, $app, $key) {
Subroutine _collect_steps: called CC=6 SCAR=18.2
65 5 my @steps;
66 5 for my $long (@{ $app->manager->load_order }) {
67 19 my $entry = $app->manager->registry->{$long};
68 19 ▶ my $cfg = $entry->{config} // {};
Condition50%1/2
$entry->{"config"} // {}
l!l
190
MC/DC0%0/1
$entry->{"config"} // {}
$entry->{"config"}
Truth table50%1/2
A
A= $entry->{"config"}
Aresult
11
00
69 19 my $plugin_steps = $cfg->{$key};
70 19 ▶ next unless $plugin_steps && ref $plugin_steps eq 'ARRAY' && @$plugin_steps;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_steps and ref $plugin_steps eq "ARRAY" and @$plugin_steps 14 5 19
Condition66%4/6
$plugin_steps and ref $plugin_steps eq "ARRAY" and @$plugin_steps
!ll&&!rl&&r
1405
$plugin_steps and ref $plugin_steps eq "ARRAY"
!ll&&!rl&&r
1405
MC/DC33%1/3
$plugin_steps and ref $plugin_steps eq "ARRAY" and @$plugin_steps
$plugin_steps ref $plugin_steps eq "ARRAY" @$plugin_steps
Truth table50%2/4
A and B and C
A= $plugin_stepsB= ref $plugin_steps eq "ARRAY"C= @$plugin_steps
ABCresult
0XX0
10X0
1100
1111
71 5 push @steps, { long_name => $long, steps => $plugin_steps };
72 }
73 5 return @steps;
74 }
75  
76 # ---------------------------------------------------------------------------
77 # Collect clean targets from all plugins in load order
78 # ---------------------------------------------------------------------------
79  
80 2 ▶sub _collect_clean ($self, $app) {
Subroutine _collect_clean: called CC=6 SCAR=18.2
81 2 my @targets;
82 2 for my $long (@{ $app->manager->load_order }) {
83 7 my $entry = $app->manager->registry->{$long};
84 7 ▶ my $cfg = $entry->{config} // {};
Condition50%1/2
$entry->{"config"} // {}
l!l
70
MC/DC0%0/1
$entry->{"config"} // {}
$entry->{"config"}
Truth table50%1/2
A
A= $entry->{"config"}
Aresult
11
00
85 7 my $plugin_clean = $cfg->{fondation_clean};
86 7 ▶ next unless $plugin_clean && ref $plugin_clean eq 'ARRAY' && @$plugin_clean;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_clean and ref $plugin_clean eq "ARRAY" and @$plugin_clean 5 2 7
Condition66%4/6
$plugin_clean and ref $plugin_clean eq "ARRAY" and @$plugin_clean
!ll&&!rl&&r
502
$plugin_clean and ref $plugin_clean eq "ARRAY"
!ll&&!rl&&r
502
MC/DC33%1/3
$plugin_clean and ref $plugin_clean eq "ARRAY" and @$plugin_clean
$plugin_clean ref $plugin_clean eq "ARRAY" @$plugin_clean
Truth table50%2/4
A and B and C
A= $plugin_cleanB= ref $plugin_clean eq "ARRAY"C= @$plugin_clean
ABCresult
0XX0
10X0
1100
1111
87 2 push @targets, { long_name => $long, targets => $plugin_clean };
88 }
89 2 return @targets;
90 }
91  
92 # ---------------------------------------------------------------------------
93 # fondation plan (dry-run preview)
94 # ---------------------------------------------------------------------------
95  
96 0 ▶sub _run_plan ($self, $app, $subcommand) {
Subroutine _run_plan: not called CC=7 SCAR=40.3
97 0 ▶ die $self->usage unless $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless $subcommand 0 0 0
98  
99 0 for ($subcommand) {
100 0 ▶ /^init$/ and return $self->_show_plan_init($app);
Branch: 1 branch
BranchTrueFalseTotal
/^init$/u and return $self->_show_plan_init($app) 0 0 0
101 0 ▶ /^upgrade$/ and return $self->_show_plan_upgrade($app);
Branch: 1 branch
BranchTrueFalseTotal
/^upgrade$/u and return $self->_show_plan_upgrade($app) 0 0 0
102 0 ▶ /^clean$/ and return $self->_show_plan_clean($app);
Branch: 1 branch
BranchTrueFalseTotal
/^clean$/u and return $self->_show_plan_clean($app) 0 0 0
103 0 ▶ /^refresh$/ and return $self->_show_plan_refresh($app);
Branch: 1 branch
BranchTrueFalseTotal
/^refresh$/u and return $self->_show_plan_refresh($app) 0 0 0
104 0 die $self->usage;
105 }
106 }
107  
108 0 ▶sub _short_name ($self, $app, $long) {
Subroutine _short_name: not called CC=2 SCAR=17.9
109 0 ▶ return $app->manager->registry->{$long}{short_name} // $long;
Condition0%0/3
$app->manager->registry->{$long}{"short_name"} // $long
l!l&&r!l&&!r
000
MC/DC0%0/2
$app->manager->registry->{$long}{"short_name"} // $long
$app->manager->registry->{$long}{"short_name"} $long
Truth table0%0/3
A // B
A= $app->manager->registry->{$long}{"short_name"}B= $long
ABresult
1X1
011
000
110 }
111  
112 0 ▶sub _show_plan_init ($self, $app) {
Subroutine _show_plan_init: not called CC=4 SCAR=30.0
113 0 my @plugins = $self->_collect_steps($app, 'fondation_init');
114 0 my $total_steps = 0;
115  
116 0 for my $p (@plugins) {
117 0 my $short = $self->_short_name($app, $p->{long_name});
118 0 say "-- $short";
119 0 for my $step (@{ $p->{steps} }) {
120 0 ▶ my @cmd = ref $step eq 'ARRAY' ? @$step : ($step);
Branch: 1 branch
BranchTrueFalseTotal
ref $step eq 'ARRAY' ? : 0 0 0
121 0 say " [run] @cmd";
122 0 $total_steps++;
123 }
124 }
125  
126 0 say "-- Init plan: " . scalar(@plugins) . " plugins, $total_steps steps --";
127 }
128  
129 0 ▶sub _show_plan_upgrade ($self, $app) {
Subroutine _show_plan_upgrade: not called CC=4 SCAR=30.0
130 0 my @plugins = $self->_collect_steps($app, 'fondation_upgrade');
131 0 my $total_steps = 0;
132  
133 0 for my $p (@plugins) {
134 0 my $short = $self->_short_name($app, $p->{long_name});
135 0 say "-- $short";
136 0 for my $step (@{ $p->{steps} }) {
137 0 ▶ my @cmd = ref $step eq 'ARRAY' ? @$step : ($step);
Branch: 1 branch
BranchTrueFalseTotal
ref $step eq 'ARRAY' ? : 0 0 0
138 0 say " [run] @cmd";
139 0 $total_steps++;
140 }
141 }
142  
143 0 say "-- Upgrade plan: " . scalar(@plugins) . " plugins, $total_steps steps --";
144 }
145  
146 0 ▶sub _show_plan_clean ($self, $app) {
Subroutine _show_plan_clean: not called CC=4 SCAR=30.0
147 0 my @clean = $self->_collect_clean($app);
148  
149 0 ▶ unless (@clean) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@clean) 0 0 0
150 0 say "-- Nothing to clean --";
151 0 return;
152 }
153  
154 0 say "-- Clean phase --";
155 0 my $total_targets = 0;
156 0 for my $p (@clean) {
157 0 my $short = $self->_short_name($app, $p->{long_name});
158 0 for my $target (@{ $p->{targets} }) {
159 0 say " [$short] remove $target";
160 0 $total_targets++;
161 }
162 }
163  
164 0 say "-- Clean plan: " . scalar(@clean) . " plugins, $total_targets targets --";
165 }
166  
167 0 ▶sub _show_plan_refresh ($self, $app) {
Subroutine _show_plan_refresh: not called CC=2 SCAR=17.9
168 0 $self->_show_plan_clean($app);
169 0 ▶ say '' if @{ [ $self->_collect_clean($app) ] };
Branch: 1 branch
BranchTrueFalseTotal
if @{[$self->_collect_clean($app)];} 0 0 0
170 0 $self->_show_plan_init($app);
171 0 say "-- Refresh plan --";
172 }
173  
174 2 ▶sub _run_init ($self, $app) {
Subroutine _run_init: called CC=2 SCAR=6.9
175 2 my @plugins = $self->_collect_steps($app, 'fondation_init');
176  
177 2 for my $plugin (@plugins) {
178 2 my $short = $app->manager->registry->{$plugin->{long_name}}{short_name};
179 2 say "-- $short";
180 2 $self->_run_steps($app, 'fondation_init', $plugin->{long_name}, $plugin->{steps});
181 }
182  
183 2 say "-- Init complete --";
184 }
185  
186 # ---------------------------------------------------------------------------
187 # fondation upgrade
188 # ---------------------------------------------------------------------------
189  
190 0 ▶sub _run_upgrade ($self, $app) {
Subroutine _run_upgrade: not called CC=2 SCAR=17.9
191 0 my @plugins = $self->_collect_steps($app, 'fondation_upgrade');
192  
193 0 for my $plugin (@plugins) {
194 0 my $short = $app->manager->registry->{$plugin->{long_name}}{short_name};
195 0 say "-- $short";
196 0 $self->_run_steps($app, 'fondation_upgrade', $plugin->{long_name}, $plugin->{steps});
197 }
198  
199 0 say "-- Upgrade complete --";
200 }
201  
202 # ---------------------------------------------------------------------------
203 # fondation clean
204 # ---------------------------------------------------------------------------
205  
206 1 ▶sub _run_clean ($self, $app) {
Subroutine _run_clean: called CC=6 SCAR=18.0
207 1 my $home = $app->home;
208 1 my @clean = $self->_collect_clean($app);
209  
210 1 ▶ return say "-- Nothing to clean --" unless @clean;
Branch: 1 branch
BranchTrueFalseTotal
unless @clean 0 1 1
211  
212 1 say "-- Cleaning generated artifacts --";
213  
214 1 for my $plugin (@clean) {
215 1 my $short = $app->manager->registry->{$plugin->{long_name}}{short_name};
216 1 for my $target (@{ $plugin->{targets} }) {
217 2 my $path = path($home, $target);
218  
219 2 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 1 1 2
elsif (-f $path) { } 1 0 1
220 1 say " [$short] Removing $target";
221 1 remove_tree($path->to_string, { safe => 0 });
222 }
223 elsif (-f $path) {
224 1 say " [$short] Removing $target";
225 1 unlink $path->to_string;
226 }
227 }
228 }
229  
230 1 say "-- Clean complete --";
231 }
232  
233 # ---------------------------------------------------------------------------
234 # fondation refresh
235 # ---------------------------------------------------------------------------
236  
237 1 ▶sub _run_refresh ($self, $app) {
Subroutine _run_refresh: called CC=1 SCAR=0.0
238 1 $self->_run_clean($app);
239 1 $self->_run_init($app);
240 1 say "-- Refresh complete --";
241 }
242  
243 1 1;
244  
245 __END__
« lib/Mojolicious/Plugin/Fondation/API.pm ↑ Summary blib/lib/Mojolicious/Plugin/Fondation/Controller/Welcome.pm »