<commit_before>package Pakket::Installer;
# ABSTRACT: Install pakket packages into an installation directory

use Moose;
use MooseX::StrictConstructor;
use Path::Tiny            qw< path  >;
use Types::Path::Tiny     qw< Path  >;
use File::Copy::Recursive qw< dircopy >;
use File::Basename        qw< basename >;
use Time::HiRes           qw< time >;
use Log::Any              qw< $log >;
use JSON::MaybeXS         qw< decode_json >;
use Pakket::Log;
use Pakket::Utils         qw< is_writeable >;
use Pakket::Constants qw<
    PARCEL_METADATA_FILE
    PARCEL_FILES_DIR
    PAKKET_PACKAGE_SPEC
>;

with 'Pakket::Role::RunCommand';

# Sample structure:
# ~/.pakket/
#        bin/
#        etc/
#        repos/
#        libraries/
#                  active ->
#

has 'pakket_dir' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'parcel_dir' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'keep_copies' => (
    'is'      => 'ro',
    'isa'     => 'Int',
    'default' => sub {1},
);

has 'index_file' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'index' => (
    'is'       => 'ro',
    'isa'      => 'HashRef',
    'lazy'     => 1,
    'builder'  => '_build_index',
    'required' => 1,
);

has 'input_file' => (
    'is'        => 'ro',
    'isa'       => Path,
    'coerce'    => 1,
    'predicate' => '_has_input_file',
);

sub _build_index {
    my $self = shift;
    return decode_json $self->index_file->slurp_utf8;
}

# TODO:
# this should be implemented using a fetcher class
# because it might be from HTTP/FTP/Git/Custom/etc.
sub fetch_package;

sub install {
    my ( $self, @packages ) = @_;

    if ( $self->_has_input_file ) {
        my $content = decode_json $self->input_file->slurp_utf8;
        foreach my $category ( keys %{$content} ) {
            push @packages, Pakket::Package->new(
                'name'     => $_,
                'category' => $category,
                'version'  => $content->{$category}{$_}{'latest'},
            ) for keys %{ $content->{$category} };
        }
    } else {
        my @clean_packages;
        foreach my $package_str (@packages) {
            my ( $pkg_cat, $pkg_name, $pkg_version ) =
                $package_str =~ PAKKET_PACKAGE_SPEC();

            if ( !defined $pkg_version ) {
                $log->critical(
                    'Currently you must provide a version to install'
                );

                exit 1;
            }

            push @clean_packages, Pakket::Package->new(
                'category' => $pkg_cat,
                'name'     => $pkg_name,
                'version'  => $pkg_version,
            );
        }

        @packages = @clean_packages;
    }

    if ( !@packages ) {
        $log->notice('Did not receive any parcels to deliver');
        return;
    }

    my $pakket_dir = $self->pakket_dir;

    $pakket_dir->is_dir
        or $pakket_dir->mkpath();

    my $work_dir = $pakket_dir->child( time() );

    if ( $work_dir->exists ) {
        $log->error(
            "Internal installation directory exists ($work_dir), exiting",
        );

        exit 1;<commit_msg>report as critical before exiting<commit_after>package Pakket::Installer;
# ABSTRACT: Install pakket packages into an installation directory

use Moose;
use MooseX::StrictConstructor;
use Path::Tiny            qw< path  >;
use Types::Path::Tiny     qw< Path  >;
use File::Copy::Recursive qw< dircopy >;
use File::Basename        qw< basename >;
use Time::HiRes           qw< time >;
use Log::Any              qw< $log >;
use JSON::MaybeXS         qw< decode_json >;
use Pakket::Log;
use Pakket::Utils         qw< is_writeable >;
use Pakket::Constants qw<
    PARCEL_METADATA_FILE
    PARCEL_FILES_DIR
    PAKKET_PACKAGE_SPEC
>;

with 'Pakket::Role::RunCommand';

# Sample structure:
# ~/.pakket/
#        bin/
#        etc/
#        repos/
#        libraries/
#                  active ->
#

has 'pakket_dir' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'parcel_dir' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'keep_copies' => (
    'is'      => 'ro',
    'isa'     => 'Int',
    'default' => sub {1},
);

has 'index_file' => (
    'is'       => 'ro',
    'isa'      => Path,
    'coerce'   => 1,
    'required' => 1,
);

has 'index' => (
    'is'       => 'ro',
    'isa'      => 'HashRef',
    'lazy'     => 1,
    'builder'  => '_build_index',
    'required' => 1,
);

has 'input_file' => (
    'is'        => 'ro',
    'isa'       => Path,
    'coerce'    => 1,
    'predicate' => '_has_input_file',
);

sub _build_index {
    my $self = shift;
    return decode_json $self->index_file->slurp_utf8;
}

# TODO:
# this should be implemented using a fetcher class
# because it might be from HTTP/FTP/Git/Custom/etc.
sub fetch_package;

sub install {
    my ( $self, @packages ) = @_;

    if ( $self->_has_input_file ) {
        my $content = decode_json $self->input_file->slurp_utf8;
        foreach my $category ( keys %{$content} ) {
            push @packages, Pakket::Package->new(
                'name'     => $_,
                'category' => $category,
                'version'  => $content->{$category}{$_}{'latest'},
            ) for keys %{ $content->{$category} };
        }
    } else {
        my @clean_packages;
        foreach my $package_str (@packages) {
            my ( $pkg_cat, $pkg_name, $pkg_version ) =
                $package_str =~ PAKKET_PACKAGE_SPEC();

            if ( !defined $pkg_version ) {
                $log->critical(
                    'Currently you must provide a version to install'
                );

                exit 1;
            }

            push @clean_packages, Pakket::Package->new(
                'category' => $pkg_cat,
                'name'     => $pkg_name,
                'version'  => $pkg_version,
            );
        }

        @packages = @clean_packages;
    }

    if ( !@packages ) {
        $log->notice('Did not receive any parcels to deliver');
        return;
    }

    my $pakket_dir = $self->pakket_dir;

    $pakket_dir->is_dir
        or $pakket_dir->mkpath();

    my $work_dir = $pakket_dir->child( time() );

    if ( $work_dir->exists ) {
        $log->critical(
            "Internal installation directory exists ($work_dir), exiting",
        );

        exit 1;
{'fraction_non_alphanumeric': 0.1550695825049702, 'fraction_numerical': 0.0042820003058571645, 'mean_word_length': 1.8397742075553625, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1967292', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2199, 'n_words': 495}