Spec/Role/Command.pm

filter: Statementstmt 93.9% 124 / 132 Branchbran 77.2% 34 / 44 Conditioncond 76.4% 13 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 94.7% 18 / 19 Podpod 100% 7 / 7 totaltotal 87.7% 200 / 228 CC 41 SCAR 21.8
CC
41
Cov
89%
build
15.2
init_plugins
13.2
load_data
7.3
CRAP 8.8
« Spec/Run.pm ↑ Summary Spec/Plugin/Help.pm »
1 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package App::Spec::Role::Command;
4  
5 9 our $VERSION = 'v0.15.1'; # TRIAL VERSION
6  
7 9 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use List::Util qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use App::Spec::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Ref::Util qw/ is_arrayref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 has name => ( is => 'rw' );
15 9 has markup => ( is => 'rw', default => 'pod' );
16 9 has class => ( is => 'rw' );
17 9 has op => ( is => 'ro' );
18 9 has plugins => ( is => 'ro' );
19 1015 ▶has plugins_by_type => ( is => 'ro', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20 448 ▶has options => ( is => 'rw', default => sub { +[] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 448 ▶has parameters => ( is => 'rw', default => sub { +[] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
22 0 ▶has subcommands => ( is => 'rw', default => sub { +{} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
23 9 has description => ( is => 'rw' );
24  
25 44 ▶sub default_plugins {
Subroutine default_plugins: called CC=1 SCAR=0.0
Pod documented
26 44 qw/ Meta Help /
27 }
28  
29 37 ▶sub has_subcommands {
Subroutine has_subcommands: called CC=2 SCAR=6.9
Pod documented
30 37 my ($self) = @_;
31 37 ▶ return $self->subcommands ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->subcommands ? : 34 3 37
32 }
33  
34 567 ▶sub build {
Subroutine build: called CC=15 SCAR=27.2
Pod documented
35 567 my ($class, %spec) = @_;
36 567 ▶ $spec{options} ||= [];
Condition100%2/2
$spec{"options"} ||= []
l!l
286281
MC/DC100%1/1
$spec{"options"} ||= []
$spec{"options"}
Truth table100%2/2
A
A= $spec{"options"}
Aresult
11
00
37 567 ▶ $spec{parameters} ||= [];
Condition100%2/2
$spec{"parameters"} ||= []
l!l
147420
MC/DC100%1/1
$spec{"parameters"} ||= []
$spec{"parameters"}
Truth table100%2/2
A
A= $spec{"parameters"}
Aresult
11
00
38 567 for (@{ $spec{options} }, @{ $spec{parameters} }) {
39 820 ▶ $_ = { spec => $_ } unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 4 816 820
40 }
41 567 ▶ $_ = App::Spec::Option->build(%$_) for @{ $spec{options} || [] };
Condition50%1/2
$spec{'options'} || []
l!l
5670
MC/DC0%0/1
$spec{'options'} || []
$spec{'options'}
Truth table50%1/2
A
A= $spec{'options'}
Aresult
11
00
42 567 ▶ $_ = App::Spec::Parameter->build(%$_) for @{ $spec{parameters} || [] };
Condition50%1/2
$spec{'parameters'} || []
l!l
5670
MC/DC0%0/1
$spec{'parameters'} || []
$spec{'parameters'}
Truth table50%1/2
A
A= $spec{'parameters'}
Aresult
11
00
43  
44 567 my $commands;
45 567 ▶ for my $name (keys %{ $spec{subcommands} || {} }) {
Condition100%2/2
$spec{'subcommands'} || {}
l!l
178389
MC/DC100%1/1
$spec{'subcommands'} || {}
$spec{'subcommands'}
Truth table100%2/2
A
A= $spec{'subcommands'}
Aresult
11
00
46 442 my $cmd = $spec{subcommands}->{ $name };
47 442 $commands->{ $name } = App::Spec::Subcommand->build(
48 name => $name,
49 %$cmd,
50 );
51 }
52 567 $spec{subcommands} = $commands;
53  
54 567 ▶ if ( defined (my $op = $spec{op}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $op = $spec{"op"})) 366 201 567
55 366 ▶ die "Invalid op '$op'" unless $op =~ m/^\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^\w+\z/ 0 366 366
56 }
57 567 ▶ if ( defined (my $class = $spec{class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $class = $spec{"class"})) 124 443 567
58 124 ▶ die "Invalid class '$class'" unless $class =~ m/^ \w+ (?: ::\w+)* \z/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /^ \w+ (?: ::\w+)* \z/x 0 124 124
59 }
60  
61 567 my $self = $class->new(%spec);
62 }
63  
64 125 ▶sub read {
Subroutine read: called CC=7 SCAR=19.5
Pod documented
65 125 my ($class, $file) = @_;
66 125 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 125 125
67 0 die "No filename given";
68 }
69  
70 125 my $spec = $class->load_data($file);
71  
72 125 my %disable;
73 my @plugins;
74  
75 125 ▶ my $spec_plugins = $spec->{plugins} || [];
Condition100%2/2
$spec->{'plugins'} || []
l!l
7451
MC/DC100%1/1
$spec->{'plugins'} || []
$spec->{'plugins'}
Truth table100%2/2
A
A= $spec->{'plugins'}
Aresult
11
00
76 125 for my $plugin (@$spec_plugins) {
77 36 ▶ if ($plugin =~ m/^-(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin =~ /^-(.*)/) 7 29 36
78 7 $disable{ $1 } = 1;
79 }
80 }
81 125 my @default_plugins = grep { not $disable{ $_ } } $class->default_plugins;
82  
83 125 push @plugins, @default_plugins;
84 125 push @plugins, grep{ not m/^-/ } @$spec_plugins;
85 125 for my $plugin (@plugins) {
86 110 ▶ unless ($plugin =~ s/^=//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin =~ s/^=//) 110 0 110
87 110 $plugin = "App::Spec::Plugin::$plugin";
88 }
89 }
90 125 $spec->{plugins} = \@plugins;
91  
92 125 my $self = $class->build(%$spec);
93  
94 125 $self->load_plugins;
95 125 $self->init_plugins;
96  
97 125 return $self;
98 }
99  
100 125 ▶sub load_data {
Subroutine load_data: called CC=6 SCAR=19.9
Pod documented
101 125 my ($class, $file) = @_;
102 125 my $spec;
103 125 ▶ if (ref $file eq 'GLOB') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 0 125 125
elsif (not ref $file) { } 42 83 125
elsif (ref $file eq 'SCALAR') { } 37 46 83
elsif (ref $file eq 'HASH') { } 46 0 46
104 0 my $data = do { local $/; <$file> };
105 0 $spec = eval { YAML::PP::Load($data) };
106 }
107 elsif (not ref $file) {
108 42 $spec = eval { YAML::PP::LoadFile($file) };
109 }
110 elsif (ref $file eq 'SCALAR') {
111 37 my $data = $$file;
112 37 $spec = eval { YAML::PP::Load($data) };
113 }
114 elsif (ref $file eq 'HASH') {
115 46 $spec = $file;
116 }
117  
118 125 ▶ unless ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 0 125 125
119 0 die "Error reading '$file': $@";
120 }
121 125 return $spec;
122 }
123  
124 125 ▶sub load_plugins {
Subroutine load_plugins: called CC=3 SCAR=11.0
Pod documented
125 125 my ($self) = @_;
126 125 my $plugins = $self->plugins;
127 125 ▶ if (@$plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$plugins) 44 81 125
128 44 require Module::Runtime;
129 44 for my $plugin (@$plugins) {
130 110 my $loaded = Module::Runtime::require_module($plugin);
131 }
132 }
133 }
134  
135 125 ▶sub init_plugins {
Subroutine init_plugins: called CC=13 SCAR=25.8
Pod documented
136 125 my ($self) = @_;
137 125 my $plugins = $self->plugins;
138 125 ▶ if (@$plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$plugins) 44 81 125
139 44 my $subcommands = $self->subcommands;
140 44 my $options = $self->options;
141 44 for my $plugin (@$plugins) {
142 110 ▶ if ($plugin->does('App::Spec::Role::Plugin::Subcommand')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->does("App::Spec::Role::Plugin::Subcommand")) 81 29 110
143 81 push @{ $self->plugins_by_type->{Subcommand} }, $plugin;
144 81 my $subc = $plugin->install_subcommands( spec => $self );
145 81 ▶ $subc = [ $subc ] unless is_arrayref($subc);
Branch: 1 branch
BranchTrueFalseTotal
unless is_arrayref($subc) 81 0 81
146  
147 81 ▶ if ($subcommands) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcommands) 69 12 81
148 69 for my $cmd (@$subc) {
149 69 ▶ $subcommands->{ $cmd->name } ||= $cmd;
Condition66%2/3
$$subcommands{$cmd->name} ||= $cmd
l!l&&r!l&&!r
1680
MC/DC0%0/2
$$subcommands{$cmd->name} ||= $cmd
$$subcommands{$cmd->name} $cmd
Truth table66%2/3
A ||= B
A= $$subcommands{$cmd->name}B= $cmd
ABresult
1X1
011
000
150 }
151 }
152 }
153  
154 110 ▶ if ($plugin->does('App::Spec::Role::Plugin::GlobalOptions')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->does("App::Spec::Role::Plugin::GlobalOptions")) 73 37 110
155 73 push @{ $self->plugins_by_type->{GlobalOptions} }, $plugin;
156 73 my $new_opts = $plugin->install_options( spec => $self );
157 73 ▶ if ($new_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_opts) 73 0 73
158 73 ▶ $options ||= [];
Condition50%1/2
$options ||= []
l!l
730
MC/DC0%0/1
$options ||= []
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
159  
160 73 for my $opt (@$new_opts) {
161 73 $opt = App::Spec::Option->build(%$opt);
162 73 ▶ unless (any { $_->name eq $opt->name } @$options) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_->name eq $opt->name; } , @$options)) 73 0 73
163 73 push @$options, $opt;
164 }
165 }
166  
167 }
168 }
169  
170 }
171 }
172 }
173  
174  
175 9 1;
176  
177 __END__
« Spec/Run.pm ↑ Summary Spec/Plugin/Help.pm »