DocKnot/Spin/Versions.pm

filter: Statementstmt 98.8% 86 / 87 Branchbran 96.1% 25 / 26 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 96.4% 134 / 139 CC 18 SCAR 15.7
CC
18
Cov
97%
_read_data
10.0
__ANON__
4.0
_datetime_to_seconds
3.0
CRAP 4.8
« DocKnot/Release.pm ↑ Summary DocKnot/Generate.pm »
1 # Parse data from a spin .versions file.
2 #
3 # The top of a web site source tree for use with spin may contain a .versions
4 # file, which is a database of software release versions and dates that
5 # support various rendering features for software pages. This module parses
6 # that file into an internal data structure and answers questions about its
7 # contents.
8 #
9 # SPDX-License-Identifier: MIT
10  
11 ##############################################################################
12 # Modules and declarations
13 ##############################################################################
14  
15 package App::DocKnot::Spin::Versions v8.0.1;
16  
17 4 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use POSIX qw(mktime strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ##############################################################################
25 # File parsing
26 ##############################################################################
27  
28 # Parse a date/time in YYYY-mm-dd HH:MM:SS format in local time into seconds
29 # since epoch. This duplicates Date::Parse, which is already a dependency,
30 # but this gives us more control over the format and better error reporting.
31 #
32 # $date - The date component
33 # $time - The time component
34 # $path - Path of file being parsed, for error reporting
35 #
36 # Returns: The time in seconds since epoch
37 # Raises: Text exception if the date is invalid
38 20 ▶sub _datetime_to_seconds {
Subroutine _datetime_to_seconds: called CC=3 SCAR=11.0
39 20 my ($date, $time, $path) = @_;
40  
41 # Check the data for validity.
42 20 ▶ if ($date !~ m{ \A \d{4}-\d\d-\d\d \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date =~ / \A \d{4}-\d\d-\d\d \z /msux) 1 19 20
43 1 die qq(invalid date "$date" in $path\n);
44 }
45 19 ▶ if ($time !~ m{ \A \d\d:\d\d:\d\d \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($time =~ / \A \d\d:\d\d:\d\d \z /msux) 1 18 19
46 1 die qq(invalid time "$time" in $path\n);
47 }
48  
49 # Parse and convert the date/time.
50 18 my @datetime = reverse(split(m{ : }xms, $time));
51 18 push(@datetime, reverse(split(m{ - }xms, $date)));
52 18 $datetime[4]--;
53 18 $datetime[5] -= 1900;
54 18 $datetime[6] = 0;
55 18 $datetime[7] = 0;
56 18 $datetime[8] = -1;
57 18 return mktime(@datetime);
58 }
59  
60 # Parse the .versions file and populate the App::DocKnot::Spin::Versions
61 # object.
62 #
63 # Raises: autodie exception on file read errors
64 # Text exception on file parsing errors
65 15 ▶sub _read_data {
Subroutine _read_data: called CC=10 SCAR=23.0
66 15 my ($self) = @_;
67 15 $self->{depends} = {};
68 15 $self->{versions} = {};
69 15 my $timestamp;
70  
71 15 my $lineno = 0;
72 15 for my $line ($self->{path}->lines_utf8()) {
73 25 $lineno++;
74 25 ▶ next if $line =~ m{ \A \s* \z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \s* \z /msux 1 24 25
75 24 ▶ next if $line =~ m{ \A \s* \# }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \s* \# /msux 1 23 24
76  
77 # The list of files may be continued from a previous line.
78 23 my @depends;
79 23 ▶ if ($line =~ m{ \A \s }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \A \s /msux) { } 2 21 23
80 2 ▶ if (!defined($timestamp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timestamp) 1 1 2
81 1 die "continuation without previous entry in $self->{path}\n";
82 }
83 1 @depends = split(qr{ \s+ }xms, $line);
84 } else {
85 21 my @line = split(qr{ \s+ }xms, $line);
86 21 my ($package, $version, $date, $time, @files) = @line;
87 21 ▶ if (!defined($time)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 1 20 21
88 1 die "invalid line $lineno in $self->{path}\n";
89 }
90 20 @depends = @files;
91 20 $timestamp = _datetime_to_seconds($date, $time, $self->{path});
92 18 $date = strftime('%Y-%m-%d', gmtime($timestamp));
93 18 $self->{versions}{$package} = [$version, $date];
94 }
95  
96 # We now have the previous release time as a timestamp in $timestamp
97 # and some set of files affected by that release in @depends. Record
98 # that as dependency information.
99 19 for my $file (@depends) {
100 19 ▶ $self->{depends}{$file} //= $timestamp;
Condition66%2/3
$self->{"depends"}{$file} //= $timestamp
l!l&&r!l&&!r
1180
MC/DC0%0/2
$self->{"depends"}{$file} //= $timestamp
$self->{"depends"}{$file} $timestamp
Truth table66%2/3
A //= B
A= $self->{"depends"}{$file}B= $timestamp
ABresult
1X1
011
000
101 19 ▶ if ($self->{depends}{$file} < $timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"depends"}{$file} < $timestamp) 1 18 19
102 1 $self->{depends}{$file} = $timestamp;
103 }
104 }
105 }
106 11 return;
107 }
108  
109 ##############################################################################
110 # Public interface
111 ##############################################################################
112  
113 # Parse a .versions file into a new App::DocKnot::Spin::Versions object.
114 #
115 # $path - Path to the .versions file
116 #
117 # Returns: Newly created object
118 # Throws: Text exception on failure to parse the file
119 # autodie exception on failure to read the file
120 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
121 13 my ($class, $path) = @_;
122  
123 # Create an empty object.
124 13 my $self = { path => path($path) };
125 13 bless($self, $class);
126  
127 # Parse the file into the newly-created object.
128 13 $self->_read_data();
129  
130 # Return the populated object.
131 9 return $self;
132 }
133  
134 # Return the timestamp of the latest release affecting a different page.
135 #
136 # $file - File path that may be listed as an affected file for a release
137 #
138 # Returns: The timestamp in seconds since epoch of the latest release
139 # affecting that file, or 0 if there are none
140 40 ▶sub latest_release {
Subroutine latest_release: called CC=2 SCAR=6.9
Pod documented
141 40 my ($self, $file) = @_;
142 40 ▶ return $self->{depends}{"$file"} // 0;
Condition100%2/2
$self->{"depends"}{"$file"} // 0
l!l
733
MC/DC100%1/1
$self->{"depends"}{"$file"} // 0
$self->{"depends"}{"$file"}
Truth table100%2/2
A
A= $self->{"depends"}{"$file"}
Aresult
11
00
143 }
144  
145 # Return the release date for a given package.
146 #
147 # $package - Name of the package
148 #
149 # Returns: Release date as a string in UTC, or undef if not known
150 9 ▶sub release_date {
Subroutine release_date: called CC=2 SCAR=6.9
Pod documented
151 9 my ($self, $package) = @_;
152 9 my $version = $self->{versions}{$package};
153 9 ▶ return defined($version) ? $version->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 8 1 9
154 }
155  
156 # Update the version and release date for a package. Add the change to Git if
157 # the .versions file is at the top of a Git repository.
158 #
159 # $package - Name of the package
160 # $version - New version
161 # $timestamp - New release date as seconds since epoch
162 #
163 # Throws: Text exception on failure
164 2 ▶sub update_version {
Subroutine update_version: called CC=1 SCAR=0.0
Pod documented
165 2 my ($self, $package, $version, $timestamp) = @_;
166 2 my $date = strftime('%Y-%m-%d', localtime($timestamp));
167 2 my $time = strftime('%H:%M:%S', localtime($timestamp));
168  
169 # Edits the line for the package to replace the version and release date.
170 4 ▶ my $edit = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
171 4 my ($product, $old_version, $old_date, $old_time) = split(q{ });
172 4 ▶ return if $product ne $package;
Branch: 1 branch
BranchTrueFalseTotal
if $product ne $package 2 2 4
173  
174 # We're going to replace the old version with the new one, but we need
175 # to space-pad one or the other if they're not the same length.
176 2 my $version_string = $version;
177 2 ▶ while (length($old_version) > length($version_string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $old_version > length $version_string) 0 2 2
178 0 $version_string .= q{ };
179 }
180 2 ▶ while (length($old_version) < length($version_string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $old_version < length $version_string) 3 2 5
181 3 $old_version .= q{ };
182 }
183  
184 # Make the replacement.
185 2 my $line = $_;
186 2 $line =~ s{ \Q$old_version\E }{$version_string}xms;
187 2 $line =~ s{ \Q$old_date\E }{$date}xms;
188 2 $line =~ s{ \Q$old_time\E }{$time}xms;
189 2 $_ = $line;
190 2 };
191  
192 # Apply that change to our versions file, and then re-read the contents to
193 # update the internal data structure.
194 2 $self->{path}->edit_lines_utf8($edit);
195 2 $self->_read_data();
196 2 return;
197 }
198  
199 # Return the latest version for a given package.
200 #
201 # $package - Name of the package
202 #
203 # Returns: Latest version for package as a string, or undef if not known
204 25 ▶sub version {
Subroutine version: called CC=2 SCAR=6.9
Pod documented
205 25 my ($self, $package) = @_;
206 25 my $version = $self->{versions}{$package};
207 25 ▶ return defined($version) ? $version->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 24 1 25
208 }
209  
210 ##############################################################################
211 # Module return value and documentation
212 ##############################################################################
213  
214 4 1;
215 __END__
« DocKnot/Release.pm ↑ Summary DocKnot/Generate.pm »