summaryrefslogtreecommitdiff
path: root/lib/App/DocKnot/Spin/Versions.pm
blob: 05537dd3e5e0e8b8e7310c3e8acc8ea7f6f323c3 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Parse data from a spin .versions file.
#
# The top of a web site source tree for use with spin may contain a .versions
# file, which is a database of software release versions and dates that
# support various rendering features for software pages.  This module parses
# that file into an internal data structure and answers questions about its
# contents.
#
# SPDX-License-Identifier: MIT

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

package App::DocKnot::Spin::Versions 7.01;

use 5.024;
use autodie;
use warnings;

use Path::Tiny qw(path);
use POSIX qw(mktime strftime);

##############################################################################
# File parsing
##############################################################################

# Parse a date/time in YYYY-mm-dd HH:MM:SS format in local time into seconds
# since epoch.  This duplicates Date::Parse, which is already a dependency,
# but this gives us more control over the format and better error reporting.
#
# $date - The date component
# $time - The time component
# $path - Path of file being parsed, for error reporting
#
# Returns: The time in seconds since epoch
#  Raises: Text exception if the date is invalid
sub _datetime_to_seconds {
    my ($date, $time, $path) = @_;

    # Check the data for validity.
    if ($date !~ m{ \A \d{4}-\d\d-\d\d \z }xms) {
        die qq(invalid date "$date" in $path\n);
    }
    if ($time !~ m{ \A \d\d:\d\d:\d\d \z }xms) {
        die qq(invalid time "$time" in $path\n);
    }

    # Parse and convert the date/time.
    my @datetime = reverse(split(m{ : }xms, $time));
    push(@datetime, reverse(split(m{ - }xms, $date)));
    $datetime[4]--;
    $datetime[5] -= 1900;
    $datetime[6] = 0;
    $datetime[7] = 0;
    $datetime[8] = -1;
    return mktime(@datetime);
}

