ArduinoBuilder/Config.pm

filter: Statementstmt 76.9% 110 / 143 Branchbran 62.1% 46 / 74 Conditioncond 39.2% 11 / 28 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 68.0% 17 / 25 Podpod 0.0% 0 / 19 totaltotal 60.4% 185 / 306 CC 51 SCAR 19.7
CC
51
Cov
68%
merge
20.0
append
12.0
read_file
11.6
CRAP 7.1
« ArduinoBuilder/FilePath.pm ↑ Summary ArduinoBuilder/JsonTool.pm »
1 package App::ArduinoBuilder::Config;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our @EXPORT_OK = qw(get_os_name);
11  
12 # Reference for the whole configuration interpretation:
13 # https://arduino.github.io/arduino-cli/0.32/platform-specification
14  
15 13 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
16 13 my ($class, %options) = @_;
17 13 my $this = bless {config => {}, files => 0, options => {%options}}, $class;
18 13 ▶ $this->read_file($options{file}, %options) if $options{file};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"file"} 0 13 13
19 13 for my $f (@{$options{files}}) {
20 1 $this->read_file($f, %options, no_resolve => 1);
21 }
22 13 ▶ $this->resolve(%options) unless $options{no_resolve};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_resolve"} 13 0 13
23 13 return $this;
24 }
25  
26 2 ▶sub read_file {
Subroutine read_file: called CC=10 SCAR=24.5
Pod undocumented
27 2 my ($this, $file_name, %options) = @_;
28 2 %options = (%{$this->{options}}, %options);
29 2 ▶ return if $options{allow_missing} && ! -f $file_name;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"allow_missing"} and not -f $file_name 0 2 2
Condition33%1/3
$options{"allow_missing"} and not -f $file_name
!ll&&!rl&&r
200
MC/DC0%0/2
$options{"allow_missing"} and not -f $file_name
$options{"allow_missing"} not -f $file_name
Truth table33%1/3
A and not(B)
A= $options{"allow_missing"}B= not -f $file_name
ABresult
0X0
100
111
30 2 ▶ open my $fh, '<', $file_name or fatal "Can’t open '${file_name}': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file_name or fatal("Can\x{2019}t open '${file_name}': $!") 2 0 2
31 2 ▶ while (my $l = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh)) 12 2 14
32 12 ▶ next if $l =~ m/^\s*(?:#.*)?$/; # Only whitespace or comment
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^\s*(?:#.*)?$/ 4 8 12
33 8 ▶ fatal "Unparsable line in ${file_name}: ${l}" unless $l =~ m/^\s*([-0-9a-z_.]+?)\s*=\s*(.*?)\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $l =~ /^\s*([-0-9a-z_.]+?)\s*=\s*(.*?)\s*$/i 0 8 8
34 8 ▶ $this->{config}{$1} = $2 if !(exists $this->{config}{$1}) || $options{allow_override};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $this->{"config"}{$1} or $options{"allow_override"} 8 0 8
Condition33%1/3
not exists $this->{"config"}{$1} or $options{"allow_override"}
l!l&&r!l&&!r
800
MC/DC0%0/2
not exists $this->{"config"}{$1} or $options{"allow_override"}
not exists $this->{"config"}{$1} $options{"allow_override"}
Truth table33%1/3
not(A) or B
A= not exists $this->{"config"}{$1}B= $options{"allow_override"}
ABresult
1X1
011
000
35 }
36 2 ▶ $this->resolve(%options) unless $options{no_resolve};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_resolve"} 1 1 2
37 2 $this->{nb_files}++;
38 2 return 1;
39 }
40  
41 # Parses a Perl data-structure into this objet.
42 # $data should be an hash-ref.
43 4 ▶sub parse_perl {
Subroutine parse_perl: called CC=5 SCAR=16.1
Pod undocumented
44 4 my ($this, $data, %options) = @_;
45  
46 4 ▶ fatal 'Can’t parse non hash-ref data: "%s"', ref($data) unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 3 4
47 3 ▶ while (my ($k, $v) = each %{$data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 5 2 7
48 5 ▶ my $key = exists $options{prefix} ? $options{prefix}.'.'.$k : $k;
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'prefix'} ? : 2 3 5
49 5 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 2 3 5
50 2 $this->parse_perl($v, %options, prefix => $key);
51 } else {
52 3 $this->set($key => $v, %options);
53 }
54 }
55  
56 2 return 1;
57 }
58  
59 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($this) = @_;
61 0 return scalar keys %{$this->{config}};
62 }
63  
64 0 ▶sub empty {
Subroutine empty: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my ($this) = @_;
66 0 return $this->size() == 0;
67 }
68  
69 0 ▶sub nb_files {
Subroutine nb_files: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my ($this) = @_;
71 return $this->{nb_files}
72 0 }
73  
74 39 ▶sub get {
Subroutine get: called CC=8 SCAR=22.2
Pod undocumented
75 39 my ($this, $key, %options) = @_;
76 39 %options = (%{$this->{options}}, %options);
77 39 ▶ return $options{default} if !$this->exists($key) && exists $options{default};
Branch: 1 branch
BranchTrueFalseTotal
if not $this->exists($key) and exists $options{"default"} 0 39 39
Condition66%2/3
not $this->exists($key) and exists $options{"default"}
!ll&&!rl&&r
3450
MC/DC0%0/2
not $this->exists($key) and exists $options{"default"}
not $this->exists($key) exists $options{"default"}
Truth table66%2/3
not(A) and B
A= not $this->exists($key)B= exists $options{"default"}
ABresult
0X0
100
111
78 39 ▶ $options{allow_partial} = 1 if $options{no_resolve};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"no_resolve"} 0 39 39
79 39 my $v = _resolve_key($key, $this->{config}, %options, allow_partial => 1);
80 39 ▶ return $v if $options{allow_partial};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"allow_partial"} 35 4 39
81 4 ▶ fatal "Key '$key' does not exist in the configuration." unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 4 4
82 4 ▶ fatal "Key '$key' has unresolved reference to value '$1'." if $v =~ m/\{([^}]+)\}/ && !$options{allow_partial};
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\{([^}]+)\}/ and not $options{"allow_partial"} 0 4 4
Condition33%1/3
$v =~ /\{([^}]+)\}/ and not $options{"allow_partial"}
!ll&&!rl&&r
400
MC/DC0%0/2
$v =~ /\{([^}]+)\}/ and not $options{"allow_partial"}
$v =~ /\{([^}]+)\}/ not $options{"allow_partial"}
Truth table33%1/3
A and not(B)
A= $v =~ /\{([^}]+)\}/B= not $options{"allow_partial"}
ABresult
0X0
100
111
83 4 return $v;
84 }
85  
86 7 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
87 7 my ($this, %options) = @_;
88 7 return keys %{$this->{config}};
89 }
90  
91 # Returns the "top-level" keys (all the different first piece of all the keys).
92 0 ▶sub top_level_keys {
Subroutine top_level_keys: not called CC=1 SCAR=6.9
Pod undocumented
93 0 my ($this) = @_;
94 0 my %set = map { $_ =~ m/^([^.]*)/; $1 => 1 } $this->keys();
95 0 return CORE::keys %set;
96 }
97  
98 39 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
99 39 my ($this, $key) = @_;
100 39 return exists $this->{config}{$key};
101 }
102  
103 15 ▶sub set {
Subroutine set: called CC=4 SCAR=17.9
Pod undocumented
104 15 my ($this, $key, $value, %options) = @_;
105 15 %options = (%{$this->{options}}, %options);
106 15 ▶ if (exists $this->{config}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"config"}{$key}) 0 15 15
107 0 ▶ return if $options{ignore_existing};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"ignore_existing"} 0 0 0
108 0 ▶ fatal "Key '$key' already exists." unless $options{allow_override};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"allow_override"} 0 0 0
109 }
110 15 $this->{config}{$key} = $value;
111 15 return;
112 }
113  
114 0 ▶sub append {
Subroutine append: not called CC=3 SCAR=24.8
Pod undocumented
115 0 my ($this, $key, $value, $sep) = @_;
116 0 ▶ if ($this->{config}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'config'}{$key}) { } 0 0 0
117 0 ▶ $this->{config}{$key} .= ($sep // ' ').$value;
Condition0%0/2
$sep // " "
l!l
00
MC/DC0%0/1
$sep // " "
$sep
Truth table0%0/2
A
A= $sep
Aresult
11
00
118 } else {
119 0 $this->{config}{$key} = $value;
120 }
121 0 return;
122 }
123  
124 72 ▶sub _resolve_key {
Subroutine _resolve_key: called CC=8 SCAR=20.9
125 72 my ($key, $config, %options) = @_;
126 72 ▶ return $options{with}{$key} if exists $options{with}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"with"}{$key} 0 72 72
127 72 ▶ if (not exists $config->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{$key}) 28 44 72
128 28 ▶ return $options{base}->get($key, %options{grep { $_ ne 'base'} CORE::keys %options}) if exists $options{base};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"base"} 14 14 28
129 14 ▶ fatal "Can’t resolve key '${key}' in the configuration." unless $options{allow_partial};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"allow_partial"} 0 14 14
130 14 return;
131 }
132 44 my $value = $config->{$key};
133 44 ▶ return $value if $options{no_resolve};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"no_resolve"} 0 44 44
134 44 ▶ while ($value =~ m/\{([^{}]+)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\{([^{}]+)\}/g) 33 44 77
135 33 my $new_key = $1;
136 33 my $match_start = $-[0];
137 33 my $match_len = $+[0] - $-[0];
138 33 my $l = 2 + length($new_key);
139 33 my $new_value = _resolve_key($new_key, $config, %options);
140 33 ▶ substr $value, $match_start, $match_len, $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 19 14 33
141 }
142 44 return $value;
143 }
144  
145 # The Arduino OS name, based on the Perl OS name.
146 15 ▶sub get_os_name {
Subroutine get_os_name: called CC=5 SCAR=20.1
Pod undocumented
147 # It is debattable how we want to treat cygwin and msys. For now we assume
148 # that they will be used with a windows native Arduino toolchain.
149 15 ▶ return 'windows' if $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys" 0 15 15
Condition33%2/6
$^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys"
l!l&&r!l&&!r
0015
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
0015
MC/DC0%0/3
$^O eq "MSWin32" or $^O eq "cygwin" or $^O eq "msys"
$^O eq "MSWin32" $^O eq "cygwin" $^O eq "msys"
Truth table25%1/4
A or B or C
A= $^O eq "MSWin32"B= $^O eq "cygwin"C= $^O eq "msys"
ABCresult
1XX1
01X1
0011
0000
150 15 ▶ return 'macosx' if $^O eq 'MacOS';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MacOS" 0 15 15
151 15 return 'linux';
152 }
153  
154 # This only does the OS resolution, not each key/value interpretation as this
155 # should always be done as late as possible in case some values changes later.
156 #
157 # Should be called manually only if you call set() with OS specific keys (mainly
158 # in tests).
159 16 ▶sub resolve {
Subroutine resolve: called CC=4 SCAR=13.9
Pod undocumented
160 16 my ($this, %options) = @_;
161 16 %options = (%{$this->{options}}, %options);
162 16 my $config = $this->{config};
163 16 ▶ my $os_name = $options{force_os_name} // get_os_name();
Condition66%2/3
$options{"force_os_name"} // get_os_name()
l!l&&r!l&&!r
1150
MC/DC0%0/2
$options{"force_os_name"} // get_os_name()
$options{"force_os_name"} get_os_name()
Truth table66%2/3
A // B
A= $options{"force_os_name"}B= get_os_name()
ABresult
1X1
011
000
164 16 for my $k (CORE::keys %$config) {
165 17 ▶ $config->{$1} = $config->{$k} if $k =~ m/^(.*)\.$os_name$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(.*)\.$os_name$/ 1 16 17
166 }
167 16 return 1;
168 }
169  
170 # By default, definition from this are kept and not replaced by those from others.
171 0 ▶sub merge {
Subroutine merge: not called CC=4 SCAR=30.0
Pod undocumented
172 0 my ($this, $other, %options) = @_;
173 0 ▶ while (my ($k, $v) = each %{$other->{config}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$other{"config"};}) 0 0 0
174 0 ▶ $this->{config}{$k} = $v if $options{allow_override} || !exists $this->{config}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"allow_override"} or not exists $this->{"config"}{$k} 0 0 0
Condition0%0/3
$options{"allow_override"} or not exists $this->{"config"}{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"allow_override"} or not exists $this->{"config"}{$k}
$options{"allow_override"} not exists $this->{"config"}{$k}
Truth table0%0/3
A or not(B)
A= $options{"allow_override"}B= not exists $this->{"config"}{$k}
ABresult
1X1
011
000
175 }
176 }
177  
178 3 ▶sub filter {
Subroutine filter: called CC=3 SCAR=11.0
Pod undocumented
179 3 my ($this, $prefix) = @_;
180 3 my $filtered = App::ArduinoBuilder::Config->new();
181 3 $filtered->{options}{base} = $this;
182 3 ▶ while (my ($k, $v) = each %{$this->{config}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$this{"config"};}) 10 3 13
183 10 ▶ if ($k =~ m/^\Q$prefix\E\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\Q$prefix\E\./) 7 3 10
184 7 $filtered->{config}{substr($k, $+[0])} = $v;
185 }
186 }
187 3 return $filtered;
188 }
189  
190 0 ▶sub prefix {
Subroutine prefix: not called CC=2 SCAR=17.9
Pod undocumented
191 0 my ($this, $prefix) = @_;
192 0 my $prefixed = App::ArduinoBuilder::Config->new();
193 0 $prefixed->{options}{base} = $this;
194 0 ▶ while (my ($k, $v) = each %{$this->{config}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$this{"config"};}) 0 0 0
195 0 $prefixed->{config}{$prefix.'.'.$k} = $v;
196 }
197 0 return $prefixed;
198 }
199  
200 7 ▶sub dump {
Subroutine dump: called CC=3 SCAR=11.0
Pod undocumented
201 7 my ($this, $prefix) = @_;
202 7 my $out = '';
203 7 ▶ my $p = $prefix // '';
Condition100%2/2
$prefix // ""
l!l
16
MC/DC100%1/1
$prefix // ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
204 7 for my $k (sort($this->keys())) {
205 21 my $v = $this->get($k, allow_partial => 1);
206 21 $out .= "${p}${k}=${v}\n";
207 }
208 7 return $out;
209 }
210  
211 0 ▶sub get_hash {
Subroutine get_hash: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my ($this) = @_;
213 0 return %{$this->{config}}
214 }
215  
216 2 1;
« ArduinoBuilder/FilePath.pm ↑ Summary ArduinoBuilder/JsonTool.pm »