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

use Moose;
use MooseX::StrictConstructor;
use Path::Tiny          qw< path >;
use Types::Path::Tiny   qw< Path  >;
use Log::Any            qw< $log >;

with qw<
    Pakket::Role::HasConfig
    Pakket::Role::HasSpecRepo
    Pakket::Role::HasSourceRepo
>;

has 'package' => (
    'is' => 'ro',
);

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

sub run {
    my $self = shift;

    if ( $self->spec_repo->has_object( $self->package->id ) ) {
        $log->debugf("Package %s already exists", $self->package->full_name);
        return;
    }

    $log->infof('Working on %s', $self->package->full_name);

    # Source
    $self->add_source();

    # Spec
    $self->add_spec();

    $log->infof('Done: %s', $self->package->full_name);
}

sub add_source {
    my $self = shift;

    if ($self->source_repo->has_object($self->package->id)) {
        $log->debugf("Package %s already exists in source repo (skipping)",
                        $self->package->full_name);
        return;
    }

    if (!$self->source_archive->exists) {
        Carp::croak("Archive with sources doesn't exist: ", $self->source_archive);
    }

    my $target = Path::Tiny->tempdir();
    my $dir    = $self->unpack($target, $self->source_archive);

    $log->debugf("Uploading %s into source repo from %s", $self->package->full_name, $dir);
    $self->source_repo->store_package_source($self->package, $dir);
}

sub add_spec {
    my $self = shift;

    $log->debugf("Creating spec for %s", $self->package->full_name);

    my $package = Pakket::Package->new(
            'category' => $self->package->category,
            'name'     => $self->package->name,
            'version'  => $self->package->version,
            'release'  => $self->package->release,
        );

    $self->spec_repo->store_package_spec($package);
}

sub unpack {
    my ($self, $target, $file) = @_;

    my $archive = Archive::Any->new($file);

    if ($archive->is_naughty) {
        Carp::croak($log->critical("Suspicious module ($file)"));
    }

    $archive->extract($target);

    my @files = $target->children();
    if (@files == 1 && $files[0]->is_dir) {
        return $files[0];
    }

    return $target;
}

__PACKAGE__->meta->make_immutable;
no Moose;
1;
__END__
<commit_msg>Use string type for source_archive in native scaffolder<commit_after>package Pakket::Scaffolder::Native;
# ABSTRACT: Scffolding Native distributions

use Moose;
use MooseX::StrictConstructor;
use Path::Tiny          qw< path >;
use Log::Any            qw< $log >;

with qw<
    Pakket::Role::HasConfig
    Pakket::Role::HasSpecRepo
    Pakket::Role::HasSourceRepo
>;

has 'package' => (
    'is' => 'ro',
);

has 'source_archive' => (
    'is'      => 'ro',
    'isa'     => 'Maybe[Str]',
);

sub run {
    my $self = shift;

    if ( $self->spec_repo->has_object( $self->package->id ) ) {
        $log->debugf("Package %s already exists", $self->package->full_name);
        return;
    }

    $log->infof('Working on %s', $self->package->full_name);

    # Source
    $self->add_source();

    # Spec
    $self->add_spec();

    $log->infof('Done: %s', $self->package->full_name);
}

sub add_source {
    my $self = shift;

    if ($self->source_repo->has_object($self->package->id)) {
        $log->debugf("Package %s already exists in source repo (skipping)",
                        $self->package->full_name);
        return;
    }

    if (!$self->source_archive) {
        Carp::croak("Please specify --source-archive=<sources_file_name>");
    }

    my $file = path($self->source_archive);
    if (!$file->exists) {
        Carp::croak("Archive with sources doesn't exist: ", $self->source_archive);
    }

    my $target = Path::Tiny->tempdir();
    my $dir    = $self->unpack($target, $file);

    $log->debugf("Uploading %s into source repo from %s", $self->package->full_name, $dir);
    $self->source_repo->store_package_source($self->package, $dir);
}

sub add_spec {
    my $self = shift;

    $log->debugf("Creating spec for %s", $self->package->full_name);

    my $package = Pakket::Package->new(
            'category' => $self->package->category,
            'name'     => $self->package->name,
            'version'  => $self->package->version,
            'release'  => $self->package->release,
        );

    $self->spec_repo->store_package_spec($package);
}

sub unpack {
    my ($self, $target, $file) = @_;

    my $archive = Archive::Any->new($file);

    if ($archive->is_naughty) {
        Carp::croak($log->critical("Suspicious module ($file)"));
    }

    $archive->extract($target);

    my @files = $target->children();
    if (@files == 1 && $files[0]->is_dir) {
        return $files[0];
    }

    return $target;
}

__PACKAGE__->meta->make_immutable;
no Moose;
1;
__END__

{'fraction_non_alphanumeric': 0.1853588907014682, 'fraction_numerical': 0.001835236541598695, 'mean_word_length': 2.82307092751364, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 142, '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': '4639238', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1659, 'n_words': 329}