summaryrefslogtreecommitdiff
path: root/lib/App/DocKnot/Util.pm
blob: f705ca76317402c7a6c96897ba786dbbf8eb6fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Shared utility functions for other DocKnot modules.
#
# A collection of random utility functions that are used by more than one
# DocKnot module but don't make sense as App::DocKnot methods.
#
# SPDX-License-Identifier: MIT

##############################################################################
# Modules and declarations
##############################################################################

package App::DocKnot::Util 7.01;

use 5.024;
use autodie;
use warnings FATAL => 'utf8';

use Carp qw(croak);
use Exporter qw(import);
use List::SomeUtils qw(all);
use Path::Tiny qw(path);
use Sort::Versions qw(versioncmp);

our @EXPORT_OK = qw(is_newer latest_tarball print_checked print_fh);

##############################################################################
# Public interface
##############################################################################

# Check if a file, which may not exist, is newer than another list of files.
#
# $file   - File whose timestamp to compare
# @others - Other files to compare against
#
# Returns: True if $file exists and is newer than @others, false otherwise
sub is_newer {
    my ($file, @others) = @_;
    return if !$file->exists();
    my $file_mtime = $file->stat()->[9];
    my @others_mtimes = map { $_->stat()->[9] } @others;
    return all { $file_mtime >= $_ } @others_mtimes;
}

# Find the files for a given package with the latest version and return them
# along with some associated metadata.
#
# $path    - Path::Tiny path to directory
# $tarname - Name of the tarball before the version component
#
# Returns: Anonymous hash with the following keys:
#            version - Latest version found
#            files   - Array of files for that version
#          or undef if no matching files were found
#  Throws: Text exception on any error
sub latest_tarball {
    my ($path, $tarname) = @_;

    # Collect the list of matching files and extract their version numbers.
    return if !$path->is_dir();
    my $regex = qr{ \A \Q$tarname\E - ([\d.]+) [.] }xms;
    my @files = map { $_->basename() } $path->children($regex);
    my @versions = map { m{ $regex }xms ? [$1, $_] : () } @files;
    return if !@versions;

    # Find the latest version and filter the list of files down to only that
    # version.
    @versions = reverse(sort { versioncmp($a->[0], $b->[0]) } @versions);
    my $latest = $versions[0][0];
    @files = map { $_->[1] } grep { $_->[0] eq $latest } @versions;

    # Return the results.
    #<<<
    return {
        version => $latest,
        files   => \@files,
    };
    #<<<
}

# print with error checking.  autodie unfortunately can't help us because
# print can't be prototyped and hence can't be overridden.
#
# @args - Arguments to print to stdout
#
# Returns: undef
#  Throws: Text exception on output failure
sub print_checked {
    my (@args) = @_;
    print @args or croak('print failed');
    return;
}

# print with error checking and an explicit file handle.  autodie
# unfortunately can't help us because print can't be prototyped and
# hence can't be overridden.
#
# $fh   - Output file handle
# $file - File name for error reporting
# @args - Remaining arguments to print
#
# Returns: undef
#  Throws: Text exception on output failure
sub print_fh {
    my ($fh, $file, @args) = @_;
    print {$fh} @args or croak("cannot write to $file: $!");
    return;
}

##############################################################################
# Module return value and documentation
##############################################################################

1;
__END__

=for stopwords
Allbery DocKnot MERCHANTABILITY NONINFRINGEMENT sublicense FH autodie

=head1 NAME

App::DocKnot::Util - Shared utility functions for other DocKnot modules

=head1 SYNOPSIS

    use App::DocKnot::Util qw(
        is_newer latest_tarball print_checked print_fh
    );
    use Path::Tiny qw(path);

    print_checked('some stdout output');
    my @inputs = (path('/input-1'), path('/input-2'));
    if (!is_newer(path('/output'), @inputs)) {
        open(my $fh, '>', '/output');
        print_fh($fh, '/output', 'some stuff');
        close($fh);
    }

    my $latest_ref = latest_tarball(path('/archive'), 'App-Foo');

=head1 REQUIREMENTS

Perl 5.24 or later and the modules List::SomeUtils, Path::Tiny, and
Sort::Versions, available from CPAN.

=head1 DESCRIPTION

This module collects utility functions used by other App::DocKnot modules.  It
is not really intended for use outside of DocKnot, but these functions can be
used if desired.

=head1 FUNCTIONS

=over 4

=item is_newer(FILE, SOURCE[, SOURCE ...])

Returns a true value if FILE exists and has a last modified time that is newer
or equal to the last modified times of all SOURCE files, and otherwise returns
a false value.  Used primarily to determine if a given output file is
up-to-date with respect to its source files.  All paths must be Path::Tiny
objects.

=item latest_tarball(PATH, NAME)

Returns data including a file list for the latest tarballs (by version number)
for a given software package NAME in the directory PATH (which must be a
Path::Tiny object).  Versions are compared using Sort::Versions.  The return
valid is a hash with the following keys:

=over 4

=item files

The list of files found for that version.

=item version

The version number extracted from this set of files.

=back

=item print_checked(ARG[, ARG ...])

The same as print (without a file handle argument), except that it throws a
text exception on failure as if autodie affected print (which it unfortunately
doesn't because print cannot be prototyped).

=item print_fh(FH, NAME, DATA[, DATA ...])

Writes the concatenation of the DATA elements (interpreted as scalar strings)
to the file handle FH.  NAME should be the name of (or Path::Tiny object for)
the file open as FH, and is used for error reporting.

This is mostly equivalent to C<print {fh}> but throws a text exception in the
event of a failure.

=back

=head1 AUTHOR

Russ Allbery <rra@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 1999-2011, 2013, 2021-2022 Russ Allbery <rra@cpan.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=head1 SEE ALSO

L<App::DocKnot>

This module is part of the App-DocKnot distribution.  The current version of
DocKnot is available from CPAN, or directly from its web site at
L<https://www.eyrie.org/~eagle/software/docknot/>.

=cut

# Local Variables:
# copyright-at-end-flag: t
# End: