| 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
| |||||||||||||||||||||||
| 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"} ||= []
MC/DC100%1/1
$spec{"options"} ||= []
$spec{"options"}
Truth table100%2/2
A
A= $spec{"options"}
| |||||||||||||||||||||||
| 37 | 567 | ▶ | $spec{parameters} ||= []; | ||||||||||||||||||||
|
Condition100%2/2
$spec{"parameters"} ||= []
MC/DC100%1/1
$spec{"parameters"} ||= []
$spec{"parameters"}
Truth table100%2/2
A
A= $spec{"parameters"}
| |||||||||||||||||||||||
| 38 | 567 | for (@{ $spec{options} }, @{ $spec{parameters} }) { | |||||||||||||||||||||
| 39 | 820 | ▶ | $_ = { spec => $_ } unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | 567 | ▶ | $_ = App::Spec::Option->build(%$_) for @{ $spec{options} || [] }; | ||||||||||||||||||||
|
Condition50%1/2
$spec{'options'} || []
MC/DC0%0/1
$spec{'options'} || []
$spec{'options'}
Truth table50%1/2
A
A= $spec{'options'}
| |||||||||||||||||||||||
| 42 | 567 | ▶ | $_ = App::Spec::Parameter->build(%$_) for @{ $spec{parameters} || [] }; | ||||||||||||||||||||
|
Condition50%1/2
$spec{'parameters'} || []
MC/DC0%0/1
$spec{'parameters'} || []
$spec{'parameters'}
Truth table50%1/2
A
A= $spec{'parameters'}
| |||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | 567 | my $commands; | |||||||||||||||||||||
| 45 | 567 | ▶ | for my $name (keys %{ $spec{subcommands} || {} }) { | ||||||||||||||||||||
|
Condition100%2/2
$spec{'subcommands'} || {}
MC/DC100%1/1
$spec{'subcommands'} || {}
$spec{'subcommands'}
Truth table100%2/2
A
A= $spec{'subcommands'}
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 55 | 366 | ▶ | die "Invalid op '$op'" unless $op =~ m/^\w+\z/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | 567 | ▶ | if ( defined (my $class = $spec{class}) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 58 | 124 | ▶ | die "Invalid class '$class'" unless $class =~ m/^ \w+ (?: ::\w+)* \z/x; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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'} || []
MC/DC100%1/1
$spec->{'plugins'} || []
$spec->{'plugins'}
Truth table100%2/2
A
A= $spec->{'plugins'}
| |||||||||||||||||||||||
| 76 | 125 | for my $plugin (@$spec_plugins) { | |||||||||||||||||||||
| 77 | 36 | ▶ | if ($plugin =~ m/^-(.*)/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 81 | ▶ | if ($subcommands) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | 69 | for my $cmd (@$subc) { | |||||||||||||||||||||
| 149 | 69 | ▶ | $subcommands->{ $cmd->name } ||= $cmd; | ||||||||||||||||||||
|
Condition66%2/3
$$subcommands{$cmd->name} ||= $cmd
MC/DC0%0/2
$$subcommands{$cmd->name} ||= $cmd
$$subcommands{$cmd->name}
$cmd
Truth table66%2/3
A ||= B
A= $$subcommands{$cmd->name}B= $cmd
| |||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 110 | ▶ | if ($plugin->does('App::Spec::Role::Plugin::GlobalOptions')) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 158 | 73 | ▶ | $options ||= []; | ||||||||||||||||||||
|
Condition50%1/2
$options ||= []
MC/DC0%0/1
$options ||= []
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 163 | 73 | push @$options, $opt; | |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | 9 | 1; | |||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||