| 1 | package App::NDTools::NDProc; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use parent 'App::NDTools::NDTool'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Getopt::Long qw(GetOptionsFromArray :config bundling); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Log::Log4Cli; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Module::Find qw(findsubmod); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use App::NDTools::Slurp qw(s_decode s_dump s_encode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Storable qw(dclone freeze thaw); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Struct::Diff 0.94 qw(diff split_diff); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Struct::Path 0.80 qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Struct::Path::PerlStyle 0.90 qw(str2path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 10 | our $VERSION = '0.32'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 114 | ▶ | sub arg_opts { | ||||||||||||||||||||||||||
|
Subroutine
arg_opts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 114 | my $self = shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 114 | Getopt::Long::Configure('pass_through'); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my %arg_opts = ( | ||||||||||||||||||||||||||||
| 24 | $self->SUPER::arg_opts(), | ||||||||||||||||||||||||||||
| 25 | 'builtin-format=s' => \$self->{OPTS}->{'builtin-format'}, | ||||||||||||||||||||||||||||
| 26 | 'builtin-rules=s' => \$self->{OPTS}->{'builtin-rules'}, | ||||||||||||||||||||||||||||
| 27 | 'disable-module=s@' => \$self->{OPTS}->{'disable-module'}, | ||||||||||||||||||||||||||||
| 28 | 'dump-blame=s' => \$self->{OPTS}->{'dump-blame'}, | ||||||||||||||||||||||||||||
| 29 | 'dump-rules=s' => \$self->{OPTS}->{'dump-rules'}, | ||||||||||||||||||||||||||||
| 30 | 'embed-blame=s' => \$self->{OPTS}->{'embed-blame'}, | ||||||||||||||||||||||||||||
| 31 | 'embed-rules=s' => \$self->{OPTS}->{'embed-rules'}, | ||||||||||||||||||||||||||||
| 32 | 'list-modules|l' => \$self->{OPTS}->{'list-modules'}, | ||||||||||||||||||||||||||||
| 33 | 'module|m=s' => \$self->{OPTS}->{module}, | ||||||||||||||||||||||||||||
| 34 | 'rules=s@' => \$self->{OPTS}->{rules}, | ||||||||||||||||||||||||||||
| 35 | 114 | ); | |||||||||||||||||||||||||||
| 36 | 114 | delete $arg_opts{'help|h'}; # skip in first args parsing -- will be accessable for modules | |||||||||||||||||||||||||||
| 37 | 114 | delete $arg_opts{'version|V'}; # --"-- | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 114 | return %arg_opts; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 114 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 114 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 114 | $self->index_modules(); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 114 | $self->{rules} = []; | |||||||||||||||||||||||||||
| 48 | 8 | map { push @{$self->{rules}}, @{$self->load_struct($_)} } | |||||||||||||||||||||||||||
| 49 | 8 | @{$self->{OPTS}->{rules}} | |||||||||||||||||||||||||||
| 50 | 114 | ▶ | if ($self->{OPTS}->{rules}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 114 | ▶ | if ($self->{OPTS}->{module} or @{$self->{rules}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"OPTS"}{"module"} or @{$self->{"rules"};}
MC/DC100%2/2
$self->{"OPTS"}{"module"} or @{$self->{"rules"};}
$self->{"OPTS"}{"module"}
@{$self->{"rules"};}
Truth table100%3/3
A or B
A= $self->{"OPTS"}{"module"}B= @{$self->{"rules"};}
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | log_info { "Explicit rules used: builtin will be ignored" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 54 | 106 | $self->{OPTS}->{'builtin-rules'} = undef; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | $self->{OPTS}->{'disable-module'} = | ||||||||||||||||||||||||||||
| 58 | 114 | { map { $_ => 1 } @{$self->{OPTS}->{'disable-module'}} }; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 114 | ▶ | sub defaults { | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | return { | ||||||||||||||||||||||||||||
| 63 | 114 | 'blame' => 1, # may be redefined per-rule | |||||||||||||||||||||||||||
| 64 | 'builtin-format' => 'RAW', | ||||||||||||||||||||||||||||
| 65 | 'modpath' => [ 'App::NDTools::NDProc::Module' ], | ||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub dump_blame { | ||||||||||||||||||||||||||
|
Subroutine
dump_blame: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 4 | my ($self, $blame) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | log_debug { "Dumping blame to $self->{OPTS}->{'dump-blame'}" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 73 | s_dump($self->{OPTS}->{'dump-blame'}, $self->{OPTS}->{ofmt}, | ||||||||||||||||||||||||||||
| 74 | 4 | $self->{OPTS}->{pretty}, $blame); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 74 | ▶ | sub dump_result { | ||||||||||||||||||||||||||
|
Subroutine
dump_result: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 74 | my ($self, $uri, $data) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | log_debug { "Dumping result to $uri" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 81 | 74 | s_dump($uri, $self->{OPTS}->{ofmt}, $self->{OPTS}->{pretty}, $data); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | sub dump_rules { | ||||||||||||||||||||||||||
|
Subroutine
dump_rules: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | for my $rule (@{$self->{rules}}) { | |||||||||||||||||||||||||||
| 88 | # remove undefs - defaults will be used anyway | ||||||||||||||||||||||||||||
| 89 | 9 | ▶ | map { defined $rule->{$_} || delete $rule->{$_} } keys %{$rule}; | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $rule->{$_} || delete $rule->{$_}
MC/DC50%1/2
defined $rule->{$_} || delete $rule->{$_}
defined $rule->{$_}
delete $rule->{$_}
Truth table66%2/3
A || B
A= defined $rule->{$_}B= delete $rule->{$_}
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | log_debug { "Dumping rules to $self->{OPTS}->{'dump-rules'}" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 92 | s_dump($self->{OPTS}->{'dump-rules'}, $self->{OPTS}->{ofmt}, | ||||||||||||||||||||||||||||
| 93 | 4 | $self->{OPTS}->{pretty}, $self->{rules}); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | sub embed { | ||||||||||||||||||||||||||
|
Subroutine
embed: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 4 | my ($self, $data, $path, $thing) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | my $spath = eval { str2path($path) }; | |||||||||||||||||||||||||||
| 100 | 4 | ▶ | die_fatal "Unable to parse '$path' ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 4 | my $ref = eval { (path($data, $spath, expand => 1))[0]}; | |||||||||||||||||||||||||||
| 102 | 4 | ▶ | die_fatal "Unable to lookup '$path' ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | ${$ref} = $self->{OPTS}->{'builtin-format'} eq 'RAW' | |||||||||||||||||||||||||||
| 105 | ? $thing | ||||||||||||||||||||||||||||
| 106 | 4 | ▶ | : s_encode($thing, $self->{OPTS}->{'builtin-format'}, {pretty => 0}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 113 | ▶ | sub exec { | ||||||||||||||||||||||||||
|
Subroutine
exec: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 113 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 113 | ▶ | if ($self->{OPTS}->{'list-modules'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | map { printf "%-16s %-8s %s\n", @{$_} } $self->list_modules; | |||||||||||||||||||||||||||
| 114 | 1 | die_info undef, 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 112 | my $mod; | |||||||||||||||||||||||||||
| 118 | 112 | ▶ | if (defined $self->{OPTS}->{module}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | die_fatal "Unknown module specified '$self->{OPTS}->{module}'", 1 | ||||||||||||||||||||||||||||
| 120 | 99 | ▶ | unless (exists $self->{MODS}->{$self->{OPTS}->{module}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 98 | $self->init_module($self->{OPTS}->{module}); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 98 | $mod = $self->{MODS}->{$self->{OPTS}->{module}}->new(); | |||||||||||||||||||||||||||
| 124 | 98 | for my $rule ($mod->parse_args($self->{ARGV})->get_opts()) { | |||||||||||||||||||||||||||
| 125 | $rule->{modname} = $self->{OPTS}->{module}, | ||||||||||||||||||||||||||||
| 126 | 92 | push @{$self->{rules}}, $rule; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # parse the rest of args (unrecognized by module (if was specified by args)) | ||||||||||||||||||||||||||||
| 131 | # to be sure there is no unsupported opts remain | ||||||||||||||||||||||||||||
| 132 | my @rest_opts = ( | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | 'help|h' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 1 | $self->{OPTS}->{help} = 1; | |||||||||||||||||||||||||||
| 135 | 1 | die "!FINISH"; | |||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||
| 137 | 1 | ▶ | 'version|V' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 1 | $self->{OPTS}->{version} = 1; | |||||||||||||||||||||||||||
| 139 | 1 | die "!FINISH"; | |||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||
| 141 | 104 | ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 104 | Getopt::Long::Configure('nopass_through'); | |||||||||||||||||||||||||||
| 144 | 104 | ▶ | unless (GetOptionsFromArray($self->{ARGV}, @rest_opts)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | defined $mod ? $mod->usage : $self->usage; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 2 | die_fatal "Unsupported opts passed", 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 102 | ▶ | if ($self->{OPTS}->{help}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 1 | $self->usage; | |||||||||||||||||||||||||||
| 151 | 1 | die_info, 0; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 101 | ▶ | if ($self->{OPTS}->{version}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | print $self->VERSION . "\n"; | |||||||||||||||||||||||||||
| 156 | 1 | die_info, 0; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 100 | ▶ | if ($self->{OPTS}->{'dump-rules'} and not @{$self->{ARGV}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"OPTS"}{"dump-rules"} and not @{$self->{"ARGV"};}
MC/DC100%2/2
$self->{"OPTS"}{"dump-rules"} and not @{$self->{"ARGV"};}
$self->{"OPTS"}{"dump-rules"}
not @{$self->{"ARGV"};}
Truth table100%3/3
A and not(B)
A= $self->{"OPTS"}{"dump-rules"}B= not @{$self->{"ARGV"};}
| |||||||||||||||||||||||||||||
| 160 | 3 | $self->dump_rules(); | |||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 97 | ▶ | $self->check_args(@{$self->{ARGV}}) or die_fatal undef, 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 96 | $self->process_args(@{$self->{ARGV}}); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 76 | die_info "All done", 0; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 114 | ▶ | sub index_modules { | ||||||||||||||||||||||||||
|
Subroutine
index_modules: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 114 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $required = { map { $_->{modname} => 1 } @{$self->{rules}} }; | |||||||||||||||||||||||||||
| 173 | 114 | ▶ | $required->{$self->{OPTS}->{module}} = 1 if ($self->{OPTS}->{module}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 114 | for my $path (@{$self->{OPTS}->{modpath}}) { | |||||||||||||||||||||||||||
| 176 | 114 | ▶ | log_trace { "Indexing modules in $path" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 177 | 114 | for my $m (findsubmod $path) { | |||||||||||||||||||||||||||
| 178 | 798 | $self->{MODS}->{(split('::', $m))[-1]} = $m; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 114 | return $self; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 216 | ▶ | sub init_module { | ||||||||||||||||||||||||||
|
Subroutine
init_module: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 216 | my ($self, $mod) = @_; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 216 | ▶ | return if ($self->{_initialized_mods}->{$mod}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | log_trace { "Inititializing module $mod ($self->{MODS}->{$mod})" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 191 | 120 | eval "require $self->{MODS}->{$mod}"; | |||||||||||||||||||||||||||
| 192 | 120 | ▶ | die_fatal "Failed to initialize module '$mod' ($@)", 1 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 120 | $self->{_initialized_mods}->{$mod} = 1; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | ▶ | sub list_modules { | ||||||||||||||||||||||||||
|
Subroutine
list_modules: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | return map { | ||||||||||||||||||||||||||||
| 200 | 7 | $self->init_module($_); | |||||||||||||||||||||||||||
| 201 | 7 | [ $_, $self->{MODS}->{$_}->VERSION, $self->{MODS}->{$_}->MODINFO ] | |||||||||||||||||||||||||||
| 202 | 1 | } sort keys %{$self->{MODS}}; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 140 | ▶ | sub load_uri { | ||||||||||||||||||||||||||
|
Subroutine
load_uri: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 140 | shift->load_struct(@_); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 10 | *load_source = \&load_uri; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 3 | ▶ | sub load_builtin_rules { | ||||||||||||||||||||||||||
|
Subroutine
load_builtin_rules: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 212 | 3 | my ($self, $data, $path) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | log_debug { "Loading builtin rules from '$path'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 215 | 3 | my $spath = eval { str2path($path) }; | |||||||||||||||||||||||||||
| 216 | 3 | ▶ | die_fatal "Unable to parse path ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 3 | my $rules = eval { (path($data, $spath, deref => 1, strict => 1))[0] }; | |||||||||||||||||||||||||||
| 218 | 3 | ▶ | die_fatal "Unable to lookup path ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 3 | ▶ | return $rules if ($self->{OPTS}->{'builtin-format'} eq 'RAW'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 1 | return s_decode($rules, $self->{OPTS}->{'builtin-format'}); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 96 | ▶ | sub process_args { | ||||||||||||||||||||||||||
|
Subroutine
process_args: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 225 | 96 | my $self = shift; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 96 | for my $uri (@_) { | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | log_info { "Processing $uri" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 229 | 98 | my $data = $self->load_uri($uri, $self->{OPTS}->{ifmt}); | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 98 | ▶ | if ($self->{OPTS}->{'builtin-rules'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 3 | $self->{rules} = $self->load_builtin_rules($data, $self->{OPTS}->{'builtin-rules'}); | |||||||||||||||||||||||||||
| 233 | # restore original rules - may be changed while processing structure | ||||||||||||||||||||||||||||
| 234 | $self->{OPTS}->{'embed-rules'} = $self->{OPTS}->{'builtin-rules'} | ||||||||||||||||||||||||||||
| 235 | 3 | ▶ | if (not defined $self->{OPTS}->{'embed-rules'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 98 | ▶ | if ($self->{OPTS}->{'dump-rules'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 1 | $self->dump_rules(); | |||||||||||||||||||||||||||
| 240 | 1 | next; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 97 | my @blame = $self->resolve_rules($uri)->process_rules(\$data); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 74 | ▶ | if ($self->{OPTS}->{'embed-blame'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 0 | ▶ | log_debug { "Embedding blame to '$self->{OPTS}->{'embed-blame'}'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 247 | 1 | $self->embed($data, $self->{OPTS}->{'embed-blame'}, \@blame); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 74 | ▶ | if ($self->{OPTS}->{'embed-rules'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | log_debug { "Embedding rules to '$self->{OPTS}->{'embed-rules'}'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 252 | 3 | $self->embed($data, $self->{OPTS}->{'embed-rules'}, $self->{rules}); | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 74 | $self->dump_result($uri, $data); | |||||||||||||||||||||||||||
| 256 | 74 | ▶ | $self->dump_blame(\@blame) if (defined $self->{OPTS}->{'dump-blame'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 94 | ▶ | sub process_rules { | ||||||||||||||||||||||||||
|
Subroutine
process_rules: called CC=15 SCAR=27.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | 94 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 94 | my $rnum = 0; # rule number | |||||||||||||||||||||||||||
| 264 | 94 | my @blame; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | RULE: | ||||||||||||||||||||||||||||
| 267 | 94 | for my $rule (@{$self->{resolved_rules}}) { | |||||||||||||||||||||||||||
| 268 | die_fatal "Unknown module specified ($rule->{modname}; rule #$rnum)", 1 | ||||||||||||||||||||||||||||
| 269 | 113 | ▶ | unless (exists $self->{MODS}->{$rule->{modname}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 112 | ▶ | if (exists $self->{OPTS}->{'disable-module'}->{$rule->{modname}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | log_debug { "Skip rule #$rnum (disabled module $rule->{modname})" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 273 | 6 | next; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 106 | ▶ | if ($rule->{disabled}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | log_debug { "Skip disabled rule #$rnum ($rule->{modname})" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 278 | 0 | next; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 106 | ▶ | for my $cond (defined $rule->{cond} ? @{$rule->{cond}} : ()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | log_debug { "Evaluating condition '$cond'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 4 | my $spath = eval { str2path($cond) }; | |||||||||||||||||||||||||||
| 285 | 4 | ▶ | die_fatal "Unable to parse '$cond' ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 4 | ▶ | unless (eval { path($data, $spath) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 2 | ▶ | die_fatal "Failed to evaluate cond '$cond' ($@)", 4 if ($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | log_info { "Skip rule #$rnum ($rule->{modname}) cond '$cond'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 291 | 2 | next RULE; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | log_debug { "Processing rule #$rnum ($rule->{modname})" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 296 | 104 | $self->init_module($rule->{modname}); | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 104 | ▶ | my $result = ref ${$data} ? dclone(${$data}) : ${$data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | my $source = exists $rule->{source} | ||||||||||||||||||||||||||||
| 300 | ? thaw($self->{sources}->{$rule->{source}}) | ||||||||||||||||||||||||||||
| 301 | 104 | ▶ | : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 104 | $self->{MODS}->{$rule->{modname}}->new->process($data, $rule, $source); | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 85 | my $changes = { rule_id => 0 + $rnum }; | |||||||||||||||||||||||||||
| 306 | 85 | ▶ | if (defined $rule->{blame} ? $rule->{blame} : $self->{OPTS}->{blame}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 307 | 83 | $changes->{diff} = diff($result, ${$data}, noU => 1); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 85 | ▶ | map { $changes->{$_} = $rule->{$_} if (defined $rule->{$_}) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | qw(blame comment source); # preserve useful info | ||||||||||||||||||||||||||||
| 312 | 85 | push @blame, dclone($changes); | |||||||||||||||||||||||||||
| 313 | } continue { | ||||||||||||||||||||||||||||
| 314 | 93 | $rnum++; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 74 | return @blame; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 97 | ▶ | sub resolve_rules { | ||||||||||||||||||||||||||
|
Subroutine
resolve_rules: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 321 | 97 | my ($self, $opt_src) = @_; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | log_debug { "Resolving rules" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 324 | 97 | $self->{resolved_rules} = dclone($self->{rules}); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 97 | for my $rule (@{$self->{resolved_rules}}) { | |||||||||||||||||||||||||||
| 327 | # single path may be specified as string, convert it to list | ||||||||||||||||||||||||||||
| 328 | $rule->{path}->[0] = delete $rule->{path} | ||||||||||||||||||||||||||||
| 329 | 116 | ▶ | if (exists $rule->{path} and not ref $rule->{path}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $rule->{"path"} and not ref $rule->{"path"}
MC/DC100%2/2
exists $rule->{"path"} and not ref $rule->{"path"}
exists $rule->{"path"}
not ref $rule->{"path"}
Truth table100%3/3
A and not(B)
A= exists $rule->{"path"}B= not ref $rule->{"path"}
| |||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 116 | ▶ | next unless (exists $rule->{source}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 43 | ▶ | unless (defined $rule->{source} and $rule->{source} ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $rule->{"source"} and $rule->{"source"} ne ""
MC/DC50%1/2
defined $rule->{"source"} and $rule->{"source"} ne ""
defined $rule->{"source"}
$rule->{"source"} ne ""
Truth table66%2/3
A and B
A= defined $rule->{"source"}B= $rule->{"source"} ne ""
| |||||||||||||||||||||||||||||
| 333 | # use processing doc as source | ||||||||||||||||||||||||||||
| 334 | 1 | $rule->{source} = $opt_src; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 43 | ▶ | next if (exists $self->{sources}->{$rule->{source}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | $self->{sources}->{$rule->{source}} = | ||||||||||||||||||||||||||||
| 338 | 42 | freeze($self->load_source($rule->{source}, $self->{OPTS}->{ifmt})); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 94 | return $self; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 10 | 1; # End of App::NDTools::NDProc | |||||||||||||||||||||||||||