1#!/usr/bin/perl
2#
3# Check or update the version of Perl modules.
4#
5# Examines all module files (*.pm) under the lib directory and verifies that
6# the package is set to the same value as the current version number as
7# determined by the MYMETA.json file at the top of the source distribution.
8#
9# When given the --update option, instead fixes all of the Perl modules found
10# to have the correct version.
11#
12# SPDX-License-Identifier: MIT
13
14use 5.010;
15use strict;
16use warnings;
17
18use lib 't/lib';
19
20use Test::RRA qw(skip_unless_automated use_prereq);
21use Test::RRA::ModuleVersion qw(test_module_versions update_module_versions);
22
23use Getopt::Long qw(GetOptions);
24
25# If we have options, we're being run from the command line and always load
26# our prerequisite modules.  Otherwise, check if we have necessary
27# prerequisites and should run as a test suite.
28if (@ARGV) {
29    require JSON::PP;
30    require Perl6::Slurp;
31    Perl6::Slurp->import;
32} else {
33    skip_unless_automated('Module version tests');
34    use_prereq('JSON::PP');
35    use_prereq('Perl6::Slurp');
36}
37
38# Return the current version of the distribution from MYMETA.json in the
39# current directory.
40#
41# Returns: The version number of the distribution
42# Throws: Text exception if MYMETA.json is not found or doesn't contain a
43#         version
44sub dist_version {
45    my $json = JSON::PP->new->utf8(1);
46    my $metadata = $json->decode(scalar(slurp('MYMETA.json')));
47    my $version = $metadata->{version};
48    if (!defined($version)) {
49        die "$0: cannot find version number in MYMETA.json\n";
50    }
51    return $version;
52}
53
54# Get the version of the overall distribution.
55my $version = dist_version();
56
57# Main routine.  We run as either a test suite or as a script to update all of
58# the module versions, selecting based on whether we got the -u / --update
59# command-line option.
60my $update;
61Getopt::Long::config('bundling', 'no_ignore_case');
62GetOptions('update|u' => \$update) or exit 1;
63if ($update) {
64    update_module_versions('lib', $version);
65} else {
66    test_module_versions('lib', $version);
67}
68exit 0;
69__END__
70
71=for stopwords
72Allbery sublicense MERCHANTABILITY NONINFRINGEMENT CPAN rra-c-util
73
74=head1 NAME
75
76module-version.t - Check or update versions of Perl modules
77
78=head1 SYNOPSIS
79
80B<module-version.t> [B<--update>]
81
82=head1 REQUIREMENTS
83
84Perl 5.8 or later, the Perl6::Slurp module, and the JSON::PP Perl module, both
85of which are available from CPAN.  JSON::PP is also included in Perl core in
86Perl 5.14 and later.
87
88=head1 DESCRIPTION
89
90This script has a dual purpose as either a test script or a utility script.
91The intent is to assist with maintaining consistent versions in a Perl
92distribution, supporting both the package keyword syntax introduced in Perl
935.12 or the older explicit setting of a $VERSION variable.
94
95As a test, it reads the current version of a package from the F<MYMETA.json>
96file in the current directory (which should be the root of the distribution)
97and then looks for any Perl modules in F<lib>.  If it finds any, it checks
98that the version number of the Perl module matches the version number of the
99package from the F<MYMETA.json> file.  These test results are reported with
100Test::More, suitable for any TAP harness.
101
102As a utility script, when run with the B<--update> option, it similarly finds
103all Perl modules in F<lib> and then rewrites their version setting to match
104the version of the package as determined from the F<MYMETA.json> file.
105
106=head1 OPTIONS
107
108=over 4
109
110=item B<-u>, B<--update>
111
112Rather than test the Perl modules for the correct version, update all Perl
113modules found in the tree under F<lib> to the current version from the
114C<MYMETA.json> file.
115
116=back
117
118=head1 AUTHOR
119
120Russ Allbery <eagle@eyrie.org>
121
122=head1 COPYRIGHT AND LICENSE
123
124Copyright 2014-2016, 2019-2021 Russ Allbery <eagle@eyrie.org>
125
126Copyright 2013-2014 The Board of Trustees of the Leland Stanford Junior
127University
128
129Permission is hereby granted, free of charge, to any person obtaining a copy
130of this software and associated documentation files (the "Software"), to deal
131in the Software without restriction, including without limitation the rights
132to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
133copies of the Software, and to permit persons to whom the Software is
134furnished to do so, subject to the following conditions:
135
136The above copyright notice and this permission notice shall be included in all
137copies or substantial portions of the Software.
138
139THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
142AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
143LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
144OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
145SOFTWARE.
146
147=head1 SEE ALSO
148
149This module is maintained in the rra-c-util package.  The current version
150is available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.
151
152=cut
153
154# Local Variables:
155# copyright-at-end-flag: t
156# End:
157