diff --git a/lib/Rex/Commands.pm b/lib/Rex/Commands.pm index 5dc78bd5c..b9fa5f3a4 100644 --- a/lib/Rex/Commands.pm +++ b/lib/Rex/Commands.pm @@ -24,11 +24,14 @@ use base qw(Exporter); exit evaluate_hostname logging + needs ); sub task { my($class, $file, @tmp) = caller; my $task_name = shift; + my $task_name_save = $task_name; + if($class ne "main") { $task_name = $class . ":" . $task_name; } @@ -44,6 +47,10 @@ sub task { push(@_, ""); } + no strict 'refs'; + push (@{"${class}::tasks"}, { name => $task_name_save, code => $_[-2] } ); + use strict; + Rex::Task->create_task($task_name, @_); } @@ -163,5 +170,22 @@ sub logging { } } +sub needs { + my ($self, @args) = @_; + + no strict 'refs'; + my @tasks_to_run = @{"${self}::tasks"}; + use strict; + + for my $task (@tasks_to_run) { + if(@args && $task->{"name"} ~~ @args) { + &{ $task->{"code"} }; + } + elsif(! @args) { + &{ $task->{"code"} }; + } + } + +} 1;