Last active
July 22, 2016 15:53
-
-
Save ugexe/ddc78c5b39a1173b72264368ead88c11 to your computer and use it in GitHub Desktop.
perl6 meta6 path table lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# split %files into subsections %lookup and %links | |
# * - %lookup maps "all the original real file paths that were installed" to "actual paths the repo installed to" | |
# * - %links allows pointing a symlink at a file in %lookup | |
# ? - if a %link key matches a %lookup key, then what? | |
my $json = q:to/ENDJSON/; | |
{ | |
"name" : "Module::XXX", | |
"version" : "1", | |
"auth" : "cpan:xxx", | |
"api" : "*", | |
"files" : { | |
"lookup" : { | |
"lib/Module/Foo.pm6" : "{module::xxx::sha1}/abc123", | |
"lib/Module/Foo/Bar.pm6" : "{module::xxx::sha1}/def123", | |
"bin/myapp.pl6" : "{module::xxx::sha1}/ghi123", | |
"resources/libraries/libp5helper.so" : "{module::xxx::sha1}/jkl123", | |
"resources/config.json" : "{module::xxx::sha1}/mno123" | |
}, | |
"links" : { | |
"resources/libraries/p5helper" : "resources/libraries/libp5helper.so" | |
} | |
}, | |
"provides" : { | |
"Module::Foo" : "lib/Module/Foo.pm6", | |
"Module::Foo::Bar" : "lib/Module/Foo/Bar.pm6", | |
"Module::Foo::Bar::Helper" : "lib/Module/Foo/Bar.pm6" | |
} | |
} | |
ENDJSON | |
package Meta::FileLookup { | |
our &link2orig is export(:ALL) = -> %_, $_ { %_<files><links>{$_} } | |
our &link2repo is export(:ALL) = -> %_, $_ { %_<files><lookup>{link2orig(%_, $_)} } | |
our &orig2link is export(:ALL) = -> %_, $_ { first *.defined, %_<files><links>.antipairs.map(*.{$_}) } | |
our &orig2repo is export(:ALL) = -> %_, $_ { %_<files><lookup>{$_} } | |
our &orig2names is export(:ALL) = -> %_, $_ { grep *.defined, %_<provides>.antipairs.map(*.{$_}) } | |
our &repo2orig is export(:ALL) = -> %_, $_ { first *.defined, %_<files><lookup>.antipairs.map(*.{$_}) } | |
our &repo2link is export(:ALL) = -> %_, $_ { orig2link(%_, repo2orig(%_, $_)) } | |
our &repo2names is export(:ALL) = -> %_, $_ { orig2names(%_, repo2orig(%_, $_)) } | |
our &name2orig is export(:ALL) = -> %_, $_ { %_<provides>{$_} } | |
our &name2repo is export(:ALL) = -> %_, $_ { orig2repo(%_, name2orig(%_, $_)) } | |
} | |
my %meta = Rakudo::Internals::JSON.from-json($json).hash; | |
my &link2orig = Meta::FileLookup::<&link2orig>.assuming(%meta); | |
my &link2repo = Meta::FileLookup::<&link2repo>.assuming(%meta); | |
my &orig2link = Meta::FileLookup::<&orig2link>.assuming(%meta); | |
my &orig2repo = Meta::FileLookup::<&orig2repo>.assuming(%meta); | |
my &orig2names = Meta::FileLookup::<&orig2names>.assuming(%meta); | |
my &repo2orig = Meta::FileLookup::<&repo2orig>.assuming(%meta); | |
my &repo2link = Meta::FileLookup::<&repo2link>.assuming(%meta); | |
my &repo2names = Meta::FileLookup::<&repo2names>.assuming(%meta); | |
my &name2orig = Meta::FileLookup::<&name2orig>.assuming(%meta); | |
my &name2repo = Meta::FileLookup::<&name2repo>.assuming(%meta); | |
use Test; | |
plan 4; | |
subtest { | |
given 'lib/Module/Foo.pm6' { subtest { | |
is .&orig2repo, '{module::xxx::sha1}/abc123'; | |
is .&orig2names, <Module::Foo>; | |
}, $_ } | |
given 'lib/Module/Foo/Bar.pm6' { subtest { | |
is .&orig2repo, '{module::xxx::sha1}/def123'; | |
is .&orig2names, <Module::Foo::Bar Module::Foo::Bar::Helper>; | |
}, $_ } | |
given 'bin/myapp.pl6' { subtest { | |
is .&orig2repo, '{module::xxx::sha1}/ghi123'; | |
}, $_ } | |
given 'resources/libraries/libp5helper.so' { subtest { | |
is .&orig2link, 'resources/libraries/p5helper'; | |
is .&orig2repo, '{module::xxx::sha1}/jkl123'; | |
}, $_ } | |
given 'resources/config.json' { subtest { | |
is .&orig2repo, '{module::xxx::sha1}/mno123'; | |
}, $_ } | |
}, 'from original paths of real files'; | |
subtest { | |
given '{module::xxx::sha1}/abc123' { subtest { | |
is .&repo2orig, 'lib/Module/Foo.pm6'; | |
is .&repo2names, <Module::Foo>; | |
}, $_ } | |
given '{module::xxx::sha1}/def123' { subtest { | |
is .&repo2orig, 'lib/Module/Foo/Bar.pm6'; | |
is .&repo2names, <Module::Foo::Bar Module::Foo::Bar::Helper>; | |
}, $_ } | |
given '{module::xxx::sha1}/ghi123' { subtest { | |
is .&repo2orig, 'bin/myapp.pl6'; | |
}, $_ } | |
given '{module::xxx::sha1}/jkl123' { subtest { | |
is .&repo2link, 'resources/libraries/p5helper'; | |
is .&repo2orig, 'resources/libraries/libp5helper.so'; | |
}, $_ } | |
given '{module::xxx::sha1}/mno123' { subtest { | |
is .&repo2orig, 'resources/config.json'; | |
}, $_ } | |
}, 'from repo paths of real files'; | |
subtest { | |
given 'resources/libraries/p5helper' { subtest { | |
is .&link2orig, 'resources/libraries/libp5helper.so'; | |
is .&link2repo, '{module::xxx::sha1}/jkl123'; | |
}, $_ } | |
}, 'from links'; | |
subtest { | |
given 'Module::Foo' { subtest { | |
is .&name2orig, 'lib/Module/Foo.pm6'; | |
is .&name2repo, '{module::xxx::sha1}/abc123'; | |
}, $_ } | |
given 'Module::Foo::Bar' { subtest { | |
is .&name2orig, 'lib/Module/Foo/Bar.pm6'; | |
is .&name2repo, '{module::xxx::sha1}/def123'; | |
}, $_ } | |
given 'Module::Foo::Bar::Helper' { subtest { | |
is .&name2orig, 'lib/Module/Foo/Bar.pm6'; | |
is .&name2repo, '{module::xxx::sha1}/def123'; | |
}, $_ } | |
}, 'from namespace'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment