| 1 |
#!/usr/bin/perl |
| 2 |
use v5.10; |
| 3 |
use open qw(:std :utf8); |
| 4 |
|
| 5 |
use lib qw(lib); |
| 6 |
use Business::ISBN::Data; |
| 7 |
|
| 8 |
my $file = $ARGV[0] // 'lib/Business/ISBN/RangeMessage.xml'; |
| 9 |
|
| 10 |
=encoding utf8 |
| 11 |
|
| 12 |
=head1 NAME |
| 13 |
|
| 14 |
make_default_data.pl - digest the latest RangeMessage.xml into default data in the module |
| 15 |
|
| 16 |
=head1 SYNOPSIS |
| 17 |
|
| 18 |
% curl https://www.isbn-international.org/export_rangemessage.xml > RangeMessage.xml |
| 19 |
% perl examples/make_default_data.pl RangeMessage.xml |
| 20 |
|
| 21 |
Without an argument, it assumes the file is lib/Business/ISBN/RangeMessage.xml |
| 22 |
|
| 23 |
% curl https://www.isbn-international.org/export_rangemessage.xml > lib/Business/ISBN/RangeMessage.xml |
| 24 |
% perl examples/make_default_data.pl |
| 25 |
|
| 26 |
=head1 DESCRIPTION |
| 27 |
|
| 28 |
This program takes the F<RangeMessage.xml> and makes the data |
| 29 |
structure to hard-code into L<Business::ISBN::Data>. |
| 30 |
|
| 31 |
The module ships with the F<RangeMessage.xml> that was current as of |
| 32 |
the release of the module, but it also hard-codes the same data. That |
| 33 |
way, the module has the data even if the XML file disappears. The |
| 34 |
module can also use any F<RangeMessage.xml> you specify, which allows |
| 35 |
you to use updated (or even past) data. The hard-coded data always |
| 36 |
gets you back to that for the release. |
| 37 |
|
| 38 |
=head1 SOURCE AVAILABILITY |
| 39 |
|
| 40 |
This module lives in a Github repository: |
| 41 |
|
| 42 |
https://github.com/briandfoy/business-isbn-data |
| 43 |
|
| 44 |
You are probably also interested in the module that uses it: |
| 45 |
|
| 46 |
https://github.com/briandfoy/business-isbn |
| 47 |
|
| 48 |
If you have something to add, create a fork on Github and send a |
| 49 |
pull request. |
| 50 |
|
| 51 |
=head1 AUTHOR |
| 52 |
|
| 53 |
brian d foy, C<< <[email protected]> >> |
| 54 |
|
| 55 |
Yakov Shafranovich updated the data in October 2008. |
| 56 |
|
| 57 |
Daniel Jakubik updated the data in July 2012. |
| 58 |
|
| 59 |
Markus Spann suggested looking for F<RangeMessage.xml> in the current |
| 60 |
directory to make it work with Perl app bundlers. |
| 61 |
|
| 62 |
Josef Moravec C<< <[email protected]> >> updated the data in January 2019. |
| 63 |
|
| 64 |
Peter Williams fixed a serious issue with ISBN-13 (GitHub #5) |
| 65 |
|
| 66 |
=head1 COPYRIGHT AND LICENSE |
| 67 |
|
| 68 |
Copyright © 2002-2023, brian d foy <[email protected]>. All rights reserved. |
| 69 |
|
| 70 |
You may redistribute this under the terms of the Artistic License 2.0. |
| 71 |
|
| 72 |
=cut |
| 73 |
|
| 74 |
|
| 75 |
do { warn "Usage: $0 RangeMessage.xml\n"; exit(2) } unless defined $file; |
| 76 |
do { warn "File <$file> does not exist\n"; exit(2) } unless -e $file; |
| 77 |
|
| 78 |
my $data = Business::ISBN::Data::_parse_range_message( $file ); |
| 79 |
|
| 80 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 81 |
# |
| 82 |
# |
| 83 |
# Thu, 16 Mar 2023 20:59:31 GMT |
| 84 |
say "DATE: $data->{_date}"; |
| 85 |
my( $day, $mon, $year ) = $data->{_date} =~ m/ |
| 86 |
# Thu, 16 Mar 2023 20:59:31 GMT |
| 87 |
\A |
| 88 |
\S+? |
| 89 |
, \s+ |
| 90 |
(?<day> \d+ ) \s |
| 91 |
(?<mon> [a-z]+ ) \s |
| 92 |
(?<year> \d+ ) |
| 93 |
/ix; |
| 94 |
my %months = map { state $n = 1; $_ => $n++ } |
| 95 |
qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
| 96 |
my( $year, $day ) = @+{qw(year day)}; |
| 97 |
my( $month ) = $months{ $+{mon} }; |
| 98 |
|
| 99 |
my $new_version_date = sprintf '%4d%02d%02d', $year, $month, $day; |
| 100 |
my $current_date = Business::ISBN::Data->VERSION; |
| 101 |
my( $major, $minor ) = split /\./, $current_date; |
| 102 |
|
| 103 |
my $new_minor = do { |
| 104 |
if( $new_version_date > $major ) { 1 } |
| 105 |
elsif( $new_version_date == $major ) { $minor + 1 } |
| 106 |
else { |
| 107 |
die "New date ($new_version_date) is older than current version ($current_date)\n"; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
my $new_version = sprintf '%s.%03d', $new_version_date, $new_minor; |
| 112 |
say "CURRENT VERSION: $current_date NEW VERSION: $new_version"; |
| 113 |
|
| 114 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 115 |
# |
| 116 |
# |
| 117 |
my $string = "\t(\n"; |
| 118 |
|
| 119 |
foreach my $key ( sort grep /^_/, keys %$data ) { |
| 120 |
my $value = "'$data->{$key}'"; |
| 121 |
$value = '__FILE__' if $key eq '_source'; |
| 122 |
$key = '_data_date' if $key eq '_date'; |
| 123 |
$string .= sprintf "\t%-10s => %s,\n", $key, $value; |
| 124 |
} |
| 125 |
|
| 126 |
$string .= "\t978 => \{\n%%978%%\n\t\},\n\t979 => \{\n%%979%%\n\t\},\n\t);"; |
| 127 |
|
| 128 |
foreach my $k ( qw(978 979) ) { |
| 129 |
my $s = "\t\t" . join "\n\t\t", |
| 130 |
map { |
| 131 |
my $group = $data->{$k}{$_}[0]; |
| 132 |
$group =~ s/'/\\'/g; |
| 133 |
|
| 134 |
my $numbers = join ", ", map { qq('$_') } $data->{$k}{$_}[1]->@*; |
| 135 |
|
| 136 |
my $s = sprintf q(%-5s => [ %-30s => [ %s ] ],), $_, qq('$group'), $numbers; |
| 137 |
$s; |
| 138 |
} |
| 139 |
sort { $a <=> $b } |
| 140 |
keys $data->{$k}->%*; |
| 141 |
|
| 142 |
$string =~ s/%%$k%%/$s/; |
| 143 |
} |
| 144 |
|
| 145 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 146 |
# |
| 147 |
# |
| 148 |
my $PM_FILE = 'lib/Business/ISBN/Data.pm'; |
| 149 |
my $TEMP_FILE = 'lib/Business/ISBN/Data.pm.tmp'; |
| 150 |
|
| 151 |
open my $in_fh, '<:encoding(UTF-8)', $PM_FILE |
| 152 |
or die "Could not open $PM_FILE: $!\n"; |
| 153 |
open my $out_fh, '>:encoding(UTF-8)', $TEMP_FILE |
| 154 |
or die "Could not open $TEMP_FILE: $!\n"; |
| 155 |
|
| 156 |
while(<$in_fh>) { |
| 157 |
state $in_replace = 0; |
| 158 |
if( s/\$VERSION = \K'\d+\.\d+'/'$new_version'/ ) { |
| 159 |
print {$out_fh} $_; |
| 160 |
next; |
| 161 |
} |
| 162 |
elsif( /\A# BEGIN REPLACE/ ) { |
| 163 |
$in_replace = 1; |
| 164 |
print {$out_fh} $_; |
| 165 |
next; |
| 166 |
} |
| 167 |
elsif( /\A# END REPLACE/ ) { |
| 168 |
$in_replace = 0; |
| 169 |
$string =~ s/\s*\z/\n/; |
| 170 |
print {$out_fh} $string; |
| 171 |
print {$out_fh} $_; |
| 172 |
next; |
| 173 |
} |
| 174 |
elsif( $in_replace ) { |
| 175 |
next; |
| 176 |
} |
| 177 |
elsif( ! $in_replace ) { |
| 178 |
print {$out_fh} $_; |
| 179 |
next; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
close($in_fh); |
| 184 |
close($out_fh); |
| 185 |
|
| 186 |
rename $TEMP_FILE => $PM_FILE or die "Could not replace $PM_FILE: $!\n"; |