Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline CompUnit::Repository::Distribution #4872

Merged
merged 2 commits into from
Apr 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 45 additions & 39 deletions src/core.c/CompUnit/Repository/Distribution.pm6
@@ -1,61 +1,67 @@
# A distribution passed to `CURI.install()` will get encapsulated in this
# class, which normalizes the meta6 data and adds identifiers/content-id
class CompUnit::Repository::Distribution does Distribution {
has Distribution $!dist handles <content prefix>;
has $!meta;
has $.repo;
has $.dist-id;
has $.repo-name;

submethod TWEAK(|) {
my $meta = $!dist.meta.hash;
$meta<ver> //= $meta<version>;
$meta<auth> //= $meta<authority> // $meta<author>;
$!meta = $meta;

$!repo-name //= $!repo.name if nqp::can($!repo,"name") && $!repo.name;
$!repo = $!repo.path-spec if $!repo.defined && $!repo !~~ Str;
}
has Distribution $.dist is built(:bind) handles <content prefix>;
has $.repo is built(:bind);
has $.dist-id is built(:bind);
has $.repo-name is built(:bind);
has %.meta is built(False);

submethod BUILD(:$!dist, :$!repo, :$!dist-id, :$!repo-name --> Nil) { }
method TWEAK(--> Nil) {
my %meta := $!dist.meta.hash;
%meta<ver> //= %meta<version> // '';
%meta<auth> //= %meta<authority> // %meta<author> // '';
%meta<api> //= '';
%!meta := %meta;

method new(Distribution $dist, *%_) {
self.bless(:$dist, |%_)
$!repo-name := $!repo.name
if nqp::not_i(nqp::isconcrete($!repo-name))
&& nqp::can($!repo,"name");
$!repo := $!repo.path-spec
if nqp::isconcrete($!repo)
&& nqp::not_i(nqp::istype($!repo,Str));
}

method meta { $!meta }

method Str() {
return "{$.meta<name>}"
~ ":ver<{$.meta<ver> // ''}>"
~ ":auth<{$.meta<auth> // ''}>"
~ ":api<{$.meta<api> // ''}>";
method new(Distribution:D $dist
--> CompUnit::Repository::Distribution:D) {
self.bless(:$dist, |%_)
}

method id() {
return nqp::sha1(self.Str);
}
method id(--> Str:D) { nqp::sha1(self.Str) }

# Alternate instantiator called from Actions.nqp during compilation
# of $?DISTRIBUTION
method from-precomp(CompUnit::Repository::Distribution:U:
--> CompUnit::Repository::Distribution:D) is implementation-detail {
if %*ENV<RAKUDO_PRECOMP_DIST> -> $json {
my %data := Rakudo::Internals::JSON.from-json: $json;
my $name := %data<repo-name>;
my $spec := %data<repo>; # XXX badly named field?
my $id := %data<dist-id>;

method from-precomp(CompUnit::Repository::Distribution:U: --> CompUnit::Repository::Distribution) {
if %*ENV<RAKUDO_PRECOMP_DIST> -> \dist {
my %data := Rakudo::Internals::JSON.from-json: dist;
my $repo := %data<repo-name>
?? CompUnit::RepositoryRegistry.repository-for-name(%data<repo-name>)
!! CompUnit::RepositoryRegistry.repository-for-spec(%data<repo>);
my $dist := $repo.distribution(%data<dist-id>);
self.new($dist, :repo(%data<repo>), :repo-name(%data<repo-name>), :dist-id(%data<dist-id>));
my $repo := $name
?? CompUnit::RepositoryRegistry.repository-for-name($name)
!! CompUnit::RepositoryRegistry.repository-for-spec($spec);
self.bless:
:dist($repo.distribution($id)),
:repo($spec),
:repo-name($name),
:dist-id($id);
}
else {
Nil
}
}

method serialize() {
method serialize(--> Str:D) {
lizmat marked this conversation as resolved.
Show resolved Hide resolved
Rakudo::Internals::JSON.to-json: {:$.repo, :$.repo-name, :$.dist-id}
}

method raku {
self.^name ~ ".new({$!dist.raku}, repo => {$!repo.raku}, repo-name => {$!repo-name.raku})";
multi method Str(CompUnit::Repository::Distribution:D:--> Str:D) {
"%!meta<name>:ver<%!meta<ver>>:auth<%!meta<auth>>:api<%!meta<api>>"
}
multi method raku(CompUnit::Repository::Distribution:D:--> Str:D) {
self.^name ~ ".new($!dist.raku(), repo => $!repo.raku(), repo-name => $!repo-name.raku())"
}
}

Expand Down