DocKnot/Util.pm

filter: Statementstmt 100% 54 / 54 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 95.1% 79 / 83 CC 7 SCAR 6.7
CC
7
Cov
94%
latest_tarball
4.0
print_checked
2.0
print_fh
2.0
CRAP 2.0
« DocKnot/Config.pm ↑ Summary DocKnot.pm »
1 # Shared utility functions for other DocKnot modules.
2 #
3 # A collection of random utility functions that are used by more than one
4 # DocKnot module but don't make sense as App::DocKnot methods.
5 #
6 # SPDX-License-Identifier: MIT
7  
8 ##############################################################################
9 # Modules and declarations
10 ##############################################################################
11  
12 package App::DocKnot::Util v8.0.1;
13  
14 12 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use List::SomeUtils qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use Sort::Versions qw(versioncmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 12 our @EXPORT_OK = qw(is_newer latest_tarball print_checked print_fh);
25  
26 ##############################################################################
27 # Public interface
28 ##############################################################################
29  
30 # Check if a file, which may not exist, is newer than another list of files.
31 #
32 # $file - File whose timestamp to compare
33 # @others - Other files to compare against
34 #
35 # Returns: True if $file exists and is newer than @others, false otherwise
36 105 ▶sub is_newer {
Subroutine is_newer: called CC=2 SCAR=6.9
Pod documented
37 105 my ($file, @others) = @_;
38 105 ▶ return if !$file->exists();
Branch: 1 branch
BranchTrueFalseTotal
unless $file->exists 21 84 105
39 84 my $file_mtime = $file->stat()->[9];
40 84 my @others_mtimes = map { $_->stat()->[9] } @others;
41 84 ▶ return all { $file_mtime >= $_ } @others_mtimes;
Subroutine __ANON__: called CC=1 SCAR=0.0
42 }
43  
44 # Find the files for a given package with the latest version and return them
45 # along with some associated metadata.
46 #
47 # $path - Path::Tiny path to directory
48 # $tarname - Name of the tarball before the version component
49 #
50 # Returns: Anonymous hash with the following keys:
51 # version - Latest version found
52 # files - Array of files for that version
53 # or undef if no matching files were found
54 # Throws: Text exception on any error
55 6 ▶sub latest_tarball {
Subroutine latest_tarball: called CC=4 SCAR=13.9
Pod documented
56 6 my ($path, $tarname) = @_;
57  
58 # Collect the list of matching files and extract their version numbers.
59 6 ▶ return if !$path->is_dir();
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_dir 1 5 6
60 5 my $regex = qr{ \A \Q$tarname\E - (v?[\d.]+) [.] }xms;
61 5 my @files = map { $_->basename() } $path->children($regex);
62 5 ▶ my @versions = map { m{ $regex }xms ? [$1, $_] : () } @files;
Branch: 1 branch
BranchTrueFalseTotal
/ $regex /msux ? : 32 0 32
63 5 ▶ return if !@versions;
Branch: 1 branch
BranchTrueFalseTotal
unless @versions 0 5 5
64  
65 # Find the latest version and filter the list of files down to only that
66 # version. This will sort versions starting with v later than any version
67 # that doesn't start with v. This is fine for my purposes right now,
68 # since the conversion of Perl modules to semantic versions starting with
69 # v is one-way, but is not generally correct.
70 5 @versions = reverse(sort { versioncmp($a->[0], $b->[0]) } @versions);
71 5 my $latest = $versions[0][0];
72 5 @files = map { $_->[1] } grep { $_->[0] eq $latest } @versions;
73  
74 # Return the results.
75 return {
76 5 version => $latest,
77 files => \@files,
78 };
79 }
80  
81 # print with error checking. autodie unfortunately can't help us because
82 # print can't be prototyped and hence can't be overridden.
83 #
84 # @args - Arguments to print to stdout
85 #
86 # Returns: undef
87 # Throws: Text exception on output failure
88 40 ▶sub print_checked {
Subroutine print_checked: called CC=2 SCAR=7.1
Pod documented
89 40 my (@args) = @_;
90 40 ▶ print @args or croak('print failed');
Branch: 1 branch
BranchTrueFalseTotal
print @args or croak("print failed") 40 0 40
91 40 return;
92 }
93  
94 # print with error checking and an explicit file handle. autodie
95 # unfortunately can't help us because print can't be prototyped and
96 # hence can't be overridden.
97 #
98 # $fh - Output file handle
99 # $file - File name for error reporting
100 # @args - Remaining arguments to print
101 #
102 # Returns: undef
103 # Throws: Text exception on output failure
104 901 ▶sub print_fh {
Subroutine print_fh: called CC=2 SCAR=7.0
Pod documented
105 901 my ($fh, $file, @args) = @_;
106 901 ▶ print {$fh} @args or croak("cannot write to $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} @args or croak("cannot write to ${file}: $!") 901 0 901
107 901 return;
108 }
109  
110 ##############################################################################
111 # Module return value and documentation
112 ##############################################################################
113  
114 12 1;
115 __END__
« DocKnot/Config.pm ↑ Summary DocKnot.pm »