App-DocKnot

 view release on metacpan or  search on metacpan

lib/App/DocKnot/Spin/Versions.pm  view on Meta::CPAN

        }
    }
    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



( run in 0.718 second using v1.01-cache-2.11-cpan-2398b32b56e )