summaryrefslogtreecommitdiff
path: root/lib/App/DocKnot/Release.pm
blob: 1634ddbb948b69e5645025d6438dff9c5e0154d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Release a distribution tarball for a package.
#
# This is the implementation of the docknot release command, which copies a
# release tarball (normally generated by docknot dist) into a publication
# area, archives old versions, and updates the .versions database for spin.
#
# SPDX-License-Identifier: MIT

##############################################################################
# Modules and declarations
##############################################################################

package App::DocKnot::Release 7.01;

use 5.024;
use autodie;
use warnings;

use App::DocKnot::Config;
use App::DocKnot::Spin::Versions;
use App::DocKnot::Util qw(latest_tarball);
use Carp qw(croak);
use List::Util qw(min);
use Path::Tiny qw(path);

##############################################################################
# Public interface
##############################################################################

# Create a new App::DocKnot::Release object, which will be used for subsequent
# calls.
#
# $args_ref - Anonymous hash of arguments with the following keys:
#   archivedir - Path to the archive directory
#   distdir    - Path to where docknot dist puts distribution tarballs
#   metadata   - Path to the package metadata
#
# Returns: Newly created object
#  Throws: Text exceptions on invalid package metadata
#          Text exceptions on invalid global configuration
#          Text exceptions on invalid distdir argument
sub new {
    my ($class, $args_ref) = @_;

    # Create the config reader.
    my %config_args;
    if ($args_ref->{metadata}) {
        $config_args{metadata} = $args_ref->{metadata};
    }
    my $config_reader = App::DocKnot::Config->new(\%config_args);

    # Load the global and package configuration.
    my $global_config_ref = $config_reader->global_config();
    my $config_ref = $config_reader->config();

    # Ensure we were given a valid archivedir and distdir arguments if they
    # were not set in the global configuration.
    my $archivedir = $args_ref->{archivedir}
      // $global_config_ref->{archivedir};
    if (!defined($archivedir)) {
        croak('archivedir path not given');
    } elsif (!-d $archivedir) {
        croak(
            "archivedir path $archivedir does not exist or is not a directory",
        );
    }
    my $distdir = $args_ref->{distdir} // $global_config_ref->{distdir};
    if (!defined($distdir)) {
        croak('distdir path not given');
    } elsif (!-d $distdir) {
        croak("distdir path $distdir does not exist or is not a directory");
    }

    # Build an App::DocKnot::Spin::Versions object if configured with a path
    # to a versions database.
    my $versions;
    if ($global_config_ref->{versions}) {
        my $versions_path = path($global_config_ref->{versions});
        $versions = App::DocKnot::Spin::Versions->new($versions_path);
    }

    # Create and return the object.
    #<<<
    my $self = {
        archivedir   => path($archivedir),
        distdir      => path($distdir),
        package      => $config_ref->{name},
        section      => $config_ref->{distribution}{section},
        tarname      => $config_ref->{distribution}{tarname},
        version_name => $config_ref->{distribution}{version},
        versions     => $versions,
    };
    #>>>
    bless($self, $class);
    return $self;
}

# Release a new version and update .versions if so configured.
#
# Throws: Text exception on any failures
sub release {
    my ($self) = @_;
    my $tarball_ref = latest_tarball($self->{distdir}, $self->{tarname});
    if (!defined($tarball_ref)) {
        croak("no release of $self->{tarname} found in $self->{distdir}");
    }

    # Archive old versions.  This is only done if the current version in the
    # archive directory is different than the version we're about to release.
    # If it is not, we overwrite the version in the archive directory, since
    # we assume we're replacing a release.
    my $current_path = $self->{archivedir}->child($self->{section});
    my $current_ref = latest_tarball($current_path, $self->{tarname});
    if (defined($current_ref)) {
        if ($current_ref->{version} ne $tarball_ref->{version}) {
            my $old_root = $self->{archivedir}->child('ARCHIVE');
            my $old_path = $old_root->child($self->{tarname});
            $old_path->mkpath();
            for my $file ($current_ref->{files}->@*) {
                $current_path->child($file)->move($old_path->child($file));
            }
        }
    }

    # Copy the new version into place and update the symlinks.
    my @times;
    $current_path->mkpath();
    for my $file ($tarball_ref->{files}->@*) {
        my $source = $self->{distdir}->child($file);
        my $dest = $current_path->child($file);
        $source->copy($dest);
        my ($atime, $mtime) = $source->stat()->@[8, 9];
        push(@times, $mtime);
        utime($atime, $mtime, $dest)
          or die "cannot reset timestamps of $dest: $!\n";
        my $generic_name = $file;
        $generic_name =~ s{ \A (\Q$self->{tarname}\E) - [\d.]+ [.] }{$1.}xms;
        my $generic_path = $current_path->child($generic_name);
        $generic_path->remove();
        symlink($file, $generic_path);
    }

    # Update the .versions file.
    if ($self->{versions}) {
        my $name = $self->{version_name};
        my $version = $tarball_ref->{version};
        my $date = min(@times);
        $self->{versions}->update_version($name, $version, $date);
    }
    return;
}

##############################################################################
# Module return value and documentation
##############################################################################

1;
__END__

=for stopwords
Allbery DocKnot MERCHANTABILITY NONINFRINGEMENT sublicense archivedir distdir

=head1 Name

App::DocKnot::Release - Release a distribution tarball

=head1 SYNOPSIS

    use App::DocKnot::Release;
    my $docknot = App::DocKnot::Release->new();
    $docknot->release();

=head1 REQUIREMENTS

Perl 5.24 or later and the modules File::BaseDir, File::ShareDir,
Git::Repository, Path::Tiny, and YAML::XS, all of which are available from
CPAN.

=head1 DESCRIPTION

This component of DocKnot releases a distribution tarball (normally created by
C<docknot dist> or App::DocKnot::Dist), maintains a software distribution
directory, and updates a version and release date database.

=head1 CLASS METHODS

=over 4

=item new(ARGS)

Create a new App::DocKnot::Release object.  This should be used for all
subsequent actions.  ARGS should be a hash reference with one or more of the
following keys:

=over 4

=item archivedir

The release area into which to put the distribution tarball.  The current
distribution will be put in a subdirectory named after the
C<distribution.section> key in the package configuration.  Older versions will
be moved to the F<ARCHIVE> subdirectory of I<archivedir>.  Required if not set
in the global configuration file.

=item distdir

The directory from which to get the new distribution tarball, normally
generated by C<docknot dist>.  The latest version in this directory will be
used.  Required if not set in the global configuration file.

=item metadata

The path to the metadata for the package on which to operate.  Default:
F<docs/docknot.yaml> relative to the current directory.

=back

=back

=head1 INSTANCE METHODS

=over 4

=item release()

Copy the distribution tarball (in multiple formats, with PGP signatures) into
a release area, updates symlink pointing to the latest version, and move any
old release to an archive area.

If C<versions> is set in the global configuration file, updates the
F<.versions> file found at that path with the new release version and release
date.  See L<App::DocKnot::Spin::Versions> for more information about
F<.versions> files.

=back

=head1 AUTHOR

Russ Allbery <rra@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=head1 SEE ALSO

L<docknot(1)>, L<App::DocKnot::Config>, L<App::DocKnot::Dist>,
L<App::DocKnot::Spin::Versions>

This module is part of the App-DocKnot distribution.  The current version of
DocKnot is available from CPAN, or directly from its web site at
L<https://www.eyrie.org/~eagle/software/docknot/>.

=cut

# Local Variables:
# copyright-at-end-flag: t
# End: