blib/lib/Venus/Run.pm

filter: Statementstmt 85.0% 251 / 295 Branchbran 68.8% 84 / 122 Conditioncond 72.2% 60 / 83 MC/DCmcdc 41.8% 18 / 43 Subroutinesub 86.5% 45 / 52 Podpod 13.1% 5 / 38 totaltotal 73.1% 463 / 633 CC 99 SCAR 20.5
CC
99
Cov
79%
resolve_recursive
24.5
resolve_func
13.9
resolve_cache
12.0
CRAP 7.8
« blib/lib/Venus/Prototype.pm ↑ Summary blib/lib/Venus/Validate.pm »
1 package Venus::Run;
2  
3 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # IMPORTS
11  
12 3 ▶use Venus;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Venus::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Venus::Os;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Venus::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # INHERITS
18  
19 3 base 'Venus::Kind::Utility';
20  
21 # INTEGRATES
22  
23 3 with 'Venus::Role::Optional';
24  
25 # STATE
26  
27 3 state $init_config = {
28 data => {
29 VENUS_RUN_DEBUG => 1,
30 },
31 exec => {
32 brew => 'perlbrew',
33 cpan => 'cpanm -llocal -qn',
34 docs => 'perldoc',
35 each => 'shim -nE',
36 edit => '$EDITOR $VENUS_FILE',
37 eval => 'shim -E',
38 exec => '$PERL',
39 info => '$PERL -V',
40 lint => 'perlcritic',
41 okay => '$PERL -c',
42 repl => '$REPL',
43 reup => 'cpanm -qn Venus',
44 says => 'eval "map log(eval), @ARGV"',
45 shim => '$PERL -MVenus=true,false,log',
46 test => '$PROVE',
47 tidy => 'perltidy',
48 },
49 libs => [
50 '-Ilib',
51 '-Ilocal/lib/perl5',
52 ],
53 path => [
54 'bin',
55 'dev',
56 'local/bin',
57 ],
58 perl => {
59 perl => 'perl',
60 prove => 'prove',
61 },
62 vars => {
63 PERL => 'perl',
64 PROVE => 'prove',
65 REPL => '$PERL -dE0',
66 },
67 };
68  
69 # ATTRIBUTES
70  
71 3 attr 'cache';
72 3 attr 'config';
73 3 attr 'debug';
74 3 attr 'handler';
75  
76 # BUILDERS
77  
78 0 ▶sub build_arg {
Subroutine build_arg: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my ($self, $file) = @_;
80  
81 return {
82 0 config => $self->load_config($file),
83 };
84 }
85  
86 49 ▶sub build_self {
Subroutine build_self: called CC=3 SCAR=11.4
Pod undocumented
87 49 my ($self, $data) = @_;
88  
89 49 ▶ $self->debug($ENV{VENUS_DEBUG}) if defined $ENV{VENUS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"VENUS_DEBUG"} 0 49 49
90  
91 49 ▶ $self->debug($ENV{VENUS_RUN_DEBUG}) if defined $ENV{VENUS_RUN_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"VENUS_RUN_DEBUG"} 0 49 49
92  
93 49 return $self;
94 }
95  
96 49 ▶sub default_cache {
Subroutine default_cache: called CC=1 SCAR=0.0
Pod undocumented
97 49 my ($self, $data) = @_;
98  
99 49 return $self->new_cache;
100 }
101  
102 34 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod undocumented
103 34 my ($self, $data) = @_;
104  
105 34 return $self->new_config;
106 }
107  
108 47 ▶sub default_debug {
Subroutine default_debug: called CC=1 SCAR=0.0
Pod undocumented
109 47 my ($self, $data) = @_;
110  
111 47 return false;
112 }
113  
114 48 ▶sub default_handler {
Subroutine default_handler: called CC=1 SCAR=0.0
Pod undocumented
115 48 my ($self, $data) = @_;
116  
117 48 return $self->new_handler;
118 }
119  
120 # HOOKS
121  
122 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
123 0 do {local $| = 1; CORE::print(STDERR @_, "\n")}
124 }
125  
126 0 ▶sub _print {
Subroutine _print: not called CC=1 SCAR=6.9
127 0 do {local $| = 1; CORE::print(@_, "\n")}
128 }
129  
130 0 ▶sub _prompt {
Subroutine _prompt: not called CC=1 SCAR=6.9
131 0 do {local $\ = ''; local $_ = <STDIN>; chomp; $_}
132 }
133  
134 0 ▶sub _system {
Subroutine _system: not called CC=2 SCAR=17.9
135 0 ▶ local $SIG{__WARN__} = sub {}; CORE::system(@_) == 0 ? true : false;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
system(@_) == 0 ? : 0 0 0
136 }
137  
138 # METHODS
139  
140 14 ▶sub callback {
Subroutine callback: called CC=6 SCAR=18.3
Pod documented
141 14 my ($self, $code, @args) = @_;
142  
143 14 ▶ if (!@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 4 10 14
144 4 return;
145 }
146  
147 10 my $config = $self->prepare_conf($self->config);
148  
149 10 my %ORIG_ENV = %ENV;
150  
151 10 $self->prepare_vars($config);
152 10 $self->prepare_path($config);
153 10 $self->prepare_libs($config);
154  
155 10 my $result;
156  
157 10 for my $step ($self->resolve($config, @args)) {
158 10 ▶ my ($prog, @step_args) = ref $step eq 'ARRAY' ? (@{$step}) : ($step);
Branch: 1 branch
BranchTrueFalseTotal
ref $step eq 'ARRAY' ? : 10 0 10
159  
160 10 ▶ if (!$prog) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prog) 0 10 10
161 0 next;
162 }
163  
164 10 $result = $code->($prog, @step_args);
165  
166 10 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 10 10
167 0 last;
168 }
169 }
170  
171 10 %ENV = %ORIG_ENV;
172  
173 10 return $result;
174 }
175  
176 12 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
177 12 my ($self, @args) = @_;
178  
179 12 return $self->callback($self->handler, @args);
180 }
181  
182 53 ▶sub expand_prog {
Subroutine expand_prog: called CC=1 SCAR=0.0
Pod undocumented
183 53 my ($self, $prog) = @_;
184  
185 53 return Venus::Os->which($prog);
186 }
187  
188 98 ▶sub expand_vars {
Subroutine expand_vars: called CC=3 SCAR=11.7
Pod undocumented
189 98 my ($self, $text) = @_;
190  
191 98 ▶ return ($text // '') =~ s{\$([A-Z_]+)}{$ENV{$1} // "\$" . $1}egr;
Condition50%2/4
$text // ""
l!l
980
$ENV{$1} // '$' . $1
l!l
100
MC/DC0%0/1
$text // ""
$text
MC/DC0%0/1
$ENV{$1} // '$' . $1
$ENV{$1}
Truth table50%1/2
A
A= $text
Aresult
11
00
Truth table50%1/2
A
A= $ENV{$1}
Aresult
11
00
192 }
193  
194 18 ▶sub prepare_conf {
Subroutine prepare_conf: called CC=6 SCAR=18.0
Pod undocumented
195 18 my ($self, $config) = @_;
196  
197 18 ▶ if (my $from = $config->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = $config->{"from"}) 1 17 18
198 1 ▶ my @files = ref($from) eq 'ARRAY' ? @{$from} : ($from);
Branch: 1 branch
BranchTrueFalseTotal
ref $from eq 'ARRAY' ? : 1 0 1
199  
200 1 for my $file (@files) {
201 1 $config = Venus::merge(
202 $self->load_config($self->expand_vars($file)), $config,
203 );
204 }
205 }
206  
207 18 ▶ if (my $when = $config->{when}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $when = $config->{"when"}) 2 16 18
208 2 my $os_type = Venus::Os->type;
209  
210 2 ▶ if ($when->{$os_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when->{$os_type}) 2 0 2
211 2 $config = Venus::merge($config, $when->{$os_type});
212 }
213 }
214  
215 18 return $config;
216 }
217  
218 10 ▶sub prepare_libs {
Subroutine prepare_libs: called CC=3 SCAR=11.1
Pod undocumented
219 10 my ($self, $config) = @_;
220  
221 10 ▶ my $lib_paths = $config->{libs} // [];
Condition50%1/2
$config->{"libs"} // []
l!l
100
MC/DC0%0/1
$config->{"libs"} // []
$config->{"libs"}
Truth table50%1/2
A
A= $config->{"libs"}
Aresult
11
00
222  
223 10 my %seen_paths;
224  
225 20 my @absolute_paths = map {Venus::Path->new($self->expand_vars($_))->absolute}
226 10 map /^-I\w*?(.*)$/, @{$lib_paths};
227  
228 10 ▶ my $separator = Venus::Os->is_win ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
'Venus::Os'->is_win ? : 0 10 10
229  
230 10 $ENV{PERL5LIB} = join $separator, grep {!$seen_paths{$_}++} @absolute_paths;
231  
232 10 return $config;
233 }
234  
235 10 ▶sub prepare_path {
Subroutine prepare_path: called CC=3 SCAR=11.0
Pod undocumented
236 10 my ($self, $config) = @_;
237  
238 10 ▶ if (!$config->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"path"}) 3 7 10
239 3 return $config;
240 }
241  
242 7 ▶ my $separator = Venus::Os->is_win ? ';' : ':';
Branch: 1 branch
BranchTrueFalseTotal
'Venus::Os'->is_win ? : 0 7 7
243  
244 21 my @absolute_paths = map {Venus::Path->new($self->expand_vars($_))->absolute}
245 7 @{$config->{path}};
246  
247 7 $ENV{PATH} = join $separator, @absolute_paths, $ENV{PATH};
248  
249 7 return $config;
250 }
251  
252 13 ▶sub prepare_vars {
Subroutine prepare_vars: called CC=8 SCAR=20.8
Pod undocumented
253 13 my ($self, $config) = @_;
254  
255 13 ▶ if (my $data = $config->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $config->{"data"}) 10 3 13
256 10 for my $key (sort keys %$data) {
257 12 $ENV{$key} = join ' ', grep defined, $data->{$key};
258 }
259 }
260  
261 13 ▶ if (my $vars = $config->{vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vars = $config->{"vars"}) 8 5 13
262 8 for my $key (sort keys %$vars) {
263 $ENV{$key} = join ' ', grep defined,
264 23 @{$self->resolve_command($config, $vars->{$key})};
265 }
266 }
267  
268 13 ▶ if (my $asks = $config->{asks}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $asks = $config->{"asks"}) 1 12 13
269 1 for my $key (sort keys %$asks) {
270 1 ▶ if (!defined $ENV{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{$key}) 1 0 1
271 1 _print $asks->{$key}; my $input = _prompt;
272 1 $ENV{$key} = $input;
273 }
274 }
275 }
276  
277 13 return $config;
278 }
279  
280 34 ▶sub resolve {
Subroutine resolve: called CC=8 SCAR=20.8
Pod documented
281 34 my ($self, $config, @args) = @_;
282  
283 34 ▶ if (!@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 1 33 34
284 1 return [];
285 }
286  
287 33 my $item = shift @args;
288  
289 33 my @resolved_with;
290  
291 33 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 25 33
292 8 @resolved_with = $self->resolve_with($config, $item, @args);
293 }
294  
295 33 my @resolved;
296  
297 33 ▶ if (!@resolved_with) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@resolved_with) 31 2 33
298 31 @resolved = $self->resolve_recursive($config, $item);
299 }
300  
301 33 my @resolved_flow;
302  
303 33 ▶ if (@resolved) {
Branch: 1 branch
BranchTrueFalseTotal
if (@resolved) 31 2 33
304 31 @resolved_flow = $self->resolve_flow($config, @resolved, @args);
305 }
306  
307 33 my $results;
308  
309 33 ▶ if (@resolved_with) {
Branch: 2 branches
BranchTrueFalseTotal
if (@resolved_with) { } 2 31 33
elsif (@resolved_flow) { } 1 30 31
310 2 $results = [@resolved_with];
311 }
312 elsif (@resolved_flow) {
313 1 $results = [@resolved_flow];
314 }
315 else {
316 30 $results = [scalar $self->resolve_command($config, @resolved, @args)];
317 }
318  
319 33 ▶ return wantarray ? (@{$results}) : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 21 33
320 }
321  
322 0 ▶sub resolve_cache {
Subroutine resolve_cache: not called CC=3 SCAR=24.8
Pod undocumented
323 0 my ($self, $config, $item) = @_;
324  
325 0 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) { } 0 0 0
326 return ref $self->cache->{$item}
327 ? $self->cache->{$item}
328 0 ▶ : $self->resolve_cache($config, $self->cache->{$item});
Branch: 1 branch
BranchTrueFalseTotal
ref $self->cache->{$item} ? : 0 0 0
329 }
330 else {
331 0 return;
332 }
333 }
334  
335 53 ▶sub resolve_command {
Subroutine resolve_command: called CC=8 SCAR=22.1
Pod undocumented
336 53 my ($self, $config, @args) = @_;
337  
338 53 my ($prog, @rest) = (@args);
339  
340 53 my $path = $self->expand_prog($prog);
341  
342 53 ▶ my @parts = grep defined, ($path || $prog), @rest;
Condition66%2/3
$path || $prog
l!l&&r!l&&!r
41120
MC/DC0%0/2
$path || $prog
$path $prog
Truth table66%2/3
A || B
A= $pathB= $prog
ABresult
1X1
011
000
343  
344 53 $prog = $self->expand_vars(shift @parts);
345  
346 53 for my $arg (@parts) {
347 70 ▶ if ($arg =~ /^(?:\|+|\&+|[<>]+|\d[<>&]+\d?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^(?:\|+|\&+|[<>]+|\d[<>&]+\d?)$/u) 3 67 70
348 3 next;
349 }
350  
351 67 ▶ if ($arg =~ /^\$[A-Z]\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\$[A-Z]\w+$/u) 3 64 67
352 3 $arg = $self->expand_vars($arg);
353 }
354  
355 67 ▶ if ($arg =~ /^\$\((.*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\$\((.*)\)$/u) 0 67 67
356 0 $arg = "\$(@{[@{$self->prep($config, $1)}]})";
357 }
358  
359 67 $arg = Venus::Os->quote($arg);
360 }
361  
362 53 ▶ my $results = [$prog ? ($prog, @parts) : ()];
Branch: 1 branch
BranchTrueFalseTotal
$prog ? : 53 0 53
363  
364 0 ▶ return wantarray ? (@{$results}) : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 53 53
365 }
366  
367 98 ▶sub resolve_exec {
Subroutine resolve_exec: called CC=4 SCAR=14.0
Pod undocumented
368 98 my ($self, $config, $item) = @_;
369  
370 98 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 98 98
371 0 return $self->cache->{$item};
372 }
373  
374 98 ▶ if (!($config->{exec} && exists $config->{exec}->{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"exec"} and exists $config->{"exec"}{$item}) 73 25 98
Condition100%3/3
$config->{"exec"} and exists $config->{"exec"}{$item}
!ll&&!rl&&r
185525
MC/DC100%2/2
$config->{"exec"} and exists $config->{"exec"}{$item}
$config->{"exec"} exists $config->{"exec"}{$item}
Truth table100%3/3
A and B
A= $config->{"exec"}B= exists $config->{"exec"}{$item}
ABresult
0X0
100
111
375 73 return;
376 }
377  
378 25 return $self->cache->{$item} = $config->{exec}->{$item};
379 }
380  
381 69 ▶sub resolve_find {
Subroutine resolve_find: called CC=4 SCAR=14.0
Pod undocumented
382 69 my ($self, $config, $item) = @_;
383  
384 69 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 69 69
385 0 return $self->cache->{$item};
386 }
387  
388 69 ▶ if (!($config->{find} && exists $config->{find}->{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"find"} and exists $config->{"find"}{$item}) 68 1 69
Condition100%3/3
$config->{"find"} and exists $config->{"find"}{$item}
!ll&&!rl&&r
6621
MC/DC100%2/2
$config->{"find"} and exists $config->{"find"}{$item}
$config->{"find"} exists $config->{"find"}{$item}
Truth table100%3/3
A and B
A= $config->{"find"}B= exists $config->{"find"}{$item}
ABresult
0X0
100
111
389 68 return;
390 }
391  
392 1 return $self->cache->{$item} = $config->{find}->{$item};
393 }
394  
395 31 ▶sub resolve_flow {
Subroutine resolve_flow: called CC=4 SCAR=13.9
Pod undocumented
396 31 my ($self, $config, $item, @args) = @_;
397  
398 31 ▶ if (!($config->{flow} && $config->{flow}{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"flow"} and $config->{"flow"}{$item}) 30 1 31
Condition100%3/3
$config->{"flow"} and $config->{"flow"}{$item}
!ll&&!rl&&r
2821
MC/DC100%2/2
$config->{"flow"} and $config->{"flow"}{$item}
$config->{"flow"} $config->{"flow"}{$item}
Truth table100%3/3
A and B
A= $config->{"flow"}B= $config->{"flow"}{$item}
ABresult
0X0
100
111
399 30 return;
400 }
401  
402 1 my @results = map {$self->resolve($config, $_, @args)} @{$config->{flow}{$item}};
403  
404 1 ▶ return wantarray ? (@results) : [@results];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
405 }
406  
407 73 ▶sub resolve_func {
Subroutine resolve_func: called CC=5 SCAR=26.3
Pod undocumented
408 73 my ($self, $config, $item, @args) = @_;
409  
410 73 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 73 73
411 0 return $self->cache->{$item};
412 }
413  
414 73 ▶ if (!($config->{func} && exists $config->{func}{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"func"} and exists $config->{"func"}{$item}) 73 0 73
Condition33%1/3
$config->{"func"} and exists $config->{"func"}{$item}
!ll&&!rl&&r
7300
MC/DC0%0/2
$config->{"func"} and exists $config->{"func"}{$item}
$config->{"func"} exists $config->{"func"}{$item}
Truth table33%1/3
A and B
A= $config->{"func"}B= exists $config->{"func"}{$item}
ABresult
0X0
100
111
415 73 return;
416 }
417  
418 0 my $from = Venus::Path->new($self->file);
419  
420 0 $ENV{VENUS_RUN_FROM_ROOT} = $from->parent->absolute->get;
421 0 $ENV{VENUS_RUN_FROM_FILE} = $from->absolute->get;
422  
423 0 my $func = Venus::Path->new($config->{func}->{$item});
424  
425 0 $ENV{VENUS_RUN_FUNC_ROOT} = $func->parent->absolute->get;
426 0 $ENV{VENUS_RUN_FUNC_FILE} = $func->absolute->get;
427  
428 0 my $run = $self->from_file("$func");
429  
430 0 my $results = $run->resolve($run->config, @args);
431  
432 0 ▶ return wantarray ? (@{$results}) : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
433 }
434  
435 68 ▶sub resolve_perl {
Subroutine resolve_perl: called CC=6 SCAR=18.0
Pod undocumented
436 68 my ($self, $config, $item) = @_;
437  
438 68 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 68 68
439 0 return $self->cache->{$item};
440 }
441  
442 68 ▶ if (!($config->{perl} && exists $config->{perl}{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"perl"} and exists $config->{"perl"}{$item}) 57 11 68
Condition100%3/3
$config->{"perl"} and exists $config->{"perl"}{$item}
!ll&&!rl&&r
273011
MC/DC100%2/2
$config->{"perl"} and exists $config->{"perl"}{$item}
$config->{"perl"} exists $config->{"perl"}{$item}
Truth table100%3/3
A and B
A= $config->{"perl"}B= exists $config->{"perl"}{$item}
ABresult
0X0
100
111
443 57 return;
444 }
445  
446 11 my $perl = $config->{perl}{$item};
447 11 ▶ my @libs = map {$self->cache->{$_} = $_} @{$config->{libs} // []};
Condition50%1/2
$config->{"libs"} // []
l!l
110
MC/DC0%0/1
$config->{"libs"} // []
$config->{"libs"}
Truth table50%1/2
A
A= $config->{"libs"}
Aresult
11
00
448 11 ▶ my @load = map {$self->cache->{$_} = $_} @{$config->{load} // []};
Condition100%2/2
$config->{"load"} // []
l!l
29
MC/DC100%1/1
$config->{"load"} // []
$config->{"load"}
Truth table100%2/2
A
A= $config->{"load"}
Aresult
11
00
449  
450 11 my $result = join ' ', $perl, @libs, @load;
451  
452 11 $self->cache->{$item} = $self->cache->{$result} = [$perl, @libs, @load];
453  
454 11 return $result;
455 }
456  
457 73 ▶sub resolve_recursive {
Subroutine resolve_recursive: called CC=21 SCAR=32.0
Pod undocumented
458 73 my ($self, $config, $item) = @_;
459  
460 73 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 12 61 73
461 return ref $self->cache->{$item} eq 'ARRAY'
462 12 ? (wantarray ? (@{$self->cache->{$item}}) : $self->cache->{$item})
463 12 ▶ : $self->cache->{$item};
Branch: 2 branches
BranchTrueFalseTotal
ref $self->cache->{$item} eq 'ARRAY' ? : 12 0 12
wantarray ? : 12 0 12
464 }
465  
466 61 ▶ my @parts = grep length, ($item // '') =~ /(?x)(?:"([^"]*)"|([^\s]*))\s?/g;
Condition50%1/2
$item // ""
l!l
610
MC/DC0%0/1
$item // ""
$item
Truth table50%1/2
A
A= $item
Aresult
11
00
467  
468 61 my @results;
469  
470 61 for my $part (@parts) {
471 99 ▶ my $cached = exists $self->cache->{$part} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->cache->{$part} ? : 1 98 99
472 my $recurse = $cached
473 99 ▶ ? (ref $self->cache->{$part} ? 0 : $self->cache->{$self->cache->{$part}})
Branch: 2 branches
BranchTrueFalseTotal
$cached ? : 1 98 99
ref $self->cache->{$part} ? : 0 1 1
474 : 0;
475 my $resolved
476 99 ▶ = $cached ? $self->cache->{$part} : $self->resolve_exec($config, $part)
Branch: 1 branch
BranchTrueFalseTotal
$cached ? : 1 98 99
Condition86%13/15
(((($self->resolve_exec($config, $part) // $self->resolve_func($config, $part)) // $self->resolve_task($config, $part)) // $self->resolve_vars($config, $part)) // $self->resolve_find($config, $part)) // $self->resolve_perl($config, $part)
l!l&&r!l&&!r
301157
((($self->resolve_exec($config, $part) // $self->resolve_func($config, $part)) // $self->resolve_task($config, $part)) // $self->resolve_vars($config, $part)) // $self->resolve_find($config, $part)
l!l&&r!l&&!r
29168
(($self->resolve_exec($config, $part) // $self->resolve_func($config, $part)) // $self->resolve_task($config, $part)) // $self->resolve_vars($config, $part)
l!l&&r!l&&!r
25469
($self->resolve_exec($config, $part) // $self->resolve_func($config, $part)) // $self->resolve_task($config, $part)
l!l&&r!l&&!r
25073
$self->resolve_exec($config, $part) // $self->resolve_func($config, $part)
l!l&&r!l&&!r
25073
MC/DC66%4/6
(((($self->resolve_exec($config, $part) // $self->resolve_func($config, $part)) // $self->resolve_task($config, $part)) // $self->resolve_vars($config, $part)) // $self->resolve_find($config, $part)) // $self->resolve_perl($config, $part)
$self->resolve_exec($config, $part) $self->resolve_func($config, $part) $self->resolve_task($config, $part) $self->resolve_vars($config, $part) $self->resolve_find($config, $part) $self->resolve_perl($config, $part)
Truth table71%5/7
A // B // C // D // E // F
A= $self->resolve_exec($config, $part)B= $self->resolve_func($config, $part)C= $self->resolve_task($config, $part)D= $self->resolve_vars($config, $part)E= $self->resolve_find($config, $part)F= $self->resolve_perl($config, $part)
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
477 // $self->resolve_func($config, $part)
478 // $self->resolve_task($config, $part)
479 // $self->resolve_vars($config, $part)
480 // $self->resolve_find($config, $part)
481 // $self->resolve_perl($config, $part);
482  
483 99 ▶ push @results, Venus::list(
Branch: 1 branch
BranchTrueFalseTotal
$resolved && (!$cached && $resolved ne $part || $recurse) ? : 42 57 99
Condition58%7/12
$resolved and !$cached && $resolved ne $part || $recurse
!ll&&!rl&&r
57042
not $cached and $resolved ne $part or $recurse
l!l&&r!l&&!r
4110
not $cached and $resolved ne $part
!ll&&!rl&&r
1041
$resolved // $part
l!l&&r!l&&!r
0570
MC/DC25%1/4
$resolved and !$cached && $resolved ne $part || $recurse
$resolved not $cached $resolved ne $part $recurse
MC/DC0%0/2
$resolved // $part
$resolved $part
Truth table50%3/6
A and not(B) and C or D
A= $resolvedB= not $cachedC= $resolved ne $partD= $recurse
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
Truth table33%1/3
A // B
A= $resolvedB= $part
ABresult
1X1
011
000
484 $resolved && ((!$cached && $resolved ne $part) || $recurse)
485 ? $self->resolve_recursive($config, $resolved)
486 : ($resolved // $part)
487 );
488 }
489  
490 61 $self->cache->{$item} = [@results];
491  
492 61 ▶ return wantarray ? (@results) : $self->cache->{$item};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 61 0 61
493 }
494  
495 73 ▶sub resolve_task {
Subroutine resolve_task: called CC=4 SCAR=17.2
Pod undocumented
496 73 my ($self, $config, $item) = @_;
497  
498 73 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 73 73
499 0 return $self->cache->{$item};
500 }
501  
502 73 ▶ if (!($config->{task} && exists $config->{task}{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"task"} and exists $config->{"task"}{$item}) 73 0 73
Condition33%1/3
$config->{"task"} and exists $config->{"task"}{$item}
!ll&&!rl&&r
7300
MC/DC0%0/2
$config->{"task"} and exists $config->{"task"}{$item}
$config->{"task"} exists $config->{"task"}{$item}
Truth table33%1/3
A and B
A= $config->{"task"}B= exists $config->{"task"}{$item}
ABresult
0X0
100
111
503 73 return;
504 }
505  
506 0 return $self->cache->{$item} = $config->{task}{$item};
507 }
508  
509 73 ▶sub resolve_vars {
Subroutine resolve_vars: called CC=5 SCAR=16.4
Pod undocumented
510 73 my ($self, $config, $item) = @_;
511  
512 73 ▶ if (exists $self->cache->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$item}) 0 73 73
513 0 return $self->cache->{$item};
514 }
515  
516 73 my ($var) = $item =~ /^\$(\w+)$/;
517  
518 73 ▶ if (!(defined $var && $config->{vars} && exists $config->{vars}{$var})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $var and $config->{"vars"} and exists $config->{"vars"}{$var}) 69 4 73
Condition83%5/6
defined $var and $config->{"vars"} and exists $config->{"vars"}{$var}
!ll&&!rl&&r
6904
defined $var and $config->{"vars"}
!ll&&!rl&&r
6724
MC/DC66%2/3
defined $var and $config->{"vars"} and exists $config->{"vars"}{$var}
defined $var $config->{"vars"} exists $config->{"vars"}{$var}
Truth table75%3/4
A and B and C
A= defined $varB= $config->{"vars"}C= exists $config->{"vars"}{$var}
ABCresult
0XX0
10X0
1100
1111
519 69 return;
520 }
521  
522 4 return $self->cache->{$item} = $config->{vars}{$var};
523 }
524  
525 8 ▶sub resolve_with {
Subroutine resolve_with: called CC=4 SCAR=13.9
Pod undocumented
526 8 my ($self, $config, $item, @args) = @_;
527  
528 8 ▶ if (!($config->{with} && exists $config->{with}->{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"with"} and exists $config->{"with"}{$item}) 6 2 8
Condition66%2/3
$config->{"with"} and exists $config->{"with"}{$item}
!ll&&!rl&&r
602
MC/DC50%1/2
$config->{"with"} and exists $config->{"with"}{$item}
$config->{"with"} exists $config->{"with"}{$item}
Truth table66%2/3
A and B
A= $config->{"with"}B= exists $config->{"with"}{$item}
ABresult
0X0
100
111
529 6 return;
530 }
531  
532 2 my $here = Venus::Path->new;
533  
534 2 my $from = Venus::Path->new($self->file);
535  
536 2 $ENV{VENUS_RUN_FROM_ROOT} = $from->parent->absolute->get;
537 2 $ENV{VENUS_RUN_FROM_FILE} = $from->absolute->get;
538  
539 2 my $with = Venus::Path->new($config->{with}->{$item});
540  
541 2 $ENV{VENUS_RUN_WITH_ROOT} = $with->parent->absolute->get;
542 2 $ENV{VENUS_RUN_WITH_FILE} = $with->absolute->get;
543  
544 2 my $run = $self->from_file("$with");
545  
546 2 chdir $ENV{VENUS_RUN_WITH_ROOT};
547  
548 2 my $results = $run->resolve($run->config, @args);
549  
550 2 chdir $here->get;
551  
552 2 ▶ return wantarray ? (@{$results}) : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
553 }
554  
555 9 ▶sub result {
Subroutine result: called CC=1 SCAR=0.0
Pod documented
556 9 my ($self, @args) = @_;
557  
558 9 return $self->execute(@args);
559 }
560  
561 # ROUTINES
562  
563 40 ▶sub file {
Subroutine file: called CC=5 SCAR=16.7
Pod documented
564 40 my ($self) = @_;
565  
566 return $ENV{VENUS_FILE}
567 || $ENV{VENUS_RUN_FILE}
568 || $ENV{VENUS_RUN_CONFIG}
569 40 ▶ || (grep -f, map ".vns.$_", qw(yaml yml json js perl pl))[0]
Condition72%8/11
$ENV{'VENUS_FILE'} || $ENV{'VENUS_RUN_FILE'} || $ENV{'VENUS_RUN_CONFIG'} || (grep((-f $_), map((".vns.$_"), ('yaml', 'yml', 'json', 'js', 'perl', 'pl'))))[0] || '.vns.pl'
l!l
391
$ENV{"VENUS_FILE"} or $ENV{"VENUS_RUN_FILE"} or $ENV{"VENUS_RUN_CONFIG"} or (grep((-f $_), map((".vns.$_"), ("yaml", "yml", "json", "js", "perl", "pl"))))[0]
l!l&&r!l&&!r
3901
$ENV{"VENUS_FILE"} or $ENV{"VENUS_RUN_FILE"} or $ENV{"VENUS_RUN_CONFIG"}
l!l&&r!l&&!r
3901
$ENV{"VENUS_FILE"} or $ENV{"VENUS_RUN_FILE"}
l!l&&r!l&&!r
3901
MC/DC25%1/4
$ENV{'VENUS_FILE'} || $ENV{'VENUS_RUN_FILE'} || $ENV{'VENUS_RUN_CONFIG'} || (grep((-f $_), map((".vns.$_"), ('yaml', 'yml', 'json', 'js', 'perl', 'pl'))))[0] || '.vns.pl'
$ENV{"VENUS_FILE"} $ENV{"VENUS_RUN_FILE"} $ENV{"VENUS_RUN_CONFIG"} (grep((-f $_), map((".vns.$_"), ("yaml", "yml", "json", "js", "perl", "pl"))))[0]
Truth table40%2/5
A or B or C or D
A= $ENV{"VENUS_FILE"}B= $ENV{"VENUS_RUN_FILE"}C= $ENV{"VENUS_RUN_CONFIG"}D= (grep((-f $_), map((".vns.$_"), ("yaml", "yml", "json", "js", "perl", "pl"))))[0]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
570 || '.vns.pl';
571 }
572  
573 35 ▶sub find_config {
Subroutine find_config: called CC=2 SCAR=7.1
Pod undocumented
574 35 my ($self) = @_;
575  
576 35 my $file = $self->file;
577  
578 35 ▶ return -f $file ? $self->load_config($file) : $self->init_config;
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 35 0 35
579 }
580  
581 12 ▶sub from_file {
Subroutine from_file: called CC=2 SCAR=7.0
Pod undocumented
582 12 my ($self, $file) = @_;
583  
584 12 ▶ $file ||= $self->file;
Condition66%2/3
$file ||= $self->file
l!l&&r!l&&!r
1110
MC/DC0%0/2
$file ||= $self->file
$file $self->file
Truth table66%2/3
A ||= B
A= $fileB= $self->file
ABresult
1X1
011
000
585  
586 12 return $self->new(config => $self->load_config($file));
587 }
588  
589 1 ▶sub from_find {
Subroutine from_find: called CC=1 SCAR=0.0
Pod undocumented
590 1 my ($self) = @_;
591  
592 1 return $self->new(config => $self->find_config);
593 }
594  
595 1 ▶sub from_hash {
Subroutine from_hash: called CC=1 SCAR=0.0
Pod undocumented
596 1 my ($self, $data) = @_;
597  
598 1 return $self->new(config => $data);
599 }
600  
601 1 ▶sub from_init {
Subroutine from_init: called CC=1 SCAR=0.0
Pod undocumented
602 1 my ($self) = @_;
603  
604 1 return $self->new(config => $self->init_config);
605 }
606  
607 1 ▶sub init_config {
Subroutine init_config: called CC=1 SCAR=0.0
Pod undocumented
608 1 my ($self, @args) = @_;
609  
610 1 return $init_config;
611 }
612  
613 48 ▶sub load_config {
Subroutine load_config: called CC=1 SCAR=0.0
Pod undocumented
614 48 my ($self, $file) = @_;
615  
616 48 return Venus::Config->read_file($file)->value
617 }
618  
619 49 ▶sub new_cache {
Subroutine new_cache: called CC=1 SCAR=0.0
Pod undocumented
620 49 my ($self) = @_;
621  
622 49 return {};
623 }
624  
625 34 ▶sub new_config {
Subroutine new_config: called CC=1 SCAR=0.0
Pod undocumented
626 34 my ($self) = @_;
627  
628 34 return $self->find_config;
629 }
630  
631 48 ▶sub new_handler {
Subroutine new_handler: called CC=1 SCAR=0.3
Pod undocumented
632 48 my ($self) = @_;
633  
634 5 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
635 5 my $command = join ' ', @_;
636  
637 5 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 5 5
638 0 _print("Using: $command");
639 }
640  
641 5 my $result = _system($command);
642  
643 5 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 5 5
644 0 _error("Error running command! $command");
645 }
646  
647 5 return $result;
648 48 };
649 }
650  
651 3 1;
652  
653  
654 =head1 NAME
655  
656 Venus::Run - Run Class
657  
658 =cut
659  
660 =head1 ABSTRACT
661  
662 Run Class for Perl 5
663  
664 =cut
665  
666 =head1 SYNOPSIS
667  
668 package main;
669  
670 use Venus::Run;
671  
672 my $run = Venus::Run->new;
673  
674 # bless({...}, 'Venus::Run')
675  
676 =cut
677  
678 =head1 DESCRIPTION
679  
680 This package provides a modular command execution framework for Perl projects.
681 It loads a configuration with commands, aliases, scripts, variables, and paths,
682 and resolves them into full shell commands. This allows you to define reusable
683 CLI behaviors using declarative config without writing wrappers or shell
684 scripts. It supports layered configuration, caching, variable expansion, and
685 recursive resolution, with support for custom flow control, Perl module
686 injection, and user prompts. It also resets the C<PATH> and C<PERL5LIB>
687 variables where appropriate. See L<vns> for an executable file which loads this
688 package and provides the CLI. See L</FEATURES> for usage and configuration
689 information.
690  
691 Environment variables:
692  
693 =over 4
694  
695 =item * C<VENUS_FILE> - Explicitly set the config file for L<Venus::Run> (and L<vns>).
696  
697 =item * C<VENUS_RUN_FILE> - Alias for C<VENUS_FILE>.
698  
699 =item * C<VENUS_RUN_CONFIG> - Alias for C<VENUS_FILE>.
700  
701 =item * C<VENUS_RUN_DEBUG> - Enable debugging in L<Venus::Run> (and L<vns>).
702  
703 =back
704  
705 =cut
706  
707 =head1 ATTRIBUTES
708  
709 This package has the following attributes:
710  
711 =cut
712  
713 =head2 cache
714  
715 cache(hashref $data) (hashref)
716  
717 The cache attribute is used to store resolved values and avoid redundant
718 computation during command expansion.
719  
720 I<Since C<4.15>>
721  
722 =over 4
723  
724 =item cache example 1
725  
726 package main;
727  
728 use Venus::Run;
729  
730 my $run = Venus::Run->new;
731  
732 my $cache = $run->cache;
733  
734 # {}
735  
736 =back
737  
738 =cut
739  
740 =head2 config
741  
742 config(hashref $data) (hashref)
743  
744 The config attribute is used to store the configuration used to resolve
745 commands, variables, paths, and other runtime behavior.
746  
747 I<Since C<4.15>>
748  
749 =over 4
750  
751 =item config example 1
752  
753 package main;
754  
755 use Venus::Run;
756  
757 my $run = Venus::Run->new;
758  
759 my $config = $run->config;
760  
761 # {...}
762  
763 =back
764  
765 =cut
766  
767 =head2 debug
768  
769 debug(boolean $data) (boolean)
770  
771 The debug attribute is used to determine whether to output additional content
772 for the purpose of debugging command execution.
773  
774 I<Since C<4.15>>
775  
776 =over 4
777  
778 =item debug example 1
779  
780 package main;
781  
782 use Venus::Run;
783  
784 my $run = Venus::Run->new;
785  
786 my $debug = $run->debug;
787  
788 # false
789  
790 =back
791  
792 =cut
793  
794 =head2 handler
795  
796 handler(coderef $data) (coderef)
797  
798 The handler attribute holds the callback (i.e. coderef) invoked for each step
799 or command returned for a resolved command or expression.
800  
801 I<Since C<4.15>>
802  
803 =over 4
804  
805 =item handler example 1
806  
807 package main;
808  
809 use Venus::Run;
810  
811 my $run = Venus::Run->new;
812  
813 my $handler = $run->handler;
814  
815 # sub {...}
816  
817 =back
818  
819 =cut
820  
821 =head1 INHERITS
822  
823 This package inherits behaviors from:
824  
825 L<Venus::Role::Utility>
826  
827 =cut
828  
829 =head1 INTEGRATES
830  
831 This package integrates behaviors from:
832  
833 L<Venus::Role::Optional>
834  
835 =cut
836  
837 =head1 METHODS
838  
839 This package provides the following methods:
840  
841 =cut
842  
843 =head2 callback
844  
845 callback(coderef $code, any @args) (any)
846  
847 The callback method executes a against each fully-resolved command derived from
848 the given arguments. This method prepares the runtime environment by expanding
849 variables, updating paths, and loading libraries as defined in the config. It
850 resolves the given arguments into executable commands and passes each one to
851 the callback in sequence. The callback receives the resolved program name
852 followed by its arguments. Environment variables are restored to their original
853 state after execution. Returns the result of the last successful callback
854 execution, or C<undef> if none were executed.
855  
856 I<Since C<4.15>>
857  
858 =over 4
859  
860 =item callback example 1
861  
862 package main;
863  
864 use Venus::Run;
865  
866 my $run = Venus::Run->new;
867  
868 my $callback = $run->callback;
869  
870 # undef
871  
872 =back
873  
874 =over 4
875  
876 =item callback example 2
877  
878 package main;
879  
880 use Venus::Run;
881  
882 my $run = Venus::Run->new;
883  
884 my $data;
885  
886 $run->config({
887 exec => {
888 info => 'perl -V',
889 },
890 libs => [
891 '-Ilib',
892 '-Ilocal/lib/perl5',
893 ],
894 perl => {
895 perl => 'perl',
896 },
897 });
898  
899 my $callback = $run->callback(sub{join ' ', @_}, 'info');
900  
901 # perl -Ilib -Ilocal/lib/perl5 -V
902  
903 =back
904  
905 =cut
906  
907 =head2 execute
908  
909 execute(any @args) (any)
910  
911 The execute method L<"resolves"|/resolve> the argument(s) provided and executes
912 L</callback> using the L</handler> for each fully-resolved command encountered.
913  
914 I<Since C<4.15>>
915  
916 =over 4
917  
918 =item execute example 1
919  
920 package main;
921  
922 use Venus::Run;
923  
924 my $run = Venus::Run->new;
925  
926 my $execute = $run->execute;
927  
928 # undef
929  
930 =back
931  
932 =over 4
933  
934 =item execute example 2
935  
936 package main;
937  
938 use Venus::Run;
939  
940 my $run = Venus::Run->new;
941  
942 $run->handler(sub{join ' ', @_});
943  
944 my $execute = $run->execute('perl');
945  
946 # ['perl']
947  
948 =back
949  
950 =over 4
951  
952 =item execute example 3
953  
954 package main;
955  
956 use Venus::Run;
957  
958 my $run = Venus::Run->new;
959  
960 $run->config({
961 exec => {
962 info => 'perl -V',
963 },
964 libs => [
965 '-Ilib',
966 '-Ilocal/lib/perl5',
967 ],
968 perl => {
969 perl => 'perl',
970 },
971 });
972  
973 $run->handler(sub{join ' ', @_});
974  
975 my $execute = $run->execute('info');
976  
977 # ['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-V'"]
978  
979 =back
980  
981 =cut
982  
983 =head2 new
984  
985 new(any @args) (Venus::Run)
986  
987 The new method constructs an instance of the package.
988  
989 I<Since C<4.15>>
990  
991 =over 4
992  
993 =item new example 1
994  
995 package main;
996  
997 use Venus::Run;
998  
999 my $run = Venus::Run->new;
1000  
1001 # bless({...}, 'Venus::Run')
1002  
1003 =back
1004  
1005 =over 4
1006  
1007 =item new example 2
1008  
1009 package main;
1010  
1011 use Venus;
1012 use Venus::Run;
1013  
1014 my $run = Venus::Run->new(debug => true);
1015  
1016 # bless({...}, 'Venus::Run')
1017  
1018 =back
1019  
1020 =over 4
1021  
1022 =item new example 3
1023  
1024 package main;
1025  
1026 use Venus;
1027 use Venus::Run;
1028  
1029 my $run = Venus::Run->new(debug => false, handler => sub {});
1030  
1031 # bless({...}, 'Venus::Run')
1032  
1033 =back
1034  
1035 =cut
1036  
1037 =head2 resolve
1038  
1039 resolve(hashref $config, any @data) (arrayref)
1040  
1041 The resolve method expands a given item or command by recursively resolving
1042 aliases, variables, and configuration entries into a full command string or
1043 array. This method returns a list in list context.
1044  
1045 I<Since C<4.15>>
1046  
1047 =over 4
1048  
1049 =item resolve example 1
1050  
1051 package main;
1052  
1053 use Venus::Run;
1054  
1055 my $run = Venus::Run->new;
1056  
1057 my $resolve = $run->resolve;
1058  
1059 # []
1060  
1061 =back
1062  
1063 =over 4
1064  
1065 =item resolve example 2
1066  
1067 package main;
1068  
1069 use Venus::Run;
1070  
1071 my $run = Venus::Run->new;
1072  
1073 my $resolve = $run->resolve({}, 'perl');
1074  
1075 # [['perl']]
1076  
1077 =back
1078  
1079 =over 4
1080  
1081 =item resolve example 3
1082  
1083 package main;
1084  
1085 use Venus::Run;
1086  
1087 my $run = Venus::Run->new;
1088  
1089 my $config = {find => {perl => '/path/to/perl'}};
1090  
1091 my $resolve = $run->resolve($config, 'perl -c');
1092  
1093 # [['/path/to/perl', "'-c'"]]
1094  
1095 =back
1096  
1097 =over 4
1098  
1099 =item resolve example 4
1100  
1101 package main;
1102  
1103 use Venus::Run;
1104  
1105 my $run = Venus::Run->new;
1106  
1107 my $config = {
1108 exec => {
1109 info => 'perl -V',
1110 },
1111 libs => [
1112 '-Ilib',
1113 '-Ilocal/lib/perl5',
1114 ],
1115 perl => {
1116 perl => 'perl',
1117 },
1118 };
1119  
1120 my $resolve = $run->resolve($config, 'info');
1121  
1122 # [['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-V'"]]
1123  
1124 =back
1125  
1126 =over 4
1127  
1128 =item resolve example 5
1129  
1130 package main;
1131  
1132 use Venus::Run;
1133  
1134 my $run = Venus::Run->new;
1135  
1136 my $config = {
1137 exec => {
1138 info => 'perl -V',
1139 },
1140 libs => [
1141 '-Ilib',
1142 '-Ilocal/lib/perl5',
1143 ],
1144 load => [
1145 '-MVenus',
1146 ],
1147 perl => {
1148 perl => 'perl',
1149 },
1150 };
1151  
1152 my $resolve = $run->resolve($config, 'info');
1153  
1154 # [['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-MVenus'", "'-V'"]]
1155  
1156 =back
1157  
1158 =over 4
1159  
1160 =item resolve example 6
1161  
1162 package main;
1163  
1164 use Venus::Run;
1165  
1166 my $run = Venus::Run->new;
1167  
1168 my $config = {
1169 exec => {
1170 repl => '$REPL',
1171 },
1172 libs => [
1173 '-Ilib',
1174 '-Ilocal/lib/perl5',
1175 ],
1176 load => [
1177 '-MVenus',
1178 ],
1179 perl => {
1180 perl => 'perl',
1181 },
1182 vars => {
1183 REPL => 'perl -dE0',
1184 },
1185 };
1186  
1187 my $resolve = $run->resolve($config, 'repl');
1188  
1189 # [['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-MVenus'", "'-dE0'"]]
1190  
1191 =back
1192  
1193 =over 4
1194  
1195 =item resolve example 7
1196  
1197 package main;
1198  
1199 use Venus::Run;
1200  
1201 my $run = Venus::Run->new;
1202  
1203 my $config = {
1204 exec => {
1205 eval => 'shim -E',
1206 says => 'eval "map log(eval), @ARGV"',
1207 shim => '$PERL -MVenus=true,false,log',
1208 },
1209 libs => [
1210 '-Ilib',
1211 '-Ilocal/lib/perl5',
1212 ],
1213 perl => {
1214 perl => 'perl',
1215 },
1216 vars => {
1217 PERL => 'perl',
1218 },
1219 };
1220  
1221 my $resolve = $run->resolve($config, 'says', 1);
1222  
1223 # [['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-MVenus=true,false,log'", "'-E'", "\"map log(eval), \@ARGV\"", "'1'"]]
1224  
1225 =back
1226  
1227 =over 4
1228  
1229 =item resolve example 8
1230  
1231 package main;
1232  
1233 use Venus::Run;
1234  
1235 my $run = Venus::Run->new;
1236  
1237 my $config = {
1238 exec => {
1239 cpan => 'cpanm -llocal -qn',
1240 },
1241 };
1242  
1243 my $resolve = $run->resolve($config, 'cpan', 'Venus');
1244  
1245 # [['cpanm', "'-llocal'", "'-qn'", "'Venus'"]]
1246  
1247 =back
1248  
1249 =over 4
1250  
1251 =item resolve example 9
1252  
1253 package main;
1254  
1255 use Venus::Run;
1256  
1257 my $run = Venus::Run->new;
1258  
1259 my $config = {
1260 exec => {
1261 test => '$PROVE',
1262 },
1263 libs => [
1264 '-Ilib',
1265 '-Ilocal/lib/perl5',
1266 ],
1267 perl => {
1268 perl => 'perl',
1269 prove => 'prove',
1270 },
1271 vars => {
1272 PROVE => 'prove -j8',
1273 },
1274 };
1275  
1276 my $resolve = $run->resolve($config, 'test', 't');
1277  
1278 # [['prove', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-j8'", "'t'"]]
1279  
1280 =back
1281  
1282 =over 4
1283  
1284 =item resolve example 10
1285  
1286 package main;
1287  
1288 use Venus::Run;
1289  
1290 my $run = Venus::Run->new;
1291  
1292 my $config = {};
1293  
1294 my $resolve = $run->resolve($config, 'echo 1 | less');
1295  
1296 # [['echo', "'1'", '|', "'less'"]]
1297  
1298 =back
1299  
1300 =over 4
1301  
1302 =item resolve example 11
1303  
1304 package main;
1305  
1306 use Venus::Run;
1307  
1308 my $run = Venus::Run->new;
1309  
1310 my $config = {};
1311  
1312 my $resolve = $run->resolve($config, 'echo 1 && echo 2');
1313  
1314 # [['echo', "'1'", '&&', 'echo', "'2'"]]
1315  
1316 =back
1317  
1318 =over 4
1319  
1320 =item resolve example 12
1321  
1322 package main;
1323  
1324 use Venus::Run;
1325  
1326 my $run = Venus::Run->new;
1327  
1328 my $config = {};
1329  
1330 my $resolve = $run->resolve($config, 'echo 1 || echo 2');
1331  
1332 # [['echo', "'1'", '||', 'echo', "'2'"]]
1333  
1334 =back
1335  
1336 =over 4
1337  
1338 =item resolve example 13
1339  
1340 package main;
1341  
1342 use Venus::Run;
1343  
1344 my $run = Venus::Run->from_file('t/conf/from.perl');
1345  
1346 # in config
1347 #
1348 # ---
1349 # from:
1350 # - /path/to/parent
1351 #
1352 # ...
1353 #
1354 # exec:
1355 # mypan: cpan -M https://pkg.myapp.com
1356  
1357 # in config (/path/to/parent)
1358 #
1359 # ---
1360 # exec:
1361 # cpan: cpanm -llocal -qn
1362 #
1363 # ...
1364  
1365 my $config = $run->prepare_conf($run->config);
1366  
1367 my $resolve = $run->resolve($config, 'mypan');
1368  
1369 # [['cpanm', "'-llocal'", "'-qn'", "'-M'", "'https://pkg.myapp.com'"]]
1370  
1371 =back
1372  
1373 =over 4
1374  
1375 =item resolve example 14
1376  
1377 package main;
1378  
1379 use Venus::Run;
1380  
1381 my $run = Venus::Run->from_file('t/conf/func.perl');
1382  
1383 # in config
1384 #
1385 # ---
1386 # with:
1387 # psql: /path/to/other
1388 #
1389 # ...
1390  
1391 # in config (/path/to/other)
1392 #
1393 # ---
1394 # exec:
1395 # backup: pg_backupcluster
1396 # restore: pg_restorecluster
1397 #
1398 # ...
1399  
1400 my $config = $run->prepare_conf($run->config);
1401  
1402 my $resolve = $run->resolve($config, 'psql', 'backup');
1403  
1404 # [['pg_backupcluster']]
1405  
1406 =back
1407  
1408 =over 4
1409  
1410 =item resolve example 15
1411  
1412 package main;
1413  
1414 use Venus::Run;
1415  
1416 my $run = Venus::Run->from_file('t/conf/psql.perl');
1417  
1418 # in config
1419 #
1420 # ---
1421 # exec:
1422 # backup: pg_backupcluster
1423 # restore: pg_restorecluster
1424 #
1425 # ...
1426  
1427 my $config = $run->prepare_conf($run->config);
1428  
1429 my $resolve = $run->resolve($config, 'backup');
1430  
1431 # [['pg_backupcluster']]
1432  
1433 =back
1434  
1435 =over 4
1436  
1437 =item resolve example 16
1438  
1439 package main;
1440  
1441 use Venus::Run;
1442  
1443 my $run = Venus::Run->from_file('t/conf/flow.perl');
1444  
1445 # in config
1446 #
1447 # ---
1448 # exec:
1449 # cpan: cpanm -llocal -qn
1450 #
1451 # ...
1452 #
1453 # flow:
1454 # setup-term:
1455 # - cpan Term::ReadKey
1456 # - cpan Term::ReadLine::Gnu
1457 #
1458 # ...
1459  
1460 my $config = $run->prepare_conf($run->config);
1461  
1462 my $resolve = $run->resolve($config, 'setup-term');
1463  
1464 # [
1465 # ['cpanm', "'-llocal'", "'-qn'", "'Term::ReadKey'"],
1466 # ['cpanm', "'-llocal'", "'-qn'", "'Term::ReadLine::Gnu'"],
1467 # ]
1468  
1469 =back
1470  
1471 =over 4
1472  
1473 =item resolve example 17
1474  
1475 package main;
1476  
1477 use Venus::Run;
1478  
1479 my $run = Venus::Run->from_file('t/conf/asks.perl');
1480  
1481 # in config
1482 #
1483 # ---
1484 # asks:
1485 # PASS: What's the password
1486 #
1487 # ...
1488  
1489 my $config = $run->prepare_vars($run->prepare_conf($run->config));
1490  
1491 my $resolve = $run->resolve($config, 'echo', '$PASS');
1492  
1493 # [['echo', "'secret'"]]
1494  
1495 =back
1496  
1497 =over 4
1498  
1499 =item resolve example 18
1500  
1501 package main;
1502  
1503 use Venus::Run;
1504  
1505 my $run = Venus::Run->from_file('t/conf/with.perl');
1506  
1507 # in config
1508 #
1509 # ---
1510 # with:
1511 # psql: /path/to/other
1512 #
1513 # ...
1514  
1515 # in config (/path/to/other)
1516 #
1517 # ---
1518 # exec:
1519 # backup: pg_backupcluster
1520 # restore: pg_restorecluster
1521 #
1522 # ...
1523  
1524 my $config = $run->prepare_conf($run->config);
1525  
1526 my $resolve = $run->resolve($config, 'psql', 'backup');
1527  
1528 # [['pg_backupcluster']]
1529  
1530 =back
1531  
1532 =over 4
1533  
1534 =item resolve example 19
1535  
1536 package main;
1537  
1538 use Venus::Run;
1539  
1540 my $run = Venus::Run->from_file('t/conf/when.perl');
1541  
1542 # in config
1543 #
1544 # ---
1545 # exec:
1546 # name: echo $OSNAME
1547 #
1548 # ...
1549 # when:
1550 # is_lin:
1551 # data:
1552 # OSNAME: LINUX
1553 # is_win:
1554 # data:
1555 # OSNAME: WINDOWS
1556 #
1557 # ...
1558  
1559 # assume Linux OS
1560  
1561 my $config = $run->prepare_vars($run->prepare_conf($run->config));
1562  
1563 my $resolve = $run->resolve($config, 'name');
1564  
1565 # [['echo', "'LINUX'"]]
1566  
1567 =back
1568  
1569 =over 4
1570  
1571 =item resolve example 20
1572  
1573 package main;
1574  
1575 use Venus::Run;
1576  
1577 my $run = Venus::Run->from_file('t/conf/when.perl');
1578  
1579 # in config
1580 #
1581 # ---
1582 # exec:
1583 # name: echo $OSNAME
1584 #
1585 # ...
1586 # when:
1587 # is_lin:
1588 # data:
1589 # OSNAME: LINUX
1590 # is_win:
1591 # data:
1592 # OSNAME: WINDOWS
1593 #
1594 # ...
1595  
1596 # assume Windows OS
1597  
1598 my $config = $run->prepare_vars($run->prepare_conf($run->config));
1599  
1600 my $resolve = $run->resolve($config, 'name');
1601  
1602 # [['echo', "'WINDOWS'"]]
1603  
1604 =back
1605  
1606 =cut
1607  
1608 =head2 result
1609  
1610 result(any @args) (any)
1611  
1612 The result method is an alias for the L</execute> method, which executes the
1613 the L</handler> for each fully-resolved command based on the arguments
1614 provided.
1615  
1616 I<Since C<4.15>>
1617  
1618 =over 4
1619  
1620 =item result example 1
1621  
1622 package main;
1623  
1624 use Venus::Run;
1625  
1626 my $run = Venus::Run->new;
1627  
1628 my $result = $run->result;
1629  
1630 # undef
1631  
1632 =back
1633  
1634 =over 4
1635  
1636 =item result example 2
1637  
1638 package main;
1639  
1640 use Venus::Run;
1641  
1642 my $run = Venus::Run->new;
1643  
1644 $run->handler(sub{join ' ', @_});
1645  
1646 my $result = $run->result('perl');
1647  
1648 # ['perl']
1649  
1650 =back
1651  
1652 =over 4
1653  
1654 =item result example 3
1655  
1656 package main;
1657  
1658 use Venus::Run;
1659  
1660 my $run = Venus::Run->new;
1661  
1662 $run->config({
1663 exec => {
1664 info => 'perl -V',
1665 },
1666 libs => [
1667 '-Ilib',
1668 '-Ilocal/lib/perl5',
1669 ],
1670 perl => {
1671 perl => 'perl',
1672 },
1673 });
1674  
1675 $run->handler(sub{join ' ', @_});
1676  
1677 my $result = $run->result('info');
1678  
1679 # ['perl', "'-Ilib'", "'-Ilocal/lib/perl5'", "'-V'"]
1680  
1681 =back
1682  
1683 =cut
1684  
1685 =head1 FEATURES
1686  
1687 This package provides the following features:
1688  
1689 =cut
1690  
1691 =over 4
1692  
1693 =item config
1694  
1695 config(hashref $data) (hashref)
1696  
1697 The CLI provided by this package operates on a configuration file, typically
1698 having a base name of C<.vns> with a Perl, JSON, or YAML file extension. Here
1699 is an example of a configuration file using YAML with the filename
1700 C<.vns.yaml>.
1701  
1702 ---
1703 exec:
1704 cpan: cpanm -llocal -qn
1705 okay: $PERL -c
1706 repl: $PERL -dE0
1707 says: $PERL -E "map log(eval), @ARGV"
1708 test: $PROVE
1709 libs:
1710 - -Ilib
1711 - -Ilocal/lib/perl5
1712 load:
1713 - -MVenus=true,false
1714 path:
1715 - ./bin
1716 - ./dev
1717 - -Ilocal/bin
1718 perl:
1719 perl: perl
1720 prove: prove
1721 vars:
1722 PERL: perl
1723 PROVE: prove
1724  
1725 I<Since C<4.15>>
1726  
1727 B<example 1>
1728  
1729 package main;
1730  
1731 use Venus::Run;
1732  
1733 my $run = Venus::Run->new;
1734  
1735 my $config = $run->config;
1736  
1737 # {...}
1738  
1739 =back
1740  
1741 =over 4
1742  
1743 =item config: asks
1744  
1745 ---
1746 asks:
1747 HOME: Enter your home dir
1748  
1749 The configuration file's C<asks> section provides a list of key/value pairs
1750 where the key is the name of the environment variable and the value is used as
1751 the message used by the CLI to prompt for input if the environment variable is
1752 not defined.
1753  
1754 =back
1755  
1756 =over 4
1757  
1758 =item config: data
1759  
1760 ---
1761 data:
1762 VENUS_DEBUG: true
1763  
1764 The configuration file's C<data> section provides a non-dynamic list of
1765 key/value pairs that will be used as environment variables.
1766  
1767 =back
1768  
1769 =over 4
1770  
1771 =item config: exec
1772  
1773 ---
1774 exec:
1775 okay: $PERL -c
1776  
1777 The configuration file's C<exec> section provides the main dynamic tasks which
1778 can be recursively resolved and expanded.
1779  
1780 =back
1781  
1782 =over 4
1783  
1784 =item config: find
1785  
1786 ---
1787 find:
1788 cpanm: /usr/local/bin/cpanm
1789  
1790 The configuration file's C<find> section provides aliases which can be
1791 recursively resolved and expanded for use in other tasks.
1792  
1793 =back
1794  
1795 =over 4
1796  
1797 =item config: flow
1798  
1799 ---
1800 flow:
1801 deps:
1802 - cpan Term::ReadKey
1803 - cpan Term::ReadLine::Gnu
1804  
1805 The configuration file's C<flow> section provides chainable tasks which are
1806 recursively resolved and expanded from other tasks.
1807  
1808 =back
1809  
1810 =over 4
1811  
1812 =item config: from
1813  
1814 ---
1815 from:
1816 - /usr/share/vns/.vns.yaml
1817  
1818 The configuration file's C<from> section provides paths to other configuration
1819 files which will be merged before execution allowing the inheritance of of
1820 configuration values.
1821  
1822 =back
1823  
1824 =over 4
1825  
1826 =item config: func
1827  
1828 ---
1829 func:
1830 psql: ./psql-tools/.vns.yml
1831  
1832 The configuration file's C<func> section provides a list of static key/value
1833 pairs where the key is the "subcommand" passed to the runner as the first
1834 arugment, and the value is the configuration file where the subcommand task
1835 definitions are defined which the runner dispatches to. The differs from the
1836 C<with> section commands in that the runner doesn't C<"chdir"|perlfunc/chdir>
1837 into the configuration root before executing commands. When executed, the
1838 following environment variables are set.
1839  
1840 Environment variables:
1841  
1842 =over 4
1843  
1844 =item * C<VENUS_RUN_FROM_ROOT> - The C<$VENUS_FILE> root directory.
1845  
1846 =item * C<VENUS_RUN_FROM_FILE> - The C<$VENUS_FILE> configuration file path.
1847  
1848 =item * C<VENUS_RUN_FUNC_ROOT> - The I<"func"> configuration file root directory.
1849  
1850 =item * C<VENUS_RUN_FUNC_FILE> - The I<"func"> configuration file path.
1851  
1852 =back
1853  
1854 =back
1855  
1856 =over 4
1857  
1858 =item config: libs
1859  
1860 ---
1861 libs:
1862 - -Ilib
1863 - -Ilocal/lib/perl5
1864  
1865 The configuration file's C<libs> section provides a list of C<-I/path/to/lib>
1866 "include" statements that will be automatically added to tasks expanded from
1867 the C<perl> section.
1868  
1869 =back
1870  
1871 =over 4
1872  
1873 =item config: load
1874  
1875 ---
1876 load:
1877 - -MVenus=true,false
1878  
1879 The configuration file's C<load> section provides a list of C<-MPackage>
1880 "import" statements that will be automatically added to tasks expanded from the
1881 C<perl> section.
1882  
1883 =back
1884  
1885 =over 4
1886  
1887 =item config: path
1888  
1889 ---
1890 path:
1891 - ./bin
1892 - ./dev
1893 - -Ilocal/bin
1894  
1895 The configuration file's C<path> section provides a list of paths to be
1896 prepended to the C<PATH> environment variable which allows programs to be
1897 found.
1898  
1899 =back
1900  
1901 =over 4
1902  
1903 =item config: perl
1904  
1905 ---
1906 perl:
1907 perl: perl
1908  
1909 The configuration file's C<perl> section provides the dynamic perl tasks which
1910 can serve as tasks with default commands (with options) and which can be
1911 recursively resolved and expanded.
1912  
1913 =back
1914  
1915 =over 4
1916  
1917 =item config: task
1918  
1919 ---
1920 task:
1921 setup: $PERL -MMyApp::Task::Setup -E0 --
1922  
1923 The configuration file's C<task> section provides the dynamic perl tasks which
1924 "load" L<Venus::Task> derived packages, and which can be recursively resolved
1925 and expanded. These tasks will typically take the form of C<perl -Ilib
1926 -MMyApp::Task -E0 --> and will be automatically executed as a CLI.
1927  
1928 =back
1929  
1930 =over 4
1931  
1932 =item config: vars
1933  
1934 ---
1935 vars:
1936 PERL: perl
1937  
1938 The configuration file's C<vars> section provides a list of dynamic key/value
1939 pairs that can be recursively resolved and expanded and will be used as
1940 environment variables.
1941  
1942 =back
1943  
1944 =over 4
1945  
1946 =item config: when
1947  
1948 ---
1949 when:
1950 is_lin:
1951 data:
1952 OSNAME: LINUX
1953 is_win:
1954 data:
1955 OSNAME: WINDOWS
1956  
1957 The configuration file's C<when> section provides a configuration tree to be
1958 merged with the existing configuration based on the name current operating
1959 system. The C<is_$name> key should correspond to one of the types specified by
1960 L<Venus::Os/type>.
1961  
1962 =back
1963  
1964 =over 4
1965  
1966 =item config: with
1967  
1968 ---
1969 with:
1970 psql: ./psql-tools/.vns.yml
1971  
1972 The configuration file's C<with> section provides a list of static key/value
1973 pairs where the key is the "subcommand" passed to the runner as the first
1974 arugment, and the value is the configuration file where the subcommand task
1975 definitions are defined which the runner dispatches to. The differs from the
1976 C<func> section commands in that the runner will C<"chdir"|perlfunc/chdir> into
1977 the configuration root before executing commands. When executed, the
1978 following environment variables are set.
1979  
1980 Environment variables:
1981  
1982 =over 4
1983  
1984 =item * C<VENUS_RUN_FROM_ROOT> - The C<$VENUS_FILE> root directory.
1985  
1986 =item * C<VENUS_RUN_FROM_FILE> - The C<$VENUS_FILE> configuration file path.
1987  
1988 =item * C<VENUS_RUN_WITH_ROOT> - The I<"with"> configuration file root directory.
1989  
1990 =item * C<VENUS_RUN_WITH_FILE> - The I<"with"> configuration file path.
1991  
1992 =back
1993  
1994 =back
1995  
1996 =over 4
1997  
1998 =item runner: vns
1999  
2000 Here are example usages of the configuration file mentioned, executed by the
2001 L<vns> CLI, which is an executable file which loads this package.
2002  
2003 # Mint a new configuration file
2004 vns new pl
2005  
2006 # Mint a new JSON configuration file
2007 vns new json
2008  
2009 # Mint a new YAML configuration file
2010 vns new yaml
2011  
2012 # Install a distribution
2013 vns cpan $DIST
2014  
2015 # i.e.
2016 # cpanm --llocal -qn $DIST
2017  
2018 # Check that a package can be compiled
2019 vns okay $FILE
2020  
2021 # i.e.
2022 # perl -Ilib -Ilocal/lib/perl5 -c $FILE
2023  
2024 # Use the Perl debugger as a REPL
2025 vns repl
2026  
2027 # i.e.
2028 # perl -Ilib -Ilocal/lib/perl5 -dE0
2029  
2030 # Evaluate arbitrary Perl expressions
2031 vns exec ...
2032  
2033 # i.e.
2034 # perl -Ilib -Ilocal/lib/perl5 -MVenus=log -E $@
2035  
2036 # Test the Perl project in the CWD
2037 vns test t
2038  
2039 # i.e.
2040 # prove -Ilib -Ilocal/lib/perl5 t
2041  
2042 =back
2043  
2044 =head1 AUTHORS
2045  
2046 Awncorp, C<awncorp@cpan.org>
2047  
2048 =cut
2049  
2050 =head1 LICENSE
2051  
2052 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
2053  
2054 This program is free software, you can redistribute it and/or modify it under
2055 the terms of the Apache license version 2.0.
2056  
2057 =cut
« blib/lib/Venus/Prototype.pm ↑ Summary blib/lib/Venus/Validate.pm »