Skip to content

Commit

Permalink
Fix .files for when lookup can't find source
Browse files Browse the repository at this point in the history
Sometimes the lookup file may not contain the source id. In these
cases we need to fall back to the slower json-parsing code path.
This also revealed a bug where old meta data was being returned
for a LazyDistribution after the first json parsing.
  • Loading branch information
ugexe committed Aug 5, 2017
1 parent bed3bbd commit a25d5fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
22 changes: 17 additions & 5 deletions src/core/CompUnit/Repository/Installation.pm
Expand Up @@ -350,8 +350,11 @@ sub MAIN(:$name, :$auth, :$ver, *@, *%) {
unlink( $dist-dir.add($dist.id) )
}

# Ideally this would return Distributions, but it'd break older bin/ scripts
proto method files(|) {*}
multi method files($file, Str:D :$name!, :$auth, :$ver, :$api) {
# if we have to include :$name then we take the slow path

my $spec = CompUnit::DependencySpecification.new(
short-name => $name,
auth-matcher => $auth // True,
Expand All @@ -362,10 +365,13 @@ sub MAIN(:$name, :$auth, :$ver, *@, *%) {
with self.candidates($spec) {
my $matches := $_.grep: { .meta<files>{$file}:exists }

return $matches.map: {
.meta<source> = self!resources-dir.add(.meta<source>);
.meta;
my $absolutified-metas := $matches.map: {
my %meta = $_.meta;
%meta<source> = self!resources-dir.add(%meta<files>{$file});
%meta;
}

return $absolutified-metas.grep(*.<source>.e);
}
}
multi method files($file, :$auth, :$ver, :$api) {
Expand All @@ -378,8 +384,14 @@ sub MAIN(:$name, :$auth, :$ver, *@, *%) {
api-matcher => $api // True,
);

return self.candidates($spec).grep: {
.meta<source> = self!resources-dir.add(.meta<source>)
with self.candidates($spec) {
my $absolutified-metas := $_.map: {
my %meta = $_.meta;
%meta<source> = self!resources-dir.add(%meta<source> || %meta<files>{$file});
%meta;
}

return $absolutified-metas.grep(*.<source>.e);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/core/CompUnit/RepositoryRegistry.pm
Expand Up @@ -217,22 +217,22 @@ class CompUnit::RepositoryRegistry {
shift @*ARGS if $ver;

my @installations = $*REPO.repo-chain.grep(CompUnit::Repository::Installation);
my @candidates = @installations.map({ .files("bin/$script", :$name, :$auth, :$ver).head }).grep(*.defined);
unless +@candidates {
@candidates = flat @installations.map({ .files("bin/$script").Slip }).grep(*.defined);
if +@candidates {
my @metas = @installations.map({ .files("bin/$script", :$name, :$auth, :$ver).head }).grep(*.defined);
unless +@metas {
@metas = flat @installations.map({ .files("bin/$script").Slip }).grep(*.defined);
if +@metas {
note "===SORRY!===\n"
~ "No candidate found for '$script' that match your criteria.\n"
~ "Did you perhaps mean one of these?";
my %caps = :name(['Distribution', 12]), :auth(['Author(ity)', 11]), :ver(['Version', 7]);
for @candidates -> $dist {
for @metas -> $meta {
for %caps.kv -> $caption, @opts {
@opts[1] = max @opts[1], ($dist.meta{$caption} // '').Str.chars
@opts[1] = max @opts[1], ($meta{$caption} // '').Str.chars
}
}
note ' ' ~ %caps.values.map({ sprintf('%-*s', .[1], .[0]) }).join(' | ');
for @candidates -> $dist {
note ' ' ~ %caps.kv.map( -> $k, $v { sprintf('%-*s', $v.[1], $dist.meta{$k} // '') } ).join(' | ')
for @metas -> $meta {
note ' ' ~ %caps.kv.map( -> $k, $v { sprintf('%-*s', $v.[1], $meta{$k} // '') } ).join(' | ')
}
}
else {
Expand All @@ -241,8 +241,8 @@ class CompUnit::RepositoryRegistry {
exit 1;
}

my $candi = @candidates.sort(*.meta<ver>).reverse.head;
my $bin = $candi.meta<source>;
my $meta = @metas.sort(*.<ver>).reverse.head;
my $bin = $meta<source>;
require "$bin";
}

Expand Down

0 comments on commit a25d5fa

Please sign in to comment.