| 1 | # Release a distribution tarball for a package. | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # This is the implementation of the docknot release command, which copies a | ||||||||||||||||||||
| 4 | # release tarball (normally generated by docknot dist) into a publication | ||||||||||||||||||||
| 5 | # area, archives old versions, and updates the .versions database for spin. | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # SPDX-License-Identifier: MIT | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | ############################################################################## | ||||||||||||||||||||
| 10 | # Modules and declarations | ||||||||||||||||||||
| 11 | ############################################################################## | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package App::DocKnot::Release v8.0.1; | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | use 5.024; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use autodie; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 2 | ▶ | use App::DocKnot::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 2 | ▶ | use App::DocKnot::Spin::Versions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 2 | ▶ | use App::DocKnot::Util qw(latest_tarball); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 2 | ▶ | use List::Util qw(min); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 2 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | ############################################################################## | ||||||||||||||||||||
| 27 | # Public interface | ||||||||||||||||||||
| 28 | ############################################################################## | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Create a new App::DocKnot::Release object, which will be used for subsequent | ||||||||||||||||||||
| 31 | # calls. | ||||||||||||||||||||
| 32 | # | ||||||||||||||||||||
| 33 | # $args_ref - Anonymous hash of arguments with the following keys: | ||||||||||||||||||||
| 34 | # archivedir - Path to the archive directory | ||||||||||||||||||||
| 35 | # distdir - Path to where docknot dist puts distribution tarballs | ||||||||||||||||||||
| 36 | # metadata - Path to the package metadata | ||||||||||||||||||||
| 37 | # | ||||||||||||||||||||
| 38 | # Returns: Newly created object | ||||||||||||||||||||
| 39 | # Throws: Text exceptions on invalid package metadata | ||||||||||||||||||||
| 40 | # Text exceptions on invalid global configuration | ||||||||||||||||||||
| 41 | # Text exceptions on invalid distdir argument | ||||||||||||||||||||
| 42 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=23.7
Pod
documented
| |||||||||||||||||||||
| 43 | 3 | my ($class, $args_ref) = @_; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # Create the config reader. | ||||||||||||||||||||
| 46 | 3 | my %config_args; | |||||||||||||||||||
| 47 | 3 | ▶ | if ($args_ref->{metadata}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 3 | $config_args{metadata} = $args_ref->{metadata}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 3 | my $config_reader = App::DocKnot::Config->new(\%config_args); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # Load the global and package configuration. | ||||||||||||||||||||
| 53 | 3 | my $global_config_ref = $config_reader->global_config(); | |||||||||||||||||||
| 54 | 3 | my $config_ref = $config_reader->config(); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # Ensure we were given a valid archivedir and distdir arguments if they | ||||||||||||||||||||
| 57 | # were not set in the global configuration. | ||||||||||||||||||||
| 58 | my $archivedir = $args_ref->{archivedir} | ||||||||||||||||||||
| 59 | 3 | ▶ | // $global_config_ref->{archivedir}; | ||||||||||||||||||
|
Condition66%2/3
$args_ref->{"archivedir"} // $global_config_ref->{"archivedir"}
MC/DC0%0/2
$args_ref->{"archivedir"} // $global_config_ref->{"archivedir"}
$args_ref->{"archivedir"}
$global_config_ref->{"archivedir"}
Truth table66%2/3
A // B
A= $args_ref->{"archivedir"}B= $global_config_ref->{"archivedir"}
| |||||||||||||||||||||
| 60 | 3 | ▶ | if (!defined($archivedir)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 61 | 0 | croak('archivedir path not given'); | |||||||||||||||||||
| 62 | } elsif (!-d $archivedir) { | ||||||||||||||||||||
| 63 | 0 | croak( | |||||||||||||||||||
| 64 | "archivedir path $archivedir does not exist or is not a directory", | ||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 3 | ▶ | my $distdir = $args_ref->{distdir} // $global_config_ref->{distdir}; | ||||||||||||||||||
|
Condition66%2/3
$args_ref->{"distdir"} // $global_config_ref->{"distdir"}
MC/DC0%0/2
$args_ref->{"distdir"} // $global_config_ref->{"distdir"}
$args_ref->{"distdir"}
$global_config_ref->{"distdir"}
Truth table66%2/3
A // B
A= $args_ref->{"distdir"}B= $global_config_ref->{"distdir"}
| |||||||||||||||||||||
| 68 | 3 | ▶ | if (!defined($distdir)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 69 | 0 | croak('distdir path not given'); | |||||||||||||||||||
| 70 | } elsif (!-d $distdir) { | ||||||||||||||||||||
| 71 | 0 | croak("distdir path $distdir does not exist or is not a directory"); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # Build an App::DocKnot::Spin::Versions object if configured with a path | ||||||||||||||||||||
| 75 | # to a versions database. | ||||||||||||||||||||
| 76 | 3 | my $versions; | |||||||||||||||||||
| 77 | 3 | ▶ | if ($global_config_ref->{versions}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 2 | my $versions_path = path($global_config_ref->{versions}); | |||||||||||||||||||
| 79 | 2 | $versions = App::DocKnot::Spin::Versions->new($versions_path); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Create and return the object. | ||||||||||||||||||||
| 83 | my $self = { | ||||||||||||||||||||
| 84 | archivedir => path($archivedir), | ||||||||||||||||||||
| 85 | distdir => path($distdir), | ||||||||||||||||||||
| 86 | package => $config_ref->{name}, | ||||||||||||||||||||
| 87 | section => $config_ref->{distribution}{section}, | ||||||||||||||||||||
| 88 | tarname => $config_ref->{distribution}{tarname}, | ||||||||||||||||||||
| 89 | version_name => $config_ref->{distribution}{version}, | ||||||||||||||||||||
| 90 | 3 | versions => $versions, | |||||||||||||||||||
| 91 | }; | ||||||||||||||||||||
| 92 | 3 | bless($self, $class); | |||||||||||||||||||
| 93 | 3 | return $self; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # Release a new version and update .versions if so configured. | ||||||||||||||||||||
| 97 | # | ||||||||||||||||||||
| 98 | # Throws: Text exception on any failures | ||||||||||||||||||||
| 99 | 3 | ▶ | sub release { | ||||||||||||||||||
|
Subroutine
release: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||
| 100 | 3 | my ($self) = @_; | |||||||||||||||||||
| 101 | 3 | my $tarball_ref = latest_tarball($self->{distdir}, $self->{tarname}); | |||||||||||||||||||
| 102 | 3 | ▶ | if (!defined($tarball_ref)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | croak("no release of $self->{tarname} found in $self->{distdir}"); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # Archive old versions. This is only done if the current version in the | ||||||||||||||||||||
| 107 | # archive directory is different than the version we're about to release. | ||||||||||||||||||||
| 108 | # If it is not, we overwrite the version in the archive directory, since | ||||||||||||||||||||
| 109 | # we assume we're replacing a release. | ||||||||||||||||||||
| 110 | 3 | my $current_path = $self->{archivedir}->child($self->{section}); | |||||||||||||||||||
| 111 | 3 | my $current_ref = latest_tarball($current_path, $self->{tarname}); | |||||||||||||||||||
| 112 | 3 | ▶ | if (defined($current_ref)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 2 | ▶ | if ($current_ref->{version} ne $tarball_ref->{version}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 2 | my $old_root = $self->{archivedir}->child('ARCHIVE'); | |||||||||||||||||||
| 115 | 2 | my $old_path = $old_root->child($self->{tarname}); | |||||||||||||||||||
| 116 | 2 | $old_path->mkpath(); | |||||||||||||||||||
| 117 | 2 | for my $file ($current_ref->{files}->@*) { | |||||||||||||||||||
| 118 | 8 | $current_path->child($file)->move($old_path->child($file)); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # Copy the new version into place and update the symlinks. | ||||||||||||||||||||
| 124 | 3 | my @times; | |||||||||||||||||||
| 125 | 3 | $current_path->mkpath(); | |||||||||||||||||||
| 126 | 3 | for my $file ($tarball_ref->{files}->@*) { | |||||||||||||||||||
| 127 | 12 | my $source = $self->{distdir}->child($file); | |||||||||||||||||||
| 128 | 12 | my $dest = $current_path->child($file); | |||||||||||||||||||
| 129 | 12 | $source->copy($dest); | |||||||||||||||||||
| 130 | 12 | my ($atime, $mtime) = $source->stat()->@[8, 9]; | |||||||||||||||||||
| 131 | 12 | push(@times, $mtime); | |||||||||||||||||||
| 132 | 12 | ▶ | utime($atime, $mtime, $dest) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | or die "cannot reset timestamps of $dest: $!\n"; | ||||||||||||||||||||
| 134 | 12 | my $generic_name = $file; | |||||||||||||||||||
| 135 | 12 | $generic_name =~ s{ \A (\Q$self->{tarname}\E) - v?[\d.]+ [.] }{$1.}xms; | |||||||||||||||||||
| 136 | 12 | my $generic_path = $current_path->child($generic_name); | |||||||||||||||||||
| 137 | 12 | $generic_path->remove(); | |||||||||||||||||||
| 138 | 12 | symlink($file, $generic_path); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Update the .versions file. | ||||||||||||||||||||
| 142 | 3 | ▶ | if ($self->{versions}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 2 | my $name = $self->{version_name}; | |||||||||||||||||||
| 144 | 2 | my $version = $tarball_ref->{version}; | |||||||||||||||||||
| 145 | 2 | my $date = min(@times); | |||||||||||||||||||
| 146 | 2 | $self->{versions}->update_version($name, $version, $date); | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | 3 | return; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | ############################################################################## | ||||||||||||||||||||
| 152 | # Module return value and documentation | ||||||||||||||||||||
| 153 | ############################################################################## | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 2 | 1; | |||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||