# Parse the .versions file and populate the App::DocKnot::Spin::Versions
# object.
#
# Raises: autodie exception on file read errors
#         Text exception on file parsing errors
sub _read_data {
    my ($self) = @_;
    $self->{depends} = {};
    $self->{versions} = {};
    my $timestamp;

    my $lineno = 0;
    for my $line ($self->{path}->lines_utf8()) {
        $lineno++;
        next if $line =~ m{ \A \s* \z }xms;
        next if $line =~ m{ \A \s* \# }xms;

        # The list of files may be continued from a previous line.
        my @depends;
        if ($line =~ m{ \A \s }xms) {
            if (!defined($timestamp)) {
                die "continuation without previous entry in $self->{path}\n";
            }
            @depends = split(qr{ \s+ }xms, $line);
        } else {
            my @line = split(qr{ \s+ }xms, $line);
            my ($package, $version, $date, $time, @files) = @line;
            if (!defined($time)) {
                die "invalid line $lineno in $self->{path}\n";
            }
            @depends = @files;
            $timestamp = _datetime_to_seconds($date, $time, $self->{path});
            $date = strftime('%Y-%m-%d', gmtime($timestamp));
            $self->{versions}{$package} = [$version, $date];
        }

        # We now have the previous release time as a timestamp in $timestamp
        # and some set of files affected by that release in @depends.  Record
        # that as dependency information.
        for my $file (@depends) {
            $self->{depends}{$file} //= $timestamp;
            if ($self->{depends}{$file} < $timestamp) {
                $self->{depends}{$file} = $timestamp;
            }
        }
    }
    return;
}

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

# Parse a .versions file into a new App::DocKnot::Spin::Versions object.
#
# $path - Path to the .versions file
#
# Returns: Newly created object
#  Throws: Text exception on failure to parse the file
#          autodie exception on failure to read the file
sub new {
    my ($class, $path) = @_;

    # Create an empty object.
    my $self = { path => path($path) };
    bless($self, $class);

    # Parse the file into the newly-created object.
    $self->_read_data();

    # Return the populated object.
    return $self;
}

# Return the timestamp of the latest release affecting a different page.
#
# $file - File path that may be listed as an affected file for a release
#
# Returns: The timestamp in seconds since epoch of the latest release
#          affecting that file, or 0 if there are none
sub latest_release {
    my ($self, $file) = @_;
    return $self->{depends}{"$file"} // 0;
}

# Return the release date for a given package.
#
# $package - Name of the package
#
# Returns: Release date as a string in UTC, or undef if not known
sub release_date {
    my ($self, $package) = @_;
    my $version = $self->{versions}{$package};
    return defined($version) ? $version->[1] : undef;
}

# Update the version and release date for a package.  Add the change to Git if
# the .versions file is at the top of a Git repository.
#
# $package   - Name of the package
# $version   - New version
# $timestamp - New release date as seconds since epoch
#
# Throws: Text exception on failure
sub update_version {
    my ($self, $package, $version, $timestamp) = @_;
    my $date = strftime('%Y-%m-%d', localtime($timestamp));
    my $time = strftime('%H:%M:%S', localtime($timestamp));

    # Edits the line for the package to replace the version and release date.
    my $edit = sub {
        my ($product, $old_version, $old_date, $old_time) = split(q{ });
        return if $product ne $package;

        # We're going to replace the old version with the new one, but we need
        # to space-pad one or the other if they're not the same length.
        my $version_string = $version;
        while (length($old_version) > length($version_string)) {
            $version_string .= q{ };
        }
        while (length($old_version) < length($version_string)) {
            $old_version .= q{ };
        }

        # Make the replacement.
        my $line = $_;
        $line =~ s{ \Q$old_version\E }{$version_string}xms;
        $line =~ s{ \Q$old_date\E }{$date}xms;
        $line =~ s{ \Q$old_time\E }{$time}xms;
        $_ = $line;
    };

    # Apply that change to our versions file, and then re-read the contents to
    # update the internal data structure.
    $self->{path}->edit_lines_utf8($edit);
    $self->_read_data();
    return;
}

# Return the latest version for a given package.
#
# $package - Name of the package
#
# Returns: Latest version for package as a string, or undef if not known
sub version {
    my ($self, $package) = @_;
    my $version = $self->{versions}{$package};
    return defined($version) ? $version->[0] : undef;
}

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

1;
__END__

=for stopwords
Allbery DocKnot MERCHANTABILITY NONINFRINGEMENT sublicense YYYY-MM-DD

=head1 NAME

App::DocKnot::Spin::Versions - Parse package release information for spin

=head1 SYNOPSIS

    use App::DocKnot::Spin::Versions;
    my $versions = App::DocKnot::Spin::Versions('/path/to/.versions');
    my $version = $versions->version('some-package');
    my $release_date = $versions->release_date('some-package');
    my $timestamp = $versions->latest_release('some/file/index.th');

=head1 REQUIREMENTS

Perl 5.24 or later and the Path::Tiny module, available from CPAN.

=head1 DESCRIPTION

App::DocKnot::Spin supports a database of release information for packages
that may be referenced in the generated web site.  This is stored as the file
named F<.versions> at the top of the source tree.  This module parses that
file and provides an API to the information it contains.

The file should consist of lines (except for continuation lines, see below) in
the form:

    <package>  <version>  <date>  <time>  <files>

starting in the first column.  Each field is separated by one or more spaces
except the last, <files>, which is all remaining space-separated words of the
line.  Blank lines and lines starting with C<#> in the first column are
ignored.

The fields are:

=over 4

=item <package>

The name of a package.

=item <version>

The version number of the latest release of that package.

=item <date>

The date of the latest release of that package in YYYY-MM-DD format in the
local time zone.

=item <time>

The time of the latest release of that package in HH:MM:SS format in the local
time zone.

=item <files>

Any number of thread input files affected by this release, separated by
spaces.  The file names should be relative to the top of the source tree for
the web site.

=back

The <files> field can be continued on the following line by starting the line
with whitespace.  Each whitespace-separated word in a continuation line is
taken as an additional affected file for the previous line.

This information is used for the C<\version> and C<\release> thread commands
and to force regeneration of files affected by a release with a timestamp
newer than the timestamp of the corresponding output file.

=head1 CLASS METHODS

=over 4

=item new(PATH)

Create a new App::DocKnot::Spin::Versions object for the F<.versions> file
specified by PATH.

=back

=head1 INSTANCE METHODS

=over 4

=item latest_release(PATH)

Return the timestamp (in seconds since epoch) for the latest release affecting
PATH, or 0 if no releases affect that file.

=item release_date(PACKAGE)

Return the release date of the latest release of PACKAGE (in UTC), or C<undef>
if there is no release information for PACKAGE.

=item update_version(PACKAGE, VERSION, TIMESTAMP)

Given a new VERSION and TIMESTAMP (in seconds since epoch) for a release of
PACKAGE, update the release information in the F<.versions> file for that
package accordingly.  If the F<.versions> file is at the root of a Git
repository, this change will be staged with C<git add>.

=item version(PACKAGE)

Return the version of the latest release of PACKAGE, or C<undef> if there is
no release information for PACKAGE.

=back

=head1 AUTHOR

Russ Allbery <rra@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2004, 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<docknot(1)>, L<App::DocKnot::Spin>

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: