From 6ddf75296ec746075ea6760a34c0d60a14e48f0a Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Sun, 20 Feb 2022 16:25:42 +0100 Subject: [PATCH] Make sure tools/install-dist.p6 is also available as .raku Because we want to be able to deprecate the use of the .p6 extension. --- tools/install-dist.raku | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 tools/install-dist.raku diff --git a/tools/install-dist.raku b/tools/install-dist.raku new file mode 100755 index 00000000000..cf3a445daa2 --- /dev/null +++ b/tools/install-dist.raku @@ -0,0 +1,98 @@ +#!/usr/bin/env raku + +=begin pod + +This script is for installing Raku modules. B does the same module registration like the 'zef' tool. + +B makes it easy to install a module system wide. + +=head1 OPTIONS + +By default the destination is the site of the Raku library. + + # Install to a custom location + --to= # /home/username/my_raku_mod_dir + +If you specify a destination that does not exists then it will be created. The --to option can only be used together with the --for option. + + --for=[ vendor | site ] + + --from= # default is the current directory + + +The command in the install session for packaging a Raku module could be done in the form: + + install-dist.p6 --to= --for=vendor + +It is recommended to set the environment variable 'RAKUDO_RERESOLVE_DEPENDENCIES' by using the script: + + RAKUDO_RERESOLVE_DEPENDENCIES=0 install-dist.p6 --to= --for=site + +=end pod + + +use v6.c; +use CompUnit::Repository::Staging; + +# Distribution::Path ignores META.info files, but we can manually set it +sub find-meta-file($dir) { + ('META6.json', 'META.info').map({$dir.child($_)}).first: {$_ ~~ :f} +} + +multi sub MAIN(:from(:$dist-prefix) = '.', :to(:$repo-prefix)!, :for(:$repo-name)!, Bool :$build = True) { + my $meta-file = find-meta-file($dist-prefix.IO); + my $dist = Distribution::Path.new($dist-prefix.IO, :$meta-file); + + build(:dist-prefix($dist-prefix.IO)) if $build; + + CompUnit::Repository::Staging.new( + :prefix($repo-prefix), + :next-repo(CompUnit::RepositoryRegistry.repository-for-name($repo-name)), + :name($repo-name), + ).install($dist); + + $_.unlink for .map: {$repo-prefix.IO.child($_)}; +} + +multi sub MAIN(:from(:$dist-prefix) = '.', :to(:$repo-id) = 'site', Bool :$force, Bool :$build = True) { + my $meta-file = find-meta-file($dist-prefix.IO); + my $dist = Distribution::Path.new($dist-prefix.IO, :$meta-file); + + build(:dist-prefix($dist-prefix.IO)) if $build; + + my $repo = first * ~~ CompUnit::Repository::Installable, + CompUnit::RepositoryRegistry.repository-for-name($repo-id), + CompUnit::RepositoryRegistry.repository-for-spec($repo-id) + or die "Repository '$repo-id' is not an installable target"; + + $repo.install($dist, :$force); +} + +multi sub MAIN(:from(:$dist-prefix) = '.', Bool :$only-build!) { + my $meta-file = find-meta-file($dist-prefix.IO); + my $dist = Distribution::Path.new($dist-prefix.IO, :$meta-file); + + build(:dist-prefix($dist-prefix.IO)); +} + +sub build(IO::Path :$dist-prefix) { + my $meta-file = find-meta-file($dist-prefix); + my $meta = Rakudo::Internals::JSON.from-json($meta-file.slurp); + + if ($meta) { + my $builder-class = (require ::("$meta")); + + my $builder = $builder-class.new(:$meta); + + if $builder.can-build { + $builder.build; + exit; + } + else { + note "Failed to build"; + exit 1; + } + } +} + +# vim: ft=perl6