Skip to content

Instantly share code, notes, and snippets.

@ugexe

ugexe/CURI.pm6 Secret

Created July 18, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/dc3963f4ed7ac8e0c0782fd2af9a2067 to your computer and use it in GitHub Desktop.
Save ugexe/dc3963f4ed7ac8e0c0782fd2af9a2067 to your computer and use it in GitHub Desktop.
use nqp;
# perl6 -Ilib -e ' \
# use CURI; \
# my $spec = CompUnit::DependencySpecification.new(:short-name("zef")); \
# my $dist = CompUnit::Repository::Installation::Distribution.new(:$spec); \
# say $dist.content("lib/Zef.pm6").open.slurp-rest'
# Just some way for a CURI to set the prefix of various items in a Distribution. Maybe share this with CURI
role CUR::Prefixes {
method prefix { "/home/nickl/.rakudobrew/moar-2016.07/install/share/perl6/site/".IO }
method dist-dir { $.prefix.child('dist') }
method resources-dir { $.prefix.child('resources') }
method sources-dir { $.prefix.child('sources') }
}
# The $version bit is just toying with the idea of handling repo-version translation in Distribution; ignore
role CompUnit::Repository::Installation::Distribution[$version where *.Int == 2 = 2] does CUR::Prefixes {
also does Distribution;
has $!meta;
has $!spec;
# CU::DependencySpecification would be nicer if it .Str gave the same id
submethod BUILD(CompUnit::DependencySpecification :$!spec!) {
#my $id = nqp::sha1($!spec.str); # couldn't get $!spec id to match installed id, but you get the idea
my $id = '9FC151204D7B2313A9A9F7E39603BED7DC60259A'; # Known installation on my machine; see above comment
my $path = self.dist-dir.child($id);
$!meta = Rakudo::Internals::JSON.from-json($path.slurp)
}
method meta { $!meta }
method content($name-path) {
my $path = do given $name-path {
when /^resources\// { self.resources-dir ~ '/' ~ self.meta<files>{parse-value($_)} }
when /^bin\// { self.resources-dir ~ '/' ~ self.meta<files>{parse-value($_)} }
when /^lib\// {
my $lib-meta = self.meta<provides>.values.first({ $name-path eq parse-value($_) });
self.sources-dir ~ '/' ~ $lib-meta{$_}<file>;
}
}
my $handle = IO::Handle.new: path => $path.IO;
$handle // $handle.throw;
}
# For meta<files> and meta<provides>
# We want a key/value, where the key is what can be passed to .content and the value is the mangled name path
# A name-path is just a relative path part that gets rooted against some authority/prefix
# Meant to be extended such that if the .value is a Pair we know the .key of the pair is the value we want
# "bin/zef" [Typical pre-installation format. Translate to "bin/zef" => "bin/zef"]
# "bin/zef" => "10923801928309d90182d" [Typical installation format]
# "bin/zef" => { "10923801928309d90182d" => ... } [Extended value, such as additional metadata (modified times, etc)
# without denormalization. Take the .value.key as the .value and'
# ignore the .value.value.] (Not actually used yet, but the same
# thing is used for meta<provides> and IS used for storing the
# mangled name so it can be uninstalled)
# Or for meta<provides>:
# "Zef" => "lib/Zef.pm6" [Typical pre-installation]
# "Zef" => { "lib/Zef.pm6" => { "file" => "f32f9023jf0923f", modified => "..."}} [Typical post installation]
my sub parse-value($str-or-kv) {
do given $str-or-kv {
when Str { $_ }
when Hash { $_.keys[0] }
when Pair { $_.key }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment