App-BatchRelease

 view release on metacpan or  search on metacpan

lib/App/BatchRelease/Release.pm  view on Meta::CPAN

sub in_child ( $what, $root, $run, %options ) {
    STDOUT->flush;
    STDERR->flush;
    pipe( my $read, my $write ) or return { error => "pipe: $!" };
    my $pid = fork // return { error => "fork: $!" };
    if ( !$pid ) {
        close $read or POSIX::_exit(1);
        child( $what, $root, $run, $write, %options );
    }
    close $write or return { error => "close: $!" };
    my $built = do { local $/ = undef; <$read> }
      // q{};
    close $read or return { error => "close: $!" };
    chomp $built;
    waitpid( $pid, 0 ) == $pid or return { error => "waitpid: $!" };
    my $status  = $?;
    my %outcome = ( built => $built eq q{} ? undef : $built );
    return { %outcome, error => undef }                                                 if !$status;
    return { %outcome, error => "the $what was killed by signal " . ( $status & 127 ) } if $status & 127;
    return { %outcome, error => "the $what exited " . ( $status >> 8 ) };
}


sub child ( $what, $root, $run, $report, %options ) {    ## no critic (Subroutines::RequireFinalReturn) It ends in _exit, which does not return.
    my $ok = eval {
        if ( defined $options{stdin} ) {
            open( STDIN, '<', $options{stdin} ) or die "Cannot read standard input from $options{stdin}: $!\n";
        }
        local $ENV{PERL5LIB} = join q{:}, grep { $_ ne q{} } @{ $options{lib} // [] }, $ENV{PERL5LIB} // q{};
        my $built = $run->();
        print {$report} "$built\n" if defined $built;
        1;
    };

    # The transcript of the batch, for the person who runs it, and not a diagnostic of this code.
    print {*STDERR} "The $what of $root failed: $@" if !$ok;    ## no critic (ProhibitPrintSTDERR)
    STDOUT->flush;
    STDERR->flush;
    close $report or $ok = 0;
    POSIX::_exit( $ok ? 0 : 1 );
}


sub dzil ( $root, $credentials, @command ) {
    chdir $root or die "chdir $root: $!\n";
    my $app = Dist::Zilla::App->new;
    my ( $command, $options, @args ) = $app->prepare_command(@command);
    my $zilla = $command->zilla;
    hand_credentials( $zilla, $credentials ) if defined $credentials;
    $app->execute_command( $command, $options, @args );
    my $built = $zilla->built_in // return;
    return Mojo::File->new("$built")->to_abs->to_string;
}


sub hand_credentials ( $zilla, $credentials ) {
    my @uploaders = grep { $_->isa('Dist::Zilla::Plugin::UploadToCPAN') } @{ $zilla->plugins };
    for my $plugin (@uploaders) {
        my $meta = $plugin->meta;
        $meta->find_attribute_by_name('username')->set_value( $plugin, $credentials->username );
        $meta->find_attribute_by_name('password')->set_value( $plugin, $credentials->password );
    }
    return scalar @uploaders;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::BatchRelease::Release - Run the release tests or the release of one Dist::Zilla distribution, with credentials handed over in memory

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use 5.040;
    use App::BatchRelease::Credentials;
    use App::BatchRelease::Release;

    my $credentials = App::BatchRelease::Credentials->new( username => 'TESTER', password => 'bogus' );
    my $outcome = App::BatchRelease::Release::release( '/bogus/Foo-Bar', $credentials );
    print defined $outcome->{error} ? "failed: $outcome->{error}\n" : "released from $outcome->{built}\n";

=head1 DESCRIPTION

L</release> does what C<dzil release> does in a checkout, with one change:
the C<[UploadToCPAN]> plugins of the distribution get the PAUSE username and
password from this program, so none of them asks for them, and none reads
F<~/.pause>.

L</test> does what C<dzil test --release --keep-build-dir> does, before the
batch, and uploads nothing.

=head2 HOW THE CREDENTIALS REACH THE UPLOAD PLUGIN

L<Dist::Zilla::Plugin::UploadToCPAN> takes its C<username> and C<password>
from the first of: its C<credentials_stash>, default C<%PAUSE>, as
F<~/.dzil/config.ini> sets it; F<~/.pause>; a prompt.  Both attributes are
lazy, and C<password> takes no value from F<dist.ini>.

This program runs the release in a child process, made with C<fork>, so the
child has the credentials in its memory from the parent, and nothing carries
them between the two.  The child builds the Dist::Zilla object as the
C<dzil release> command builds it, with L<Dist::Zilla::App>, so the global
configuration and its stashes still apply.  Then, before the release starts,
it sets the C<username> and C<password> of every plugin that is an
L<Dist::Zilla::Plugin::UploadToCPAN>, through the meta-object of the class.
A lazy attribute that has a value does not run its default, so neither a
stash, nor F<~/.pause>, nor a prompt is asked.  That holds whatever the
F<dist.ini> names as its stash.

A child also keeps the batch apart: each release and each test run loads the
plugins of its own distribution into a fresh process, and one that dies, or
changes the directory or the environment, takes only its own process with it.

=head1 FUNCTIONS

=head2 release

C<($root, $credentials, %options)>.  Releases the distribution in the
checkout C<$root>, as C<dzil release> does, in a child process, through
L</in_child>, with the credentials handed to the Dist::Zilla object.

Returns a HashRef, what L</in_child> returns.

    my $outcome = App::BatchRelease::Release::release( '/bogus/Foo-Bar', $credentials, lib => ['/bogus/Foo-Baz/.build/abc/lib'] );

=head2 test

C<($root, %options)>.  Runs the release tests of the distribution in the
checkout C<$root>, as C<dzil test --release --keep-build-dir> does, in a child
process, through L</in_child>.  That builds the distribution in a new
directory under F<.build/> of the checkout, and runs its tests there with
C<RELEASE_TESTING> and C<AUTHOR_TESTING>.  It keeps the build, also when the
tests pass, so that the caller can give its F<lib/> to the tests of a
distribution that requires it.  The caller removes the build.  It hands no
credentials, and uploads nothing.

Returns a HashRef, what L</in_child> returns.

    my $outcome = App::BatchRelease::Release::test('/bogus/Foo-Bar');
    say "the release tests failed: $outcome->{error}" if defined $outcome->{error};

=head2 in_child

C<($what, $root, $run, %options)>.  Runs the code C<$run>, which runs C<dzil>
in the checkout C<$root>, in a child process, through L</child>, and waits for
it.  C<$what> names the work in the messages, such as C<release>.  The output
goes to the standard output and the standard error of this program.

With C<< stdin => PATH >>, the child reads its standard input from C<PATH>,
for the questions that the plugins of the distribution ask, such as
C<[ConfirmRelease]>.  The caller passes F</dev/tty> when its own standard
input carried the credentials.

With C<< lib => \@dirs >>, the child puts C<@dirs> at the front of
C<PERL5LIB>, so that the tests, which Dist::Zilla runs in another process,
find modules that are not installed, such as those of a distribution of the
batch that was built a moment ago.

Returns a HashRef of C<error>, undef when C<$run> returned, else the reason
that the child failed, such as C<the release exited 1>, and C<built>, the
directory of the build that C<$run> returned, when it got that far.

    my $outcome = in_child( 'release', '/bogus/Foo-Bar', sub { dzil( '/bogus/Foo-Bar', $credentials, 'release' ) } );

The child exits through L</child>, with C<POSIX::_exit>, and never returns
from this call.  This process returns.

=head2 child

C<($what, $root, $run, $report, %options)>, as L</in_child> takes them, and
the handle C<$report> to the parent.  The body of the child process: runs
C<$run>, writes the directory of the build that it returns to C<$report>, and
ends the process, with status 0 when it succeeded and 1 when it died, after it
prints the error.

Returns nothing, because it exits with C<POSIX::_exit>, so that no C<END>
block and no destructor of an object of the parent, such as a temporary file,
runs in the child.

    child( $what, $root, $run, $write, %options ) if !$pid;

=head2 dzil

C<($root, $credentials, @command)>.  Changes to C<$root> and runs the
C<dzil> command C<@command> there, such as C<release>, in this process, as
C<dzil> does.  When C<$credentials> is defined, it first hands them to the
Dist::Zilla object through L</hand_credentials>.

Returns a string or undef: the absolute path of the directory of the build, or
undef when Dist::Zilla does not say.

    my $built = dzil( '/bogus/Foo-Bar', $credentials, 'release' );

It dies as the command dies.

=head2 hand_credentials

C<($zilla, $credentials)>.  Sets the C<username> and C<password> of every
plugin of the Dist::Zilla object C<$zilla> that is an
L<Dist::Zilla::Plugin::UploadToCPAN>, from C<$credentials>, an
L<App::BatchRelease::Credentials>.

Returns an integer, the count of such plugins.

    my $count = hand_credentials( $zilla, $credentials );

=head1 SEE ALSO

Please see those modules/websites for more information related to this module.

=over 4

=item *

L<App::BatchRelease|App::BatchRelease>

=back

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/Troglodyne-Internet-Widgets/App-BatchRelease/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHORS

Current Maintainers:

=over 4

=item *

George S. Baugh <george@troglodyne.net>

=back

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2026 Troglodyne LLC


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



( run in 0.793 second using v1.01-cache-2.11-cpan-f5f21f1076c )