| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::RunList; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 105 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 105 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 105 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 105 | ▶ | use Rex::TaskList; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 105 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 105 | my $INSTANCE; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 28 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 28 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 19 | 28 | return bless \%args, $class; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # returns a singleton | ||||||||||||||||||||||||||||
| 23 | 40 | ▶ | sub instance { | ||||||||||||||||||||||||||
|
Subroutine
instance: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 40 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 40 | ▶ | return $INSTANCE if $INSTANCE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 28 | $INSTANCE = $class->new(@_); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 101 | ▶ | sub add_task { | ||||||||||||||||||||||||||
|
Subroutine
add_task: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 101 | my ( $self, $task_name, $task_args, $task_opts ) = @_; | |||||||||||||||||||||||||||
| 31 | 101 | ▶ | $task_args ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$task_args ||= []
MC/DC0%0/1
$task_args ||= []
$task_args
Truth table50%1/2
A
A= $task_args
| |||||||||||||||||||||||||||||
| 32 | 101 | ▶ | $task_opts ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$task_opts ||= {}
MC/DC0%0/1
$task_opts ||= {}
$task_opts
Truth table50%1/2
A
A= $task_opts
| |||||||||||||||||||||||||||||
| 33 | 101 | my $task = $self->task_list->get_task($task_name); | |||||||||||||||||||||||||||
| 34 | 101 | $task->set_args(@$task_args); | |||||||||||||||||||||||||||
| 35 | 101 | $task->set_opts(%$task_opts); | |||||||||||||||||||||||||||
| 36 | 101 | push @{ $self->{tasks} }, $task; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | sub current_index { | ||||||||||||||||||||||||||
|
Subroutine
current_index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 41 | 5 | ▶ | return $self->{current_index} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'current_index'} || 0
MC/DC100%1/1
$self->{'current_index'} || 0
$self->{'current_index'}
Truth table100%2/2
A
A= $self->{'current_index'}
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 70 | ▶ | sub increment_current_index { | ||||||||||||||||||||||||||
|
Subroutine
increment_current_index: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 70 | my ($self) = @_; | |||||||||||||||||||||||||||
| 46 | 70 | return $self->{current_index} += 1; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | ▶ | sub current_task { | ||||||||||||||||||||||||||
|
Subroutine
current_task: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 5 | my $i = $self->current_index; | |||||||||||||||||||||||||||
| 52 | 5 | $self->{tasks}->[$i]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 39 | ▶ | sub tasks { @{ shift->{tasks} || [] } } | ||||||||||||||||||||||||||
|
Subroutine
tasks: called CC=2 SCAR=7.2
Pod
undocumented
Condition50%1/2
+(shift())->{'tasks'} || []
MC/DC0%0/1
+(shift())->{'tasks'} || []
+(shift())->{'tasks'}
Truth table50%1/2
A
A= +(shift())->{'tasks'}
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 290 | ▶ | sub task_list { | ||||||||||||||||||||||||||
|
Subroutine
task_list: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 290 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 290 | ▶ | return $self->{task_list} if $self->{task_list}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 28 | $self->{task_list} = Rex::TaskList->create; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 29 | ▶ | sub run_tasks { | ||||||||||||||||||||||||||
|
Subroutine
run_tasks: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 29 | my ($self) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 29 | for my $task ( $self->tasks ) { | |||||||||||||||||||||||||||
| 67 | 71 | Rex::TaskList->run($task); | |||||||||||||||||||||||||||
| 68 | 55 | $self->increment_current_index; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Parse @ARGV to get tasks, task args, and task opts. Use these values to | ||||||||||||||||||||||||||||
| 73 | # generate a list of tasks the user wants to run. | ||||||||||||||||||||||||||||
| 74 | 35 | ▶ | sub parse_opts { | ||||||||||||||||||||||||||
|
Subroutine
parse_opts: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 35 | my ( $self, @params ) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 35 | ▶ | return $self->pre_1_4_parse_opts(@params) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | unless Rex::Config->get_task_chaining_cmdline_args; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 11 | ▶ | while ( my $task_name = shift @params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 33 | ▶ | $self->exit_rex($task_name) unless $self->task_list->is_task($task_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 33 | my @args; | |||||||||||||||||||||||||||
| 84 | my %opts; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 33 | ▶ | while ( my $param = shift @params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 88 | ▶ | if ( $self->task_list->is_task($param) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 22 | unshift @params, $param; | |||||||||||||||||||||||||||
| 89 | 22 | last; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 66 | ▶ | if ( $param =~ /^--/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 33 | my ( $key, $val ) = split /=/, $param, 2; | |||||||||||||||||||||||||||
| 94 | 33 | $key =~ s/^--//; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 33 | ▶ | $opts{$key} = defined $val ? $val : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||
| 99 | 33 | push @args, $param; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 33 | $self->add_task( $task_name, \@args, \%opts ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 11 | return $self; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # this function is to parse the task parameters in a pre 1.4 fashion. | ||||||||||||||||||||||||||||
| 110 | # this is used if the feature flag 'no_task_chaining_cmdline_args' or | ||||||||||||||||||||||||||||
| 111 | # '< 1.4' is enabled. | ||||||||||||||||||||||||||||
| 112 | 24 | ▶ | sub pre_1_4_parse_opts { | ||||||||||||||||||||||||||
|
Subroutine
pre_1_4_parse_opts: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 24 | my ( $self, @params ) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | #### parse task options | ||||||||||||||||||||||||||||
| 116 | 24 | my %task_opts; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 24 | for my $p (@params) { | |||||||||||||||||||||||||||
| 119 | 104 | my ( $key, $val ) = split( /=/, $p, 2 ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 104 | $key =~ s/^--//; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 104 | ▶ | if ( defined $val ) { $task_opts{$key} = $val; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 68 | $task_opts{$key} = 1; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 24 | for my $task_name (@params) { | |||||||||||||||||||||||||||
| 128 | 104 | ▶ | next if $task_name =~ m/^\-\-/ || $task_name =~ m/=/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$task_name =~ /^\-\-/u or $task_name =~ /=/u
MC/DC50%1/2
$task_name =~ /^\-\-/u or $task_name =~ /=/u
$task_name =~ /^\-\-/u
$task_name =~ /=/u
Truth table66%2/3
A or B
A= $task_name =~ /^\-\-/uB= $task_name =~ /=/u
| |||||||||||||||||||||||||||||
| 129 | 68 | ▶ | $self->exit_rex($task_name) unless $self->task_list->is_task($task_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 68 | $self->add_task( $task_name, [], \%task_opts ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub exit_rex { | ||||||||||||||||||||||||||
|
Subroutine
exit_rex: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 0 | my ( $self, $task_name ) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | my $msg = "Task names are case sensitive "; | |||||||||||||||||||||||||||
| 137 | 0 | $msg .= "and the module delimiter is a single colon."; | |||||||||||||||||||||||||||
| 138 | 0 | Rex::Logger::info( "No task named '$task_name' found. $msg", 'error' ); | |||||||||||||||||||||||||||
| 139 | 0 | Rex::CLI::exit_rex(1); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 105 | 1; | |||||||||||||||||||||||||||