Alien-Build-Plugin-Build-Xrepo

 view release on metacpan or  search on metacpan

lib/Alien/Build/Plugin/Build/Xrepo.pm  view on Meta::CPAN

                my $r   = $engine_for->();
                my $src = Cwd::getcwd();

                # Register any local repo trees (patched/private package recipes) with the engine
                # before installing, mirroring the engine's configure stage.  Remember only the
                # repos this stage actually added: they point into the build's temporary tree and
                # must be unregistered before that tree disappears.
                for my $repo_def ( @{ $recipe->local_repos || [] } ) {
                    my $dir = Path::Tiny->new($repo_def)->absolute;
                    next unless $dir->child('packages')->is_dir;
                    my $nm = 'alien-' . $dir->basename;
                    if ( !$r->can('add_repo') ) {
                        say "[xrepo] no engine support for add_repo (local repo $nm ignored)" if $verbose;
                        next;
                    }
                    my $added = eval { $r->add_repo( $nm, $dir->stringify ); 1 };
                    $build->runtime_prop->{repos}{$nm}++ if $added;
                }

                # Keep the repos around a little longer
                $meta->register_hook(
                    clean_install => sub ($build) {
                        return unless keys %{ $build->runtime_prop->{repos} };
                        say 'Removing temporary repos from xrepo...' if $verbose;
                        _remove_repos( $r, [ keys %{ $build->runtime_prop->{repos} } ] );
                    }
                );
                my %packages;
                my %errors;
                for my $name ( $recipe->packages ) {

                    # a per-package recipe version wins; otherwise the ambient plugin version (the
                    # alienfile `version` property) is the install-time default.
                    my $version = $recipe->version_for($name);
                    $version = $ambient{version} unless defined $version;
                    my %opts = $recipe->opts_for( $name, %ambient );
                    my $info;
                    eval { $info = $r->install( $name, $version, %opts ) };
                    if ($@) {
                        $errors{$name} = "$@";
                        next;
                    }
                    if ( ref $info && eval { $info->can('_data_printer') } ) {
                        $packages{$name} = $info->_data_printer(undef);
                    }
                    my $target = Path::Tiny->new($src)->child($name);
                    $target->mkpath;
                    try { $r->export( $name, $version, %opts, packagedir => $target->stringify ) }
                    catch ($e) {
                        $target->remove_tree if $target->exists;
                        delete $packages{$name};
                        $errors{$name} = 'export failed: ' . ( $e || 'unknown error' );
                    }
                }

                # A failed sibling does not sink the surviving packages (partial installs are by
                # design), but it must never be silent: prune export-failures from the success set,
                # flag every failure in the returned install/runtime prop so consumers can see what
                # is missing, and warn loudly.
                my $spared = Path::Tiny->new($src)->child('xrepo.manifest');
                $spared->spew_utf8( join "\n", grep { exists $packages{$_} } $recipe->packages );
                if (%errors) {
                    $build->runtime_prop->{errors} = { map { $_ => $errors{$_} } sort keys %errors };
                    my $failed = join ', ', sort keys %errors;
                    warn "Alien::Build::Plugin::Build::Xrepo: partial failure - these packages failed to install or export: $failed\n";
                    my ( $ok, $bad ) = ( scalar( keys %packages ), scalar( keys %errors ) );
                    warn "[xrepo] $ok packages installed, $bad failed (see install_prop->{xrepo}{errors} / runtime_prop->{errors} for details)\n"
                        if $verbose;
                }
                my $xrepo = $build->install_prop->{xrepo} ||= {};
                $xrepo->{packages}     = \%packages;
                $xrepo->{errors}       = \%errors;
                $xrepo->{download_dir} = $src;

                # The produced content is local (xrepo store exports, not a network fetch), so
                # declare it to Alien::Build as a 'file' protocol download. Without
                # download_details the extract step warns about a missing digest and a future
                # Alien::Build will die by default.
                my $dl = Path::Tiny->new('.')->absolute->stringify;
                $build->install_prop->{download_detail}{$dl} = { protocol => 'file' };

                # Unregister the temporary local repos as soon as the installs/exports are done.
                # Their paths live under the build's temp tree; leaving them registered makes
                # later xrepo commands fail because the repositories no longer exist.
                _remove_repos( $r, [ keys %{ $build->runtime_prop->{repos} } ] );
                die 'Alien::Build::Plugin::Build::Xrepo: no packages installed successfully' unless %packages;
            }
        );
        $meta->default_hook(
            extract => sub ( $build, $dest ) {
                $self->__build($build);
                my $xrepo = $build->install_prop->{xrepo};
                die 'Alien::Build::Plugin::Build::Xrepo: the plugin download stage must run before extract'
                    unless $xrepo && defined $xrepo->{download_dir};
                my $src = Path::Tiny->new( $xrepo->{download_dir} );

                # The extract-hook contract makes the current working directory the staging
                # scaffold: Alien::Build chdirs every extract into a fresh, empty TempDir and
                # expects the content there (Alien::Build::Manual::PluginAuthor, "extract hook").
                # $dest is the *source* for a file protocol for this plugin it is the same download
                # temp dir $xrepo->{download_dir} points at, so copying into it would be a
                # self-copy. Copy into the cwd scaffold, not $dest.
                my $dst = Path::Tiny->new( Cwd::getcwd() );
                for my $child ( $src->children ) {
                    next if $child->basename eq '.';
                    my $out = $dst->child( $child->basename );
                    $child->is_dir ? _copy_tree( $child, $out ) : $child->copy($out);
                }
                1;
            }
        );
        $meta->default_hook(
            build => sub ($build) {
                $self->__build($build);
                my $xrepo = $build->install_prop->{xrepo};
                die 'Alien::Build::Plugin::Build::Xrepo: the plugin download stage must run before build'
                    unless $xrepo && defined $xrepo->{download_dir};
                my $src    = Path::Tiny->new( $xrepo->{download_dir} );
                my $prefix = Path::Tiny->new( $build->install_prop->{prefix} ) || die 'Alien::Build::Plugin::Build::Xrepo: prefix is not set';
                $prefix->mkpath;
                my %exported;



( run in 0.558 second using v1.01-cache-2.11-cpan-234fa220139 )