#!perl
use 5.040;
use warnings FATAL => 'all';
use re '/aasx';

# PODNAME: cpan-batch-release
# ABSTRACT: Release every Dist::Zilla distribution of a set of checkouts whose work is not all on CPAN

use App::BatchRelease();

exit App::BatchRelease->new->run(@ARGV);

__END__

=head1 SYNOPSIS

    cpan-batch-release --author PAUSEID [--author PAUSEID]... [--dry-run]
                       [--conformant-only] [--credentials] [--yes]
                       [--skip-login-check] [--pull-latest] [--jobs N] CHECKOUT...

    cpan-batch-release --author TESTER --dry-run ~/src/*
    cpan-batch-release --author TESTER --dry-run --pull-latest ~/src/*
    cpan-batch-release --author TESTER --conformant-only ~/src/*
    printf 'username: %s\npassword: %s\n\n' "$PAUSE_USER" "$PAUSE_PASSWORD" \
        | cpan-batch-release --author TESTER --credentials --yes ~/src/*

=head1 DESCRIPTION

Each CHECKOUT is a directory.  For each one, C<cpan-batch-release> finds out
whether it is a Dist::Zilla distribution whose work is not all on CPAN, as
L<App::BatchRelease::State> says, and releases all such distributions in one
run, with C<dzil release> in each checkout.  It asks for the PAUSE username
and password once, before the first release, and uses them for every upload.

It looks at each checkout first.  It fetches each one, and changes no work
tree while it looks, unless C<--pull-latest> tells it to:

=over

=item *

A path that is not a directory, has no F<dist.ini>, or is not the top of a
git checkout is skipped.

=item *

A fork is skipped: a distribution whose newest release on CPAN another PAUSE
author uploaded, one that no C<--author> names.  This check comes before the
fetch, so a fork is neither fetched nor pulled.  A distribution that was never
released is not a fork.

=item *

It runs C<git fetch> for the remote of the upstream of the default branch, on
a dry run too, because a fetch changes no branch and no work tree.  A
checkout whose fetch fails is skipped, with the error.  So is one whose
default branch has no upstream, because then nothing can tell whether
somebody pushed work that it does not have; C<git branch
--set-upstream-to> gives it one.  So is one whose default branch is behind
its upstream, such as C<2 commits behind origin/main>, because C<dzil
release> would upload the old work tree, and C<[Git::Push]> would find out
only after the upload.  A default branch that is ahead of its upstream is
fine: C<[Git::Push]> pushes it.  With C<--pull-latest>, the checkout is
brought up to date instead; see L</OPTIONS>.

=item *

A distribution whose F<dist.ini> is not committed on the default branch is
skipped.

=item *

The F<dist.ini> gives the name and the version.  MetaCPAN says which versions
of the distribution it has.  The tag of the version, C<$version> or
C<v$version>, is compared with the default branch.  A distribution that
MetaCPAN does not have, or whose version MetaCPAN does not have, is due.  A
distribution with commits after the tag of its version is due when the next
release gets a new version from the tags, as with C<[Git::NextVersion]>.
When the version is written in F<dist.ini>, it is skipped, with the advice to
raise the version, because PAUSE refuses a version a second time.

=item *

A distribution that is due is still skipped when its checkout is not on the
default branch, or has changes that are not committed, because C<dzil
release> releases the work tree.  With C<--conformant-only>, it is skipped
when it is not in full conformance, as L<Dist::Conformance> defines it.

=back

Then it prints the distributions to release, in an order where each comes
after the ones of the batch that it requires, and the ones it skips, each with
the reason.  The requirements come from the code, F<dist.ini> and
F<prereqs.yml>, without a build; see L<App::BatchRelease::Prereqs>.

Before it prints them, it runs the release tests of each distribution to
release, in that order, as C<dzil test --release> does, whether or not its
F<dist.ini> loads C<[TestRelease]>.  Each test run gets the F<lib/> of the
test build of each distribution of the batch that it requires.  A
distribution whose release tests fail is skipped, with the reason, and so is
each one that requires it.  The output of each test run shows as it runs.
When all have run, it removes the build of each one that passed, and keeps
the build of each one that failed under F<.build/> of its checkout.  A dry run
runs the release tests too, and then stops after the list.

Else it asks for confirmation, then for the PAUSE username and password, and
checks them with PAUSE.  Then it releases the distributions, one at a time, in
order.  The output of each C<dzil release> shows as it runs.  When a release
fails, the distributions of the batch that require it are skipped, and the
others are still released.  At the end it prints a summary of the
distributions released, skipped, with the reason, and failed, with the error.

=head1 OPTIONS

=over

=item --author PAUSEID

The PAUSE ID of an author whose distributions to release.  Give it once for
each author, for example for each co-maintainer who uploads.  Case does not
matter.  It is required.  A checkout whose newest release on CPAN is by
another author is a fork, and is skipped.

=item --dry-run, -n

Prints what it would release, in order, with the state of each, and what it
would skip, and stops.  It tags, commits and uploads nothing, reads nothing
from standard input and asks for nothing.  It still fetches, and runs the
release tests, which build each distribution.  With C<--pull-latest>, it
changes no checkout, and says what the pull would do.  It does not run the
release tests of a distribution that the pull would change, nor of one that
requires it, because the work tree is not yet the one that would be
released.  The list says so.

=item --pull-latest

Before the other checks, brings each checkout up to its upstream.  It checks
out the default branch, when the checkout is on another branch or a detached
head, and fast-forwards the default branch with C<git pull --ff-only>, when
it is behind its upstream.  A checkout that needs either and has changes in
the work tree, changed or untracked, is skipped, and so is a default branch
that is behind and ahead at once, which cannot fast-forward.  It never
stashes, merges, rebases or resets.

With C<--dry-run>, it does none of this.  It says what it would do, such as
C<would check out main and fast-forward 2 commits first>, and gives the state
that the distribution would have after the fast-forward.

=item --jobs N, -j N

Checks up to N checkouts at once, each in a child process: the fetch, the
state, the branch and the conformance.  Default 8.  With 1, it checks them one
after another.  The releases themselves always run one at a time, in order.

=item --conformant-only

Skips each distribution that is not in full conformance: one for which
C<conformance check> of L<Dist::Conformance> would report a failure, or
could not run, as when the checkout has no F<.conformance.yml>.

=item --credentials

Reads the PAUSE credentials from standard input, before anything else, as a
block of C<name: value> lines that ends at a blank line or at the end of the
input.  The names are C<username> and C<password>.  A name that the block does
not give is asked for at F</dev/tty>, and so is the confirmation, because
standard input is spoken for.  See L<App::BatchRelease::Credentials>.

=item --yes, -y

Releases without asking for confirmation.  A run with no terminal needs it.

=item --skip-login-check

Does not check the credentials with PAUSE before the first build.

=item --help, -h

Prints this.

=back

=head1 THE CREDENTIALS

The credentials stay in the memory of this program.  None is written to a
file, put on a command line or in the environment.  Each release runs in a
child process, made with C<fork>, which has them from its parent, and which
hands them to the C<[UploadToCPAN]> plugin of the distribution before the
release starts.  See L<App::BatchRelease::Release/HOW THE CREDENTIALS REACH
THE UPLOAD PLUGIN>.

The plugins of a distribution can still ask questions of their own, such as
C<[ConfirmRelease]>, which asks before each upload.  With no terminal, it
takes its default, which is no, unless C<DZIL_CONFIRMRELEASE_DEFAULT> is
true.

=head1 EXIT STATUS

0 when nothing failed, also when nothing was due or the person did not
confirm.  1 when a release failed.  2 for a mistake in the arguments, or a run
that stopped before the first release, such as an unanswered question or
credentials that PAUSE refused.

=head1 SEE ALSO

L<App::BatchRelease>

L<App::BatchRelease::State>

L<Dist::Conformance>

L<Dist::Zilla::Plugin::UploadToCPAN>

=cut
