| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Resource::Common; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 32 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 32 | require Exporter; | |||||||||||||||||||||||||||
| 13 | 32 | require Rex::Config; | |||||||||||||||||||||||||||
| 14 | 32 | ▶ | use Rex::Resource; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 32 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 32 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 32 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 32 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 32 | @EXPORT = qw(emit resource resource_name changed created removed); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub changed { return "changed"; } | ||||||||||||||||||||||||||
|
Subroutine
changed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub created { return "created"; } | ||||||||||||||||||||||||||
|
Subroutine
created: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub removed { return "removed"; } | ||||||||||||||||||||||||||
|
Subroutine
removed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub emit { | ||||||||||||||||||||||||||
|
Subroutine
emit: called CC=7 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 1 | my ( $type, $message ) = @_; | |||||||||||||||||||||||||||
| 28 | 1 | ▶ | if ( !Rex::Resource->is_inside_resource ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | die "emit() only allowed inside resource."; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | $message ||= ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$message ||= ""
MC/DC0%0/1
$message ||= ""
$message
Truth table50%1/2
A
A= $message
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | Rex::Logger::debug( "Emiting change: " . $type . " - $message." ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | if ( $type eq changed ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | current_resource()->changed(1); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ( $type eq created ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | current_resource()->created(1); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if ( $type eq removed ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | current_resource()->removed(1); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if ($message) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | current_resource()->message($message); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =over 4 | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item resource($name, $function) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 36 | ▶ | sub resource { | ||||||||||||||||||||||||||
|
Subroutine
resource: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 36 | my ( $name, $options, $function ) = @_; | |||||||||||||||||||||||||||
| 61 | 36 | my $name_save = $name; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 36 | my $caller_pkg = caller; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 36 | ▶ | if ( ref $options eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 4 | $function = $options; | |||||||||||||||||||||||||||
| 67 | 4 | $options = {}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 36 | ▶ | if ( $name_save !~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | Rex::Logger::info( | |||||||||||||||||||||||||||
| 72 | "Please use only the following characters for resource names:", "warn" ); | ||||||||||||||||||||||||||||
| 73 | 0 | Rex::Logger::info( " A-Z, a-z, 0-9 and _", "warn" ); | |||||||||||||||||||||||||||
| 74 | 0 | Rex::Logger::info( "Also the resource should start with A-Z or a-z", | |||||||||||||||||||||||||||
| 75 | "warn" ); | ||||||||||||||||||||||||||||
| 76 | 0 | die "Wrong resource name syntax."; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 36 | my ( $class, $file, @tmp ) = caller; | |||||||||||||||||||||||||||
| 80 | my $res = Rex::Resource->new( | ||||||||||||||||||||||||||||
| 81 | type => "${class}::$name", | ||||||||||||||||||||||||||||
| 82 | name => $name, | ||||||||||||||||||||||||||||
| 83 | display_name => ( | ||||||||||||||||||||||||||||
| 84 | $options->{name} | ||||||||||||||||||||||||||||
| 85 | 36 | ▶ | || ( $options->{export} ? $name : "${caller_pkg}::${name}" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$options->{'name'} || ($options->{'export'} ? $name : "${caller_pkg}::$name")
MC/DC0%0/2
$options->{'name'} || ($options->{'export'} ? $name : "${caller_pkg}::$name")
$options->{'name'}
($options->{'export'} ? $name : "${caller_pkg}::$name")
Truth table33%1/3
A || B
A= $options->{'name'}B= ($options->{'export'} ? $name : "${caller_pkg}::$name")
| |||||||||||||||||||||||||||||
| 86 | ), | ||||||||||||||||||||||||||||
| 87 | cb => $function | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | ▶ | my $func = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 4 | $res->call(@_); | |||||||||||||||||||||||||||
| 92 | 36 | }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 36 | ▶ | if (!$class->can($name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $class->can($name) and $name_save =~ /^[a-zA-Z_][a-zA-Z0-9_]+$/u
MC/DC0%0/2
not $class->can($name) and $name_save =~ /^[a-zA-Z_][a-zA-Z0-9_]+$/u
not $class->can($name)
$name_save =~ /^[a-zA-Z_][a-zA-Z0-9_]+$/u
Truth table33%1/3
not(A) and B
A= not $class->can($name)B= $name_save =~ /^[a-zA-Z_][a-zA-Z0-9_]+$/u
| |||||||||||||||||||||||||||||
| 95 | && $name_save =~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ ) | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 36 | ▶ | if ( $class ne "main" && $class ne "Rex::CLI" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class ne "main" and $class ne "Rex::CLI"
MC/DC50%1/2
$class ne "main" and $class ne "Rex::CLI"
$class ne "main"
$class ne "Rex::CLI"
Truth table66%2/3
A and B
A= $class ne "main"B= $class ne "Rex::CLI"
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # if not in main namespace, register the task as a sub | ||||||||||||||||||||||||||||
| 100 | 33 | Rex::Logger::debug( | |||||||||||||||||||||||||||
| 101 | "Registering resource (not main namespace): ${class}::$name_save"); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 3 | Rex::Logger::debug("Registering resource: ${class}::$name_save"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 36 | my $code = $_[-2]; | |||||||||||||||||||||||||||
| 108 | 36 | my $ref_to_resource = qualify_to_ref( $name_save, $class ); | |||||||||||||||||||||||||||
| 109 | 36 | *{$ref_to_resource} = $func; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 36 | ▶ | if ( exists $options->{export} && $options->{export} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $options->{"export"} and $options->{"export"}
MC/DC50%1/2
exists $options->{"export"} and $options->{"export"}
exists $options->{"export"}
$options->{"export"}
Truth table66%2/3
A and B
A= exists $options->{"export"}B= $options->{"export"}
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # register in caller namespace | ||||||||||||||||||||||||||||
| 115 | 32 | my $ref_to_ISA = qualify_to_ref( 'ISA', $caller_pkg ); | |||||||||||||||||||||||||||
| 116 | 32 | my $ref_to_EXPORT = qualify_to_ref( 'EXPORT', $caller_pkg ); | |||||||||||||||||||||||||||
| 117 | 32 | push @{ *{$ref_to_ISA} }, "Rex::Exporter" | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | unless ( grep { $_ eq "Rex::Exporter" } @{ *{$ref_to_ISA} } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 32 | push @{ *{$ref_to_EXPORT} }, $name_save; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | sub resource_name { | ||||||||||||||||||||||||||
|
Subroutine
resource_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 4 | Rex::Config->set( resource_name => current_resource()->{res_name} ); | |||||||||||||||||||||||||||
| 125 | 4 | return current_resource()->{res_name}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub resource_ensure { | ||||||||||||||||||||||||||
|
Subroutine
resource_ensure: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 0 | my ($option) = @_; | |||||||||||||||||||||||||||
| 130 | 0 | $option->{ current_resource()->{res_ensure} }->(); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 9 | ▶ | sub current_resource { | ||||||||||||||||||||||||||
|
Subroutine
current_resource: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 9 | return $Rex::Resource::CURRENT_RES[-1]; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =back | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 32 | 1; | |||||||||||||||||||||||||||