| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 63 | 5 | ▶ | return if !@versions; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 107 | 901 | return; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | ############################################################################## | ||||||||||
| 111 | # Module return value and documentation | ||||||||||
| 112 | ############################################################################## | ||||||||||
| 113 | |||||||||||
| 114 | 12 | 1; | |||||||||
| 115 | __END__ | ||||||||||