brian d foy
brian d foy

Reputation: 132913

How can I tell programmatically if a Raku module is installed?

I was playing around with a plugin thingy that would load things that were available. The docs on the $*REPO is not quite there so I guessed a bit. This seems to work but I have the feeling I'm missing something simpler (besides the regular golfing on the other bits):

my @modules = <Digest::MD5 NotThere PrettyDump>;
my @installed = gather installed-modules( @modules );

put "Already installed: @installed[]";
try require ::( @installed[0] );

# is there a better way to do this without eval
my $digest = ::( @installed[0] ).new;

sub installed-modules ( *@candidates ) {
    for @candidates -> $module {
        put $module, '-' x 15;
        my $ds = CompUnit::DependencySpecification.new:
            :short-name($module);
        if $*REPO.resolve: $ds {
            put "Found $module";
            take $module;
            }
        else {
            put "Didn't find $module";
            }
        }
    }

Upvotes: 10

Views: 382

Answers (1)

ugexe
ugexe

Reputation: 5726

$*REPO.resolve(CompUnit::DependencySpecification.new(:short-name<Test>))

Note that this is only useful to a certain degree as this only tells you if a module can be resolved. What I mean by this is it would also detect a non-installed module being provided by a directory such as -I lib, and you won't know which CompUnit::Repository it came from. You could also grep the results of something like $*REPO.repo-chain.grep(* ~~ CompUnit::Repository::Installable).map(*.installed).flat

Additionally the meaning of an "installed" module is not so simple -- CompUnit::Repository::Installable repositories are what is likely implied, but consider a third party CompUnit::Repository ( such as https://github.com/ugexe/Raku-CompUnit--Repository--Tar ) -- with this modules are essentially still installed, but the repo itself is not CompUnit::Repository::Installable. All ::Installable really means in rakudo is that rakudo knows how to install it -- it has nothing to do with what rakudo knows how to find and load

Some PRs ( closed, but I will revisit eventually ) that help address some of these problems via a method candidates { ... }:

https://github.com/rakudo/rakudo/pull/1125

https://github.com/rakudo/rakudo/pull/1132

Upvotes: 11

Related Questions