Search code examples
raku

How do I install local modules?


For creating and maintaining Perl 5 modules, I use Dist::Zilla. One of my favorite features is being able to install local modules.

However, with Perl 6, I'm not sure how to install local modules. Sure, I can use use lib:

use lib 'relative/path';
use My::Awesome::Module;

But, I'd really like to be able to install My::Awesome::Module, so that all I had to do was use it:

use My::Awesome::Module;

One way to accomplish this, would be setting PERL6LIB, but that still isn't "installing" a module like zef install ./My-Awesome-Module.


Update: Looks like I need to craft an appropriate META6.json file.


Solution

  • To understand how to setup a module to be understood by toolchain utilities, see Preparing the module. Typically this means adding a META6.json file that describes the distribution, including quasi-manifest elements such as which files you really meant to include/provide. Once the META6.json is created the module is ready to be installed:

    zef install ./My-Awesome-Module
    

    which (assuming no uninstalled dependencies) is essentially:

    my $install-to-repo = CompUnit::RepositoryRegistry.repository-for-name("site");
    my $preinstall-dist = Distribution::Path.new("./My-Awesome-Module");
    $install-to-repo.install($preinstall-dist);
    

    Starting with rakudo 2019.01 you can, assuming no uninstalled dependencies, install a local distribution without a META6.json -- but this is purely developmental nicety that won't work on complex setups that do not have e.g. namespacing and file structures that can be inferred.

    my $read-from-repo   = CompUnit::Repository::FileSystem.new(prefix => "./My-Awesome-Module/lib");
    my $install-to-repo  = CompUnit::RepositoryRegistry.repository-for-name("site");
    my $some-module-name = "My::Awesome::Module"; # needed to get at the Distribution object in the next step
    my $preinstall-dist  = $read-from-repo.candidates($some-module-name).head;
    $install-to-repo.install($preinstall-dist);