Runner/Command/list.pm

filter: Statementstmt 98.0% 101 / 103 Branchbran 96.1% 25 / 26 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 96.8% 152 / 157 CC 19 SCAR 12.8
CC
19
Cov
97%
_list_service
6.1
_list_services
5.0
_get_service_info
5.0
CRAP 3.6
« Runner/ExecCommand.pm ↑ Summary Runner/Command/run.pm »
1 package Beam::Runner::Command::list;
2 2 our $VERSION = '0.016';
3 # ABSTRACT: List the available containers and services
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod beam list
8 #pod beam list <container>
9 #pod
10 #pod =head1 DESCRIPTION
11 #pod
12 #pod List the available containers found in the directories defined in
13 #pod C<BEAM_PATH>, and list the runnable services found in them. Also show
14 #pod the C<$summary> from the container file, and the abstract from every
15 #pod service.
16 #pod
17 #pod When listing services, this command must load every single class
18 #pod referenced in the container, but it will not instanciate any object.
19 #pod
20 #pod =head1 SEE ALSO
21 #pod
22 #pod L<beam>, L<Beam::Runner::Command>, L<Beam::Runner>
23 #pod
24 #pod =cut
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use List::Util qw( any max );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Beam::Wire;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Beam::Runner::Util qw( find_container_path find_containers );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Pod::Find qw( pod_where );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Term::ANSIColor qw( color );
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # The extensions to remove to show the container's name
38 2 my @EXTS = grep { $_ } @Beam::Runner::Util::EXTS;
39  
40 #pod =method run
41 #pod
42 #pod my $exit = $class->run;
43 #pod my $exit = $class->run( $container );
44 #pod
45 #pod Print the list of containers to C<STDOUT>, or, if C<$container> is given,
46 #pod print the list of runnable services. A runnable service is an object
47 #pod that consumes the L<Beam::Runnable> role.
48 #pod
49 #pod =cut
50  
51 5 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
52 5 my ( $class, $container ) = @_;
53  
54 5 ▶ if ( !$container ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($container) 2 3 5
55 2 return $class->_list_containers;
56 }
57  
58 3 ▶ if ( !$class->_list_services( $container ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_list_services($container)) 1 2 3
59 1 warn qq{No runnable services in container "$container"\n};
60 1 return 1;
61 }
62  
63 2 return 0;
64 }
65  
66 #=sub _list_containers
67 #
68 # my $exit = $class->_list_containers
69 #
70 # Print all the containers found in the BEAM_PATH to STDOUT
71 #
72 #=cut
73  
74 2 ▶sub _list_containers {
Subroutine _list_containers: called CC=4 SCAR=13.9
75 2 my ( $class ) = @_;
76 die "Cannot list containers: BEAM_PATH environment variable not set\n"
77 2 ▶ unless $ENV{BEAM_PATH};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"BEAM_PATH"} 1 1 2
78  
79 1 my %containers = find_containers();
80 1 my @container_names = sort keys %containers;
81 1 my $printed = 0;
82 1 for my $i ( 0..$#container_names ) {
83 3 ▶ if ( $printed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($printed) 1 2 3
84 1 print "\n";
85 1 $printed = 0;
86 }
87 3 $printed += $class->_list_services( $containers{ $container_names[ $i ] } );
88 }
89  
90 1 return 0;
91 }
92  
93 #=sub _list_services
94 #
95 # my $exit = $class->_list_services( $container );
96 #
97 # Print all the runnable services found in the container to STDOUT
98 #
99 #=cut
100  
101 6 ▶sub _list_services {
Subroutine _list_services: called CC=5 SCAR=16.1
102 6 my ( $class, $container ) = @_;
103 6 my $path = find_container_path( $container );
104 6 my $cname = $path->basename( @EXTS );
105 6 my $wire = Beam::Wire->new(
106 file => $path,
107 );
108  
109 6 my $config = $wire->config;
110 6 my %services;
111 6 for my $name ( keys %$config ) {
112 37 my ( $name, $abstract ) = _list_service( $wire, $name, $config->{$name} );
113 37 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 20 17 37
114 17 $services{ $name } = $abstract;
115 }
116 6 ▶ return 0 unless keys %services;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %services 2 4 6
117  
118 4 my ( $bold, $reset ) = ( color( 'bold' ), color( 'reset' ) );
119 4 ▶ print "$bold$cname$reset" . ( eval { " -- " . $wire->get( '$summary' ) } || '' ) . "\n";
Condition100%2/2
eval { do { ' -- ' . $wire->get('$summary') } } || ''
l!l
31
MC/DC100%1/1
eval { do { ' -- ' . $wire->get('$summary') } } || ''
eval { do { ' -- ' . $wire->get('$summary') } }
Truth table100%2/2
A
A= eval { do { ' -- ' . $wire->get('$summary') } }
Aresult
11
00
120  
121 4 my $size = max map { length } keys %services;
122 4 print join( "\n", map { sprintf "- $bold%-${size}s$reset -- %s", $_, $services{ $_ } } sort keys %services ), "\n";
123 4 return 1;
124 }
125  
126 #=sub _list_service
127 #
128 # my $service_info = _list_service( $wire, $name, $config );
129 #
130 # If the given service is a runnable service, return the information
131 # about it ready to be printed to STDOUT. $wire is a Beam::Wire object,
132 # $name is the name of the service, $config is the service's
133 # configuration hash
134 #
135 #=cut
136  
137 40 ▶sub _list_service {
Subroutine _list_service: called CC=6 SCAR=18.1
138 40 my ( $wire, $name, $svc ) = @_;
139  
140 # If it doesn't look like a service, we don't care
141 40 ▶ return unless $wire->is_meta( $svc, 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $wire->is_meta($svc, 1) 10 30 40
142  
143 # Services that are just references to other services should still
144 # be available under their referenced name
145 30 my %svc = %{ $wire->normalize_config( $svc ) };
146 30 ▶ if ( $svc{ ref } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc{"ref"}) 3 27 30
147 3 my $ref_svc = $wire->get_config( $svc{ ref } );
148 3 return _list_service( $wire, $name, $ref_svc );
149 }
150  
151 # Services that extend other services must be resolved to find their
152 # class and roles
153 27 my %merged = $wire->merge_config( %svc );
154 #; use Data::Dumper;
155 #; print "$name merged: " . Dumper \%merged;
156 27 my $class = $merged{ class };
157 27 ▶ my @roles = @{ $merged{ with } || [] };
Condition50%1/2
$merged{'with'} || []
l!l
027
MC/DC0%0/1
$merged{'with'} || []
$merged{'with'}
Truth table50%1/2
A
A= $merged{'with'}
Aresult
11
00
158  
159 # Can we determine this object is runnable without loading anything?
160 0 ▶ if ( grep { $_ eq 'Beam::Runnable' } @roles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "Beam::Runnable";} @roles) 0 27 27
161 0 return _get_service_info( $name, $class, \%merged );
162 }
163  
164 27 ▶ if ( eval { any {; use_module( $_ )->DOES( 'Beam::Runnable' ) } $class, @roles } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &any(sub { &use_module($_)->DOES("Beam::Runnable"); } , $class, @roles) } }) 17 10 27
165 17 return _get_service_info( $name, $class, \%merged );
166 }
167  
168 10 return;
169 }
170  
171 #=sub _get_service_info( $name, $class )
172 #
173 # my ( $name, $abstract ) = _get_service_info( $name, $class, $config );
174 #
175 # Get the information about the given service. Opens the C<$class>
176 # documentation to find the class's abstract (the C<=head1 NAME>
177 # section). If C<$config> contains a C<summary> in its C<args> hashref,
178 # will use that in place of the POD documentation.
179 #
180 #=cut
181  
182 17 ▶sub _get_service_info {
Subroutine _get_service_info: called CC=5 SCAR=16.1
183 17 my ( $name, $class, $config ) = @_;
184 17 ▶ if ( $config->{args}{summary} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"args"}{"summary"}) 12 5 17
185 # XXX: This does not allow good defaults from the object
186 # itself... There's no way to get that without instantiating the
187 # object, which means potentially doing a lot of work like
188 # connecting to a database. If we had some way of making things
189 # extra lazy, we could create the object without doing much
190 # work...
191 12 return $name, $config->{args}{summary};
192 }
193 5 my $pod_path = pod_where( { -inc => 1 }, $class );
194 5 ▶ return $name, $class unless $pod_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod_path 1 4 5
195  
196 4 my $pod_root = Pod::Simple::SimpleTree->new->parse_file( $pod_path )->root;
197 #; use Data::Dumper;
198 #; print Dumper $pod_root;
199 4 my @nodes = @{$pod_root}[2..$#$pod_root];
200 #; print Dumper \@nodes;
201 4 ▶ my ( $name_i ) = grep { $nodes[$_][0] eq 'head1' && $nodes[$_][2] eq 'NAME' } 0..$#nodes;
Condition100%3/3
$nodes[$_][0] eq 'head1' && $nodes[$_][2] eq 'NAME'
!ll&&!rl&&r
40223
MC/DC100%2/2
$nodes[$_][0] eq 'head1' && $nodes[$_][2] eq 'NAME'
$nodes[$_][0] eq 'head1' $nodes[$_][2] eq 'NAME'
Truth table100%3/3
A && B
A= $nodes[$_][0] eq 'head1'B= $nodes[$_][2] eq 'NAME'
ABresult
0X0
100
111
202 4 ▶ return $name, $class unless defined $name_i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name_i 1 3 4
203  
204 3 my $abstract = $nodes[ $name_i + 1 ][2];
205 3 return $name, $abstract;
206 }
207  
208 2 1;
209  
210 __END__
« Runner/ExecCommand.pm ↑ Summary Runner/Command/run.pm »