blib/lib/App/mkpkgconfig/PkgConfig.pm

filter: Statementstmt 97.3% 149 / 153 Branchbran 70.0% 35 / 50 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 27 / 27 Podpod 100% 13 / 13 totaltotal 90.2% 232 / 257 CC 37 SCAR 17.7
CC
37
Cov
90%
write
12.3
new_from
8.1
resolve_dependencies
8.0
CRAP 5.8
« script/mkpkgconfig ↑ Summary blib/lib/App/mkpkgconfig.pm »
1 package App::mkpkgconfig::PkgConfig;
2  
3 # ABSTRACT: output pkg-config .pc files
4  
5 3 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Regexp::Common 'balanced';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use App::mkpkgconfig::PkgConfig::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use constant Keyword => 'App::mkpkgconfig::PkgConfig::Entry::Keyword';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant Variable => 'App::mkpkgconfig::PkgConfig::Entry::Variable';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = 'v2.0.2';
16  
17 3 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶sub croak {
Subroutine croak: called CC=1 SCAR=0.0
21 4 require Carp;
22 4 goto &Carp::croak;
23 }
24  
25 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28  
29  
30  
31  
32 has _keywords => (
33 is => 'ro',
34 18 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 3 init_args => 'keywords',
36 );
37  
38 has _variables => (
39 is => 'ro',
40 18 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 3 init_args => 'variables',
42 );
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61 7 ▶sub new_from {
Subroutine new_from: called CC=8 SCAR=20.9
Pod documented
62 7 my $class = shift;
63 7 my $file = shift;
64  
65 7 ▶ open( my $fh, '<', $file )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("unable to open $file\n") 7 0 7
66 or croak( "unable to open $file\n" );
67  
68 7 my $pkg = $class->new;
69  
70 7 ▶ while ( defined( $_ = $fh->getline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $fh->getline)) 89 7 96
71  
72 89 ▶ next if /^\s*#/; # ignore comments
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 13 76 89
73 76 ▶ next if /^\s*$/; # ignore empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 13 63 76
74  
75 63 chomp;
76 ## no critic (ComplexRegexes)
77 63 ▶ croak( "unable to parse line: $_\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless / ^[\s]* (?<name>[^\s:=]+) \s* (?<op>[:=]) \s* (?<value>.*?) \s* (?:[#].*)? $ /x 0 63 63
78 unless m{
79 ^[\s]*
80 (?<name>[^\s:=]+)
81 \s*
82 (?<op>[:=])
83 \s*
84 (?<value>.*?)
85 \s*
86 (?:[#].*)?
87 $
88 }x;
89  
90 63 ▶ if ( $+{op} eq q{:} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'op'} eq ':') { } 28 35 63
91 28 $pkg->add_keyword( $+{name} => $+{value} );
92 }
93 else {
94 35 $pkg->add_variable( $+{name} => $+{value} );
95 }
96 }
97  
98 7 ▶ close $fh or croak;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak() 7 0 7
99  
100 7 return $pkg;
101 }
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112 13 ▶sub variable {
Subroutine variable: called CC=1 SCAR=0.0
Pod documented
113 13 return $_[0]->_variables->{ $_[1] };
114 }
115  
116  
117  
118  
119  
120  
121  
122  
123  
124 11 ▶sub variables {
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
125 11 return values %{ $_[0]->_variables };
126 }
127  
128  
129  
130  
131  
132  
133  
134  
135  
136 2 ▶sub keyword {
Subroutine keyword: called CC=1 SCAR=0.0
Pod documented
137 2 return $_[0]->_keywords->{ $_[1] };
138 }
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149 8 ▶sub keywords {
Subroutine keywords: called CC=1 SCAR=0.0
Pod documented
150 8 return values %{ $_[0]->_keywords };
151 }
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163 129 ▶sub add_variable {
Subroutine add_variable: called CC=2 SCAR=7.1
Pod documented
164 129 my ( $self, $name, $value ) = @_;
165  
166 129 ▶ croak( "attempt to set $name to an undefined value\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 129 129
167 unless defined $name;
168 129 $self->_variables->{$name} = Variable->new( $name, $value );
169 }
170  
171  
172  
173  
174  
175  
176  
177  
178  
179 14 ▶sub add_variables {
Subroutine add_variables: called CC=2 SCAR=6.9
Pod documented
180 14 my ( $self, $variables ) = @_;
181  
182 $self->add_variable( $_, $variables->{$_} )
183 14 for keys %{$variables};
184 }
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195 50 ▶sub add_keyword {
Subroutine add_keyword: called CC=2 SCAR=7.1
Pod documented
196 50 my ( $self, $name, $value ) = @_;
197  
198 50 ▶ croak( "attempt to set $name to an undefined value\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 50 50
199 unless defined $name;
200  
201 50 $self->_keywords->{$name} = Keyword->new( $name, $value );
202 }
203  
204  
205  
206  
207  
208  
209  
210  
211  
212 8 ▶sub add_keywords {
Subroutine add_keywords: called CC=2 SCAR=6.9
Pod documented
213 8 my ( $self, $keywords ) = @_;
214  
215 $self->add_keyword( $_, $keywords->{$_} )
216 8 for keys %{$keywords};
217 }
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251 9 ▶sub write { ## no critic (BuiltinHomonyms)
Subroutine write: called CC=12 SCAR=25.1
Pod documented
252 9 my ( $self, $file ) = ( shift, shift );
253  
254 9 my %options = (
255 vars => [],
256 write => 'all',
257 @_,
258 );
259  
260 9 ▶ my $fh
Branch: 2 branches
BranchTrueFalseTotal
my $fh = defined $file ? "IO::File"->new($file, "w") : "IO::Handle"->new_from_fd(fileno STDOUT, "w") or croak("unable to create ${file}: $!\n") 9 0 9
defined $file ? : 0 9 9
261 = defined $file
262 ? IO::File->new( $file, 'w' )
263 : IO::Handle->new_from_fd( fileno( STDOUT ), 'w' )
264 or croak( "unable to create $file: $!\n" );
265  
266 9 ▶ if ( $options{comments} && @{ $options{comments} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"comments"} and @{$options{"comments"};}) 6 3 9
Condition66%2/3
$options{"comments"} and @{$options{"comments"};}
!ll&&!rl&&r
306
MC/DC50%1/2
$options{"comments"} and @{$options{"comments"};}
$options{"comments"} @{$options{"comments"};}
Truth table66%2/3
A and B
A= $options{"comments"}B= @{$options{"comments"};}
ABresult
0X0
100
111
267 6 $fh->say( "# $_" ) for @{ $options{comments} };
268 6 $fh->say();
269 }
270  
271 9 my @entries = values %{ $self->_keywords };
272  
273 9 ▶ if ( $options{write} eq 'req' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'write'} eq 'req') { } 7 2 9
274  
275 7 ▶ if ( defined $options{vars} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"vars"}) 7 0 7
276 push @entries, $self->_variables->{$_} // croak( "request for an undefined variable: $_\n" )
277 7 ▶ for @{ $options{vars} };
Condition50%1/2
$self->_variables->{$_} // croak("request for an undefined variable: $_\n")
l!l
30
MC/DC0%0/1
$self->_variables->{$_} // croak("request for an undefined variable: $_\n")
$self->_variables->{$_}
Truth table50%1/2
A
A= $self->_variables->{$_}
Aresult
11
00
278 }
279 }
280 else {
281 2 push @entries, values %{ $self->_variables };
282 }
283  
284 9 my @vars_needed = $self->resolve_dependencies( @entries );
285  
286 43 $fh->say( "${_} = @{[ $self->_variables->{$_}->value ]}" )
287 9 foreach $self->order_variables( @vars_needed );
288  
289 9 $fh->say();
290  
291 25 $fh->say( "${_}: @{[ $self->_keywords->{$_}->value ]}" )
292 9 foreach order_keywords( keys %{ $self->_keywords } );
293  
294 9 return;
295 }
296  
297 4 ▶sub _entry_type {
Subroutine _entry_type: called CC=2 SCAR=7.6
298 4 ▶ $_[0]->isa( Keyword ) ? 'Keyword' : 'Variable';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa('App::mkpkgconfig::PkgConfig::Entry::Keyword') ? : 0 4 4
299 }
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311 18 ▶sub resolve_dependencies {
Subroutine resolve_dependencies: called CC=8 SCAR=20.8
Pod documented
312 18 my ( $self, @entries ) = @_;
313  
314 18 my %validated;
315 3 ▶ use Hash::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 # descend dependency tree. use an ordered hash to keep track of
318 # which variables are in the current tree, and an array of dependency
319 # arrays to keep track of each variable's dependencies. The ordered
320 # hash makes it easy to generate human readable error output.
321  
322 # could have used an actual tree, but only need a fraction of the
323 # functionality, and it's faster to check for duplicates in a hash
324 # than to compare tree nodes.
325  
326 18 for my $entry ( @entries ) {
327 64 my $track = Hash::Ordered->new;
328 64 $track->push( $entry->name, undef );
329 64 my @depends = ( [ $entry->depends ] );
330  
331 64 ▶ while ( @depends ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@depends) 138 60 198
332  
333 # check dependencies for last variable
334 138 ▶ while ( my $name = pop @{ $depends[-1] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = pop @{$depends[-1];}) 85 134 219
335 85 ▶ next if $validated{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $validated{$name} 0 85 85
336  
337 85 ▶ if ( $track->exists( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track->exists($name)) 3 82 85
338 3 croak(
339 sprintf(
340 "%s '%s' has a circular dependency: %s\n",
341 _entry_type( $entry ),
342 $entry->name, join( '->', $track->keys, $name ) ) );
343 }
344  
345 82 ▶ my $var = $self->_variables->{$name} // croak(
Condition100%2/2
$self->_variables->{$name} // croak(sprintf("%s '%s' depends upon an undefined variable: %s\n", _entry_type($entry), $entry->name, join("->", $track->keys, $name, "undef")))
l!l
811
MC/DC100%1/1
$self->_variables->{$name} // croak(sprintf("%s '%s' depends upon an undefined variable: %s\n", _entry_type($entry), $entry->name, join("->", $track->keys, $name, "undef")))
$self->_variables->{$name}
Truth table100%2/2
A
A= $self->_variables->{$name}
Aresult
11
00
346 sprintf(
347 "%s '%s' depends upon an undefined variable: %s\n",
348 _entry_type( $entry ),
349 $entry->name, join( '->', $track->keys, $name, 'undef' ),
350 ) );
351  
352 81 $track->push( $name, undef );
353 81 push @depends, [ $var->depends ];
354 }
355  
356 134 my ( $name ) = $track->pop;
357 134 $validated{$name} = undef;
358 134 pop @depends;
359 }
360  
361 60 ▶ delete $validated{ $entry->name } if $entry->isa( Keyword );
Branch: 1 branch
BranchTrueFalseTotal
if $entry->isa("App::mkpkgconfig::PkgConfig::Entry::Keyword") 26 34 60
362 }
363  
364 14 return keys %validated;
365 }
366  
367  
368  
369  
370  
371  
372  
373  
374  
375  
376 10 ▶sub order_variables {
Subroutine order_variables: called CC=7 SCAR=20.4
Pod documented
377  
378 10 my ( $self, @needed ) = @_;
379  
380 10 ▶ return () unless @needed;
Branch: 1 branch
BranchTrueFalseTotal
unless @needed 0 10 10
381  
382 10 @needed = do { my %uniqstr; @uniqstr{@needed} = (); keys %uniqstr; };
383  
384 my %dephash
385 10 ▶ = map { $_ => [ ( $self->_variables->{$_} // croak( "unknown variable: $_\n" ) )->depends ] }
Condition50%1/2
$self->_variables->{$_} // croak("unknown variable: $_\n")
l!l
480
MC/DC0%0/1
$self->_variables->{$_} // croak("unknown variable: $_\n")
$self->_variables->{$_}
Truth table50%1/2
A
A= $self->_variables->{$_}
Aresult
11
00
386 @needed;
387  
388 10 require Algorithm::Dependency::Ordered;
389 10 require Algorithm::Dependency::Source::HoA;
390  
391 10 my $ordered;
392  
393 eval {
394 10 ▶ my $deps
Branch: 1 branch
BranchTrueFalseTotal
my $deps = "Algorithm::Dependency::Ordered"->new("source", "Algorithm::Dependency::Source::HoA"->new(\%dephash)) or die "error creating dependency object\n" 10 0 10
395 = Algorithm::Dependency::Ordered->new(
396 source => Algorithm::Dependency::Source::HoA->new( \%dephash ) )
397 or die( "error creating dependency object\n" );
398  
399 10 $ordered = $deps->schedule( @needed );
400  
401 10 ▶ if ( !defined $ordered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ordered) 0 10 10
402 0 die( "error in variable dependencies: perhaps there's cycle?\n" );
403 }
404 10 1;
405 10 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $deps = "Algorithm::Dependency::Ordered"->new("source", "Algorithm::Dependency::Source::HoA"->new(\%dephash)) or die "error creating dependency object\n"; $ordered = $deps->schedule(@needed); unless (defined $ordered) { die "error in variable dependencies: perhaps there's cycle?\n"; } ; 1 } } or do { my $err = $@; require Data::Dumper; die $err, "Data::Dumper"->Dump([\%dephash, \@needed], ["deps", "needed"]) } 10 0 10
406 0 my $err = $@;
407 0 require Data::Dumper;
408 0 die( $err, Data::Dumper->Dump( [ \%dephash, \@needed ], [qw( deps needed )] ) );
409 };
410  
411 # move variables with no dependencies to the beginning of the list
412 # to make it more human friendly
413 10 my @nodeps = sort grep { !@{ $dephash{$_} } } @$ordered;
414  
415  
416 10 ▶ if ( @nodeps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodeps) 10 0 10
417 10 my %nodeps;
418 10 @nodeps{@nodeps} = ();
419 10 $ordered = [ @nodeps, grep { !exists $nodeps{$_} } @$ordered ];
420 }
421  
422 10 return @{$ordered};
423 }
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434 10 ▶sub order_keywords {
Subroutine order_keywords: called CC=1 SCAR=0.0
Pod documented
435 10 my ( @keywords ) = @_;
436  
437 10 my %keywords;
438 10 @keywords{@keywords} = ();
439  
440 10 my @first_keys = grep { exists $keywords{$_} } qw( Name Description Version );
441 10 my %last_keys;
442 10 @last_keys{@keywords} = ();
443 10 delete @last_keys{@first_keys};
444  
445 10 return @first_keys, keys %last_keys;
446 }
447  
448 3 1;
449  
450 #
451 # This file is part of App-mkpkgconfig
452 #
453 # This software is Copyright (c) 2020 by Smithsonian Astrophysical Observatory.
454 #
455 # This is free software, licensed under:
456 #
457 # The GNU General Public License, Version 3, June 2007
458 #
459  
460 __END__
« script/mkpkgconfig ↑ Summary blib/lib/App/mkpkgconfig.pm »