Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created November 15, 2013 23:56
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 lizmat/7493850 to your computer and use it in GitHub Desktop.
Save lizmat/7493850 to your computer and use it in GitHub Desktop.
some preliminary work on S11
#-------------------------------------------------------------------------------
# LFAV - Support module loading with long name/from/auth/version information
#
# Although the order for specification is usually longname, from, auth and
# version (hence the name of the module LFAV), it was deemed more useful to
# make "from" the top level hash, to easier allow for shortcuts for different
# module sources.
constant Unk := Any;
constant MAGIC_KEY := "LFAV";
constant DATA_VERSION := 1;
class LFAV;
has %.data;
# |- from
# |- longname
# |- auth
# |- ver
# |- info (e.g. stash or filename)
has @.extensions= <nqp pbc pir pm pm6>;
#-------------------------------------------------------------------------------
# set - set info for given module / class / etc.
method set (
$longname, :$from= "perl6", :$auth= Unk, :$ver= Unk
) is rw {
# set up structure if necessary
my $longnames= %.data{$from} //= do { my %h of Hash };
my $auths= $longnames{$longname} //= do { my %h{Unk} of Hash };
my $vers= $auths{$auth} //= do { my %h{Unk} };
# allow to be used as lvalue
return Proxy.new:
FETCH => method {
die "Cannot FETCH with 'set', please use 'candidates'";
False;
},
STORE => method ( $value ) { $vers{$ver}= $value; True };
} #set
#-------------------------------------------------------------------------------
# candidates - find candidates for given module name
method candidates (
$longname?, :$from= "perl6", :$auth?, :$ver?
) {
say $auth.WHAT;
#---------------------------------------------------------------------------
# f - filter with given value the given candidates
my sub f {
my $what= shift(@_);
@_ ==> map {
my $hash := pop $_;
my @found := @($_);
map { [ @found, @($_.kv) ] }, # @($_) doesn't work, why?
$hash{ grep $what, $hash.keys }:p;
}
} #f
[%!data.item] ==> f($from) ==> f($longname) ==> f($auth) ==> f($ver);
# |- from, longname, auth, ver, value
} #candidates
#-------------------------------------------------------------------------------
# serialize - serialize for later deserialization
method serialize () {
join( "\n",
MAGIC_KEY,
DATA_VERSION,
self.candidates( from => Any ).map( { .perl } )
);
} #serialize
#-------------------------------------------------------------------------------
# deserialize - create object from serialized data
method deserialize ($serialized) {
my @lines= $serialized.lines;
my $type= @lines.shift;
die "Improperly serialized data: $type" if $type ne MAGIC_KEY;
my $version= @lines.shift;
die "Improperly version: $version" if $version != DATA_VERSION;
my $new= self.new;
for @lines {
my $line= $_.eval;
$new.set(
$line[1], from => $line[0], auth => $line[2], ver => $line[3]
)= $line[4];
}
$new;
} #deserialize
#-------------------------------------------------------------------------------
# spurt - save as a file
method spurt ($filename) { spurt( $filename, self.serialize ) } #spurt
#-------------------------------------------------------------------------------
# slurp - create object from file
method slurp ($filename) {
my $handle= open( $filename, :r ) orelse die $!;
self.deserialize($handle);
} #slurp
#-------------------------------------------------------------------------------
# update-config - update the config of a directory
method update-config ($directory) {
for dir($directory).grep( !m/^\./ ) {
.say
}
} #update-config
#-------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment