diff options
author | Russ Allbery <rra@cpan.org> | 2022-01-02 13:52:21 -0800 |
---|---|---|
committer | Russ Allbery <rra@cpan.org> | 2022-01-02 13:52:21 -0800 |
commit | 66d9ae6a41b46481882c10474d92b8633a11e279 (patch) | |
tree | 1d80641a9016258405ee1ea1394149617d52c372 | |
parent | abf331abc91809690467f83d15397cac4a09b8ae (diff) |
Convert App::DocKnot::Spin::Versions to Path::Tiny
Use Path::Tiny for all paths and file reading, and in the process
add better support for UTF-8. Track the path to the .versions file
in the object to support later methods to update a specific version.
-rw-r--r-- | lib/App/DocKnot/Spin.pm | 5 | ||||
-rw-r--r-- | lib/App/DocKnot/Spin/Versions.pm | 35 |
2 files changed, 21 insertions, 19 deletions
diff --git a/lib/App/DocKnot/Spin.pm b/lib/App/DocKnot/Spin.pm index eade414..628bc19 100644 --- a/lib/App/DocKnot/Spin.pm +++ b/lib/App/DocKnot/Spin.pm @@ -540,8 +540,7 @@ sub spin { } my $versions_path = $input->child('.versions'); if ($versions_path->exists()) { - $self->{versions} - = App::DocKnot::Spin::Versions->new("$versions_path"); + $self->{versions} = App::DocKnot::Spin::Versions->new($versions_path); } if ($input->child('.git')->is_dir()) { $self->{repository} = Git::Repository->new(work_tree => $input); @@ -741,7 +740,7 @@ Russ Allbery <rra@cpan.org> =head1 COPYRIGHT AND LICENSE -Copyright 1999-2011, 2013, 2021 Russ Allbery <rra@cpan.org> +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 diff --git a/lib/App/DocKnot/Spin/Versions.pm b/lib/App/DocKnot/Spin/Versions.pm index 679b368..c0e04f4 100644 --- a/lib/App/DocKnot/Spin/Versions.pm +++ b/lib/App/DocKnot/Spin/Versions.pm @@ -18,6 +18,7 @@ use 5.024; use autodie; use warnings; +use Path::Tiny qw(path); use POSIX qw(mktime strftime); ############################################################################## @@ -56,18 +57,18 @@ sub _datetime_to_seconds { return mktime(@datetime); } -# Parse a .versions file and populate the App::DocKnot::Spin::Versions object. -# -# $path - Path to the .versions file +# 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, $path) = @_; + my ($self) = @_; my $timestamp; - open(my $fh, '<', $path); - while (defined(my $line = <$fh>)) { + 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; @@ -75,17 +76,17 @@ sub _read_data { my @depends; if ($line =~ m{ \A \s }xms) { if (!defined($timestamp)) { - die "continuation without previous entry in $path\n"; + 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 $. in $path\n"; + die "invalid line $lineno in $self->{path}\n"; } @depends = @files; - $timestamp = _datetime_to_seconds($date, $time, $path); + $timestamp = _datetime_to_seconds($date, $time, $self->{path}); $date = strftime('%Y-%m-%d', gmtime($timestamp)); $self->{versions}{$package} = [$version, $date]; } @@ -100,7 +101,6 @@ sub _read_data { } } } - close($fh); return; } @@ -119,14 +119,17 @@ sub new { my ($class, $path) = @_; # Create an empty object. + #<<< my $self = { - depends => {}, + depends => {}, + path => path($path), versions => {}, }; + #>>> bless($self, $class); # Parse the file into the newly-created object. - $self->_read_data($path); + $self->_read_data(); # Return the populated object. return $self; @@ -134,13 +137,13 @@ sub new { # Return the timestamp of the latest release affecting a different page. # -# $file - File name that may be listed as an affected file for a release +# $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 $self->{depends}{"$file"} // 0; } # Return the release date for a given package. @@ -189,7 +192,7 @@ App::DocKnot::Spin::Versions - Parse package release information for spin =head1 REQUIREMENTS -Perl 5.24 or later. +Perl 5.24 or later and the Path::Tiny module, available from CPAN. =head1 DESCRIPTION @@ -284,7 +287,7 @@ Russ Allbery <rra@cpan.org> =head1 COPYRIGHT AND LICENSE -Copyright 2004, 2021 Russ Allbery <rra@cpan.org> +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 |