diff options
-rw-r--r-- | META6.json | 6 | ||||
-rw-r--r-- | bin/rdm | 4 | ||||
-rw-r--r-- | lib/Dist/Maker/Bin.rakumod | 97 | ||||
-rw-r--r-- | lib/Dist/Maker/Commands/Add.rakumod | 54 | ||||
-rw-r--r-- | lib/X/Dist/Maker/DuplicateProvide.rakumod | 33 |
5 files changed, 188 insertions, 6 deletions
@@ -17,13 +17,15 @@ "provides": { "Dist::Maker": "lib/Dist/Maker.rakumod", "Dist::Maker::Bin": "lib/Dist/Maker/Bin.rakumod", + "Dist::Maker::Commands::Add": "lib/Dist/Maker/Commands/Add.rakumod", "Dist::Maker::Commands::Depend": "lib/Dist/Maker/Commands/Depend.rakumod", "Dist::Maker::Commands::New": "lib/Dist/Maker/Commands/New.rakumod", "Dist::Maker::Exception": "lib/Dist/Maker/Exception.rakumod", "Dist::Maker::Util": "lib/Dist/Maker/Util.rakumod", "X::Dist::Maker::Clobber": "lib/X/Dist/Maker/Clobber.rakumod", - "X::Dist::Maker::DuplicateDependency": "lib/X/Dist/Maker/DuplicateDependency.pm6", - "X::Dist::Maker::NoDistribution": "lib/X/Dist/Maker/NoDistribution.pm6", + "X::Dist::Maker::DuplicateDependency": "lib/X/Dist/Maker/DuplicateDependency.rakumod", + "X::Dist::Maker::DuplicateProvide": "lib/X/Dist/Maker/DuplicateProvide.rakumod", + "X::Dist::Maker::NoDistribution": "lib/X/Dist/Maker/NoDistribution.rakumod", "rdm": "bin/rdm" }, "resources": [ @@ -2,6 +2,10 @@ use v6.d; +my %*SUB-MAIN-OPTS = + :named-anywhere, +; + use Dist::Maker::Bin; =begin pod diff --git a/lib/Dist/Maker/Bin.rakumod b/lib/Dist/Maker/Bin.rakumod index 30c2211..bef08b6 100644 --- a/lib/Dist/Maker/Bin.rakumod +++ b/lib/Dist/Maker/Bin.rakumod @@ -2,8 +2,11 @@ use v6.d; +use IO::Path::XDG; use JSON::Fast; +use Template::Mustache; +use Dist::Maker::Commands::Add; use Dist::Maker::Commands::Depend; use Dist::Maker::Commands::New; use Dist::Maker::Exception; @@ -11,16 +14,102 @@ use Dist::Maker::Util; use Dist::Maker; use X::Dist::Maker::Clobber; use X::Dist::Maker::DuplicateDependency; +use X::Dist::Maker::DuplicateProvide; use X::Dist::Maker::NoDistribution; unit module Dist::Maker::Bin; -#| Add a dependency. +#| Add a new file. +multi sub MAIN ( + 'add', + + #| The name of the new unit. + Str $provide is copy = Str, + + #| The kind of provide to create. See Dist::Maker::Commands::Add for + #| more information. + Str :$kind = Str, +) is export { + CATCH { + when Dist::Maker::Exception { + note $_.message; + exit $_.code; + } + } + + dm-config-load; + + my $meta-file = dm-scour('META6.json', $*CWD); + + if (!$meta-file) { + X::Dist::Maker::NoDistribution.new( + path => $*CWD, + ).throw + } + + my %meta = from-json($meta-file.slurp); + + if (!$provide) { $provide = dm-prompt('Unit name') } + + if (%meta<provides>.keys ∋ $provide) { + X::Dist::Maker::DuplicateProvide.new( + :$provide, + ).throw + } + + my $contents = Str; + my $template = xdg-config-home.add('rdm/templates/provide.raku'); + + if ($template.f) { + my $body; + + if ($kind) { + my $kind-template = $template + .parent + .add("kinds/$kind.raku"); + + if ($kind-template.f) { + $body = Template::Mustache.render( + $kind-template.slurp, + { + :%meta, + this => { + :$provide, + }, + }, + ); + } else { + note "No template found at $kind-template.absolute()"; + } + } + + $contents = Template::Mustache.render($template.slurp, { + :%meta, + body => $body.?trim // '# TODO', + this => { + :$provide, + } + }); + } + + my $file = dist-maker-add( + $provide, + $meta-file, + :$contents, + :!sanity-checks + ); + + if (!dm-config('main.quiet')) { + say "Added $file to provide $provide for %meta<name>"; + } +} + +#| Add a module to depend on. multi sub MAIN ( 'depend', - #| The module to add to the dependencies. - Str $module, + #| The name of the module. + Str $module is copy = Str, ) is export { CATCH { when Dist::Maker::Exception { @@ -43,7 +132,7 @@ multi sub MAIN ( if (!$module) { $module = dm-prompt('Dependency name'); } - if (%meta<depends> (cont) $module) { + if (%meta<depends> ∋ $module) { X::Dist::Maker::DuplicateDependency.new( :$module ).throw diff --git a/lib/Dist/Maker/Commands/Add.rakumod b/lib/Dist/Maker/Commands/Add.rakumod new file mode 100644 index 0000000..734d878 --- /dev/null +++ b/lib/Dist/Maker/Commands/Add.rakumod @@ -0,0 +1,54 @@ +#! /usr/bin/env false + +use v6.d; + +use JSON::Fast; + +unit module Dist::Maker::Commands::Add; + +sub dist-maker-add ( + Str:D $provide, + IO::Path:D $meta-json, + Str :$contents, + Bool:D :$sanity-checks = True, + --> IO::Path +) is export { + my %meta = $meta-json.slurp.&from-json; + + if (%meta<provides>.keys ∋ $provide) { + X::Dist::Maker::DuplicateProvide.new( + :$provide, + ).throw + } + + my $provide-file = $meta-json + .parent + .add("lib/{$provide.subst('::', '/', :g)}.rakumod") + ; + + $provide-file.parent.mkdir; + $provide-file.spurt($contents // ''); + + %meta<provides>{$provide} = $provide-file.relative($meta-json.parent); + $meta-json.spurt(to-json(%meta, :sorted-keys) ~ "\n"); + + $provide-file; +} + +=begin pod + +=NAME Dist::Maker::Commands::Add +=AUTHOR Patrick Spek <p.spek@tyil.work> +=VERSION 0.0.0 + +=head1 Synopsis + +=head1 Description + +=head1 Examples + +=head1 See also + +=end pod + +# vim: ft=perl6 noet diff --git a/lib/X/Dist/Maker/DuplicateProvide.rakumod b/lib/X/Dist/Maker/DuplicateProvide.rakumod new file mode 100644 index 0000000..7b24251 --- /dev/null +++ b/lib/X/Dist/Maker/DuplicateProvide.rakumod @@ -0,0 +1,33 @@ +#! /usr/bin/env false + +use v6.d; + +use Dist::Maker::Exception; + +unit class X::Dist::Maker::DuplicateProvide does Dist::Maker::Exception; + +has $.provide; + +method code () { 5 } + +method message () { + "$!provide is already provided." +} + +=begin pod + +=NAME X::Dist::Maker::DuplicateProvide +=AUTHOR Patrick Spek <p.spek@tyil.work> +=VERSION 0.0.0 + +=head1 Synopsis + +=head1 Description + +=head1 Examples + +=head1 See also + +=end pod + +# vim: ft=perl6 noet |