<commit_before>package Pakket::Scaffolder::Perl;
# ABSTRACT: Scffolding Perl distributions

use Moose;
use MooseX::StrictConstructor;
use version 0.77;
use Carp ();
use Archive::Any;
use CPAN::Meta;
use CPAN::Meta::Prereqs;
use Parse::CPAN::Packages::Fast;
use JSON::MaybeXS       qw< decode_json encode_json >;
use Ref::Util           qw< is_arrayref is_hashref >;
use Path::Tiny          qw< path >;
use Types::Path::Tiny   qw< Path  >;
use Log::Any            qw< $log >;

use Pakket::Package;
use Pakket::Types;
use Pakket::Utils::Perl qw< should_skip_module >;
use Pakket::Constants   qw< PAKKET_PACKAGE_SPEC >;
use Pakket::Scaffolder::Perl::Module;
use Pakket::Scaffolder::Perl::CPANfile;

with qw<
    Pakket::Role::HasConfig
    Pakket::Role::HasSpecRepo
    Pakket::Role::HasSourceRepo
    Pakket::Role::Perl::BootstrapModules
    Pakket::Scaffolder::Perl::Role::Borked
    Pakket::Scaffolder::Role::Backend
    Pakket::Scaffolder::Role::Terminal
>;

use constant {
    'ARCHIVE_DIR_TEMPLATE' => 'ARCHIVE-XXXXXX',
};

has 'metacpan_api' => (
    'is'      => 'ro',
    'isa'     => 'Str',
    'lazy'    => 1,
    'builder' => '_build_metacpan_api',
);

has 'phases' => (
    'is'       => 'ro',
    'isa'      => 'ArrayRef[PakketPhase]',
    'required' => 1,
);

has 'processed_dists' => (
    'is'      => 'ro',
    'isa'     => 'HashRef',
    'default' => sub { return +{} },
);

has 'modules' => (
    'is'  => 'ro',
    'isa' => 'HashRef',
);

has 'spec_index' => (
    'is'      => 'ro',
    'isa'     => 'HashRef',
    'lazy'    => 1,
    'builder' => '_build_spec_index',
);

has 'prereqs' => (
    'is'      => 'ro',
    'isa'     => 'CPAN::Meta::Prereqs',
    'lazy'    => 1,
    'builder' => '_build_prereqs',
);

has 'download_dir' => (
    'is'      => 'ro',
    'isa'     => Path,
    'lazy'    => 1,
    'builder' => '_build_download_dir',
);

has 'cache_dir' => (
    'is'        => 'ro',
    'isa'       => Path,
    'coerce'    => 1,
    'predicate' => '_has_cache_dir',
);

has 'file_02packages' => (
    'is'      => 'ro',
    'isa'     => 'Str',
);

has 'cpan_02packages' => (
    'is'      => 'ro',
    'isa'     => 'Parse::CPAN::Packages::Fast',
    'lazy'    => 1,
    'builder' => '_build_cpan_02packages',
);

has 'versioner' => (
    'is'      => 'ro',
    'isa'     => 'Pakket::Versioning',
    'lazy'    => 1,
    'builder' => '_build_versioner',
);

has 'no_deps' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'no_bootstrap' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'is_local' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'types' => (
    'is'      => 'ro',
    'isa'     => 'ArrayRef',
    'default' => sub { [qw< requires recommends suggests >] },
);

sub _build_metacpan_api {
    my $self = shift;
    return $ENV{'PAKKET_METACPAN_API'}
        || $self->config->{'perl'}{'metacpan_api'}
        || 'https://fastapi.metacpan.org';
}

sub _build_prereqs {
    my $self = shift;
    return CPAN::Meta::Prereqs->new( $self->modules );
}

sub _build_download_dir {
    my $self = shift;
    return Path::Tiny->tempdir( 'CLEANUP' => 1 );
}

sub _build_spec_index {
    my $self = shift;
    my $spec_index = $self->spec_repo->all_object_ids;
    my %spec_index;
    for my $s ( @{ $spec_index } ) {
        $s =~ PAKKET_PACKAGE_SPEC();
        $spec_index{$2}{$3} = 1;
    }
    return \%spec_index;
}

sub _build_cpan_02packages {
    my $self = shift;
    my ( $dir, $file );

    if ( $self->file_02packages ) {
        $file = path( $self->file_02packages );
        $log->infof( "Using 02packages file: %s", $self->file_02packages );<commit_msg>Revert "scaffolder: spec_index: use PAKKET_PACKAGE_SPEC"<commit_after>package Pakket::Scaffolder::Perl;
# ABSTRACT: Scffolding Perl distributions

use Moose;
use MooseX::StrictConstructor;
use version 0.77;
use Carp ();
use Archive::Any;
use CPAN::Meta;
use CPAN::Meta::Prereqs;
use Parse::CPAN::Packages::Fast;
use JSON::MaybeXS       qw< decode_json encode_json >;
use Ref::Util           qw< is_arrayref is_hashref >;
use Path::Tiny          qw< path >;
use Types::Path::Tiny   qw< Path  >;
use Log::Any            qw< $log >;

use Pakket::Package;
use Pakket::Types;
use Pakket::Utils::Perl qw< should_skip_module >;
use Pakket::Constants   qw< PAKKET_PACKAGE_SPEC >;
use Pakket::Scaffolder::Perl::Module;
use Pakket::Scaffolder::Perl::CPANfile;

with qw<
    Pakket::Role::HasConfig
    Pakket::Role::HasSpecRepo
    Pakket::Role::HasSourceRepo
    Pakket::Role::Perl::BootstrapModules
    Pakket::Scaffolder::Perl::Role::Borked
    Pakket::Scaffolder::Role::Backend
    Pakket::Scaffolder::Role::Terminal
>;

use constant {
    'ARCHIVE_DIR_TEMPLATE' => 'ARCHIVE-XXXXXX',
};

has 'metacpan_api' => (
    'is'      => 'ro',
    'isa'     => 'Str',
    'lazy'    => 1,
    'builder' => '_build_metacpan_api',
);

has 'phases' => (
    'is'       => 'ro',
    'isa'      => 'ArrayRef[PakketPhase]',
    'required' => 1,
);

has 'processed_dists' => (
    'is'      => 'ro',
    'isa'     => 'HashRef',
    'default' => sub { return +{} },
);

has 'modules' => (
    'is'  => 'ro',
    'isa' => 'HashRef',
);

has 'spec_index' => (
    'is'      => 'ro',
    'isa'     => 'HashRef',
    'lazy'    => 1,
    'builder' => '_build_spec_index',
);

has 'prereqs' => (
    'is'      => 'ro',
    'isa'     => 'CPAN::Meta::Prereqs',
    'lazy'    => 1,
    'builder' => '_build_prereqs',
);

has 'download_dir' => (
    'is'      => 'ro',
    'isa'     => Path,
    'lazy'    => 1,
    'builder' => '_build_download_dir',
);

has 'cache_dir' => (
    'is'        => 'ro',
    'isa'       => Path,
    'coerce'    => 1,
    'predicate' => '_has_cache_dir',
);

has 'file_02packages' => (
    'is'      => 'ro',
    'isa'     => 'Str',
);

has 'cpan_02packages' => (
    'is'      => 'ro',
    'isa'     => 'Parse::CPAN::Packages::Fast',
    'lazy'    => 1,
    'builder' => '_build_cpan_02packages',
);

has 'versioner' => (
    'is'      => 'ro',
    'isa'     => 'Pakket::Versioning',
    'lazy'    => 1,
    'builder' => '_build_versioner',
);

has 'no_deps' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'no_bootstrap' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'is_local' => (
    'is'      => 'ro',
    'isa'     => 'Bool',
    'default' => 0,
);

has 'types' => (
    'is'      => 'ro',
    'isa'     => 'ArrayRef',
    'default' => sub { [qw< requires recommends suggests >] },
);

sub _build_metacpan_api {
    my $self = shift;
    return $ENV{'PAKKET_METACPAN_API'}
        || $self->config->{'perl'}{'metacpan_api'}
        || 'https://fastapi.metacpan.org';
}

sub _build_prereqs {
    my $self = shift;
    return CPAN::Meta::Prereqs->new( $self->modules );
}

sub _build_download_dir {
    my $self = shift;
    return Path::Tiny->tempdir( 'CLEANUP' => 1 );
}

sub _build_spec_index {
    my $self = shift;
    my $spec_index = $self->spec_repo->all_object_ids;
    my %spec_index;
    for ( @{ $spec_index } ) {
        m{^.*?/(.*)=(.*?)$};
        $spec_index{$1}{$2} = 1;
    }
    return \%spec_index;
}

sub _build_cpan_02packages {
    my $self = shift;
    my ( $dir, $file );

    if ( $self->file_02packages ) {
        $file = path( $self->file_02packages );
        $log->infof( "Using 02packages file: %s", $self->file_02packages );
{'fraction_non_alphanumeric': 0.20336700336700336, 'fraction_numerical': 0.009158249158249159, 'mean_word_length': 2.7000498256103636, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 177, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5298485', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2929, 'n_words': 549}