aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-02-05 05:42:10 +0100
committerPatrick Spek <p.spek@tyil.nl>2020-02-05 05:42:10 +0100
commite3f4aaa9945bc54b2c8244f7d1fc9560ba087f5c (patch)
tree6f9b419f0dc7ec467d06fe0e127d390e627088ee
parent45cf623d7f464747b1175c1e6e5216fbae3b5945 (diff)
downloadDist::Maker-e3f4aaa9945bc54b2c8244f7d1fc9560ba087f5c.tar.gz
Dist::Maker-e3f4aaa9945bc54b2c8244f7d1fc9560ba087f5c.tar.bz2
Implement bare bones new command
-rw-r--r--META6.json9
-rw-r--r--bin/rdm23
-rw-r--r--lib/Dist/Manager.pm654
-rw-r--r--lib/Dist/Manager/Bin.pm671
-rw-r--r--lib/Dist/Manager/Commands/New.pm694
-rw-r--r--lib/Dist/Manager/Exception.pm639
-rw-r--r--lib/Dist/Manager/Util.pm641
-rw-r--r--lib/X/Dist/Manager/Clobber.pm633
8 files changed, 364 insertions, 0 deletions
diff --git a/META6.json b/META6.json
index 8752762..f0d6bc1 100644
--- a/META6.json
+++ b/META6.json
@@ -4,6 +4,8 @@
"Patrick Spek <p.spek@tyil.work>"
],
"depends": [
+ "Config",
+ "IO::Path::XDG"
],
"description": "Manage Raku module distributions",
"license": "AGPL-3.0",
@@ -11,6 +13,13 @@
"name": "Dist::Manager",
"perl": "6.d",
"provides": {
+ "Dist::Manager": "lib/Dist/Manager.pm6",
+ "Dist::Manager::Bin": "lib/Dist/Manager/Bin.pm6",
+ "Dist::Manager::Commands::New": "lib/Dist/Manager/Commands/New.pm6",
+ "Dist::Manager::Exception": "lib/Dist/Manager/Exception.pm6",
+ "Dist::Manager::Util": "lib/Dist/Manager/Util.pm6",
+ "X::Dist::Manager::Clobber": "lib/X/Dist/Manager/Clobber.pm6",
+ "rdm": "bin/rdm"
},
"resources": [
],
diff --git a/bin/rdm b/bin/rdm
new file mode 100644
index 0000000..28792a7
--- /dev/null
+++ b/bin/rdm
@@ -0,0 +1,23 @@
+#! /usr/bin/env perl6
+
+use v6.d;
+
+use Dist::Manager::Bin;
+
+=begin pod
+
+=NAME rdm
+=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/Dist/Manager.pm6 b/lib/Dist/Manager.pm6
new file mode 100644
index 0000000..e86bab3
--- /dev/null
+++ b/lib/Dist/Manager.pm6
@@ -0,0 +1,54 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Config;
+use IO::Path::XDG;
+
+unit module Dist::Manager;
+
+my Config $config .= new.read: {
+ new => {
+ path => $*HOME,
+ }
+};
+
+sub dm-config-load () is export
+{
+ my $file = xdg-config-home.add('rdm/config.toml');
+
+ $config .= read($file.absolute) if $file.e;
+
+ return;
+}
+
+multi sub dm-config (
+ --> Config
+) is export {
+ $config;
+}
+
+multi sub dm-config (
+ Str:D $key,
+ --> Any
+) is export {
+ $config.get($key);
+}
+
+=begin pod
+
+=NAME Dist::Manager
+=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/Dist/Manager/Bin.pm6 b/lib/Dist/Manager/Bin.pm6
new file mode 100644
index 0000000..e4aea97
--- /dev/null
+++ b/lib/Dist/Manager/Bin.pm6
@@ -0,0 +1,71 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Dist::Manager::Commands::New;
+use Dist::Manager::Exception;
+use Dist::Manager::Util;
+use Dist::Manager;
+
+unit module Dist::Manager::Bin;
+
+#| Create a new module skeleton.
+multi sub MAIN (
+ 'new',
+
+ #| The name of the module.
+ Str $name is copy = Str,
+
+ #| The path where the module directory will be created at.
+ Str $path is copy = Str,
+) is export {
+ CATCH {
+ when Dist::Manager::Exception {
+ note $_.message;
+ exit $_.code;
+ }
+ }
+
+ dm-config-load;
+
+ if (!$name) { $name = dm-prompt('Name'); }
+ if (!$path) { $path = dm-prompt('Path', dm-config('new.path')); }
+
+ my $destination = $path.IO.add($name);
+
+ if ($destination.e) {
+ ClobberException.new(
+ path => $destination.absolute,
+ ).throw;
+ }
+
+ my $description = dm-prompt('Description', :empty);
+ my $license = dm-prompt('License', 'AGPL-3.0');
+
+ dist-manager-new(
+ $name,
+ $description,
+ $license,
+ $path.IO,
+ );
+
+ say "Created skeleton for $name at $destination";
+}
+
+=begin pod
+
+=NAME Dist::Manager::Bin
+=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/Dist/Manager/Commands/New.pm6 b/lib/Dist/Manager/Commands/New.pm6
new file mode 100644
index 0000000..339edb4
--- /dev/null
+++ b/lib/Dist/Manager/Commands/New.pm6
@@ -0,0 +1,94 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use IO::Path::XDG;
+use JSON::Fast;
+use Template::Mustache;
+
+use X::Dist::Manager::Clobber;
+
+constant ClobberException = X::Dist::Manager::Clobber;
+
+unit module Dist::Manager::Commands::New;
+
+#| Create a new Raku module distribution skeleton.
+sub dist-manager-new (
+ #| The name of the new module.
+ Str:D $name,
+
+ #| The description for the new module.
+ Str:D $description,
+
+ #| The SPDX code for this license.
+ Str:D $license,
+
+ #| The path to the directory in which the module skeleton will be
+ #| created.
+ IO::Path:D $path,
+) is export {
+ my $location = $path.add($name);
+
+ # Die early if the destination already exists
+ if ($location.e) {
+ ClobberException.new(
+ path => $location.absolute,
+ ).throw;
+ }
+
+ # Create the base directory
+ $location.mkdir;
+
+ # Create a Hash to represent the META data
+ my %meta =
+ api => '0',
+ version => '0.0.0',
+ perl => '6.d',
+ name => $name,
+ description => $description,
+ license => $license,
+ authors => [
+ 'Patrick Spek <p.spek@tyil.work>',
+ ],
+ source-url => '',
+ auth => '',
+ ;
+
+ # Create a META6.json
+ $location
+ .add('META6.json')
+ .spurt(to-json(%meta, :sorted-keys) ~ "\n")
+ ;
+
+ my $templates = xdg-config-home.add('rdm/templates');
+
+ # Add readme if a template for it exists
+ my $readme = $templates.add('readme.rakudoc');
+
+ if ($readme.f) {
+ $location
+ .add('README.rakudoc')
+ .spurt(Template::Mustache
+ .render($readme.slurp, %meta)
+ )
+ ;
+ }
+}
+
+=begin pod
+
+=NAME Dist::Manager::Commands::New
+=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/Dist/Manager/Exception.pm6 b/lib/Dist/Manager/Exception.pm6
new file mode 100644
index 0000000..cff45e8
--- /dev/null
+++ b/lib/Dist/Manager/Exception.pm6
@@ -0,0 +1,39 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+#| The ErrorCode subset defines all acceptable error codes. These are also used
+#| as exit codes when used from a shell, and therefore come with limits.
+#| 0 = success
+#| 1 = error from raku itself
+#| 2 = called MAIN with invalid arguments
+subset ErrorCode of Int where 2 < * < 255;
+
+#| The base class for Exceptions within Dist::Manager. This should ensure that
+#| error handling is done in similar ways across the code base.
+unit role Dist::Manager::Exception is Exception;
+
+#| The error message to show to the end user.
+method message (--> Str) { … }
+
+#| The error code to return. This is also used for the exit code when used
+#| through the CLI.
+method code (--> ErrorCode) { … }
+
+=begin pod
+
+=NAME Dist::Manager::Exception
+=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/Dist/Manager/Util.pm6 b/lib/Dist/Manager/Util.pm6
new file mode 100644
index 0000000..b980e75
--- /dev/null
+++ b/lib/Dist/Manager/Util.pm6
@@ -0,0 +1,41 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+unit module Dist::Manager::Util;
+
+sub dm-prompt (
+ Str:D $prompt is copy,
+ Str() $default = Str,
+ Bool:D :$empty = False,
+) is export {
+ if ($default) {
+ $prompt ~= " [$default]";
+ }
+
+ loop {
+ my $input = prompt("$prompt: ");
+
+ return $input unless $input eq '';
+ return $default if $default;
+ return '' if $empty;
+ }
+}
+
+=begin pod
+
+=NAME Dist::Manager::Util
+=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/Manager/Clobber.pm6 b/lib/X/Dist/Manager/Clobber.pm6
new file mode 100644
index 0000000..e97c45d
--- /dev/null
+++ b/lib/X/Dist/Manager/Clobber.pm6
@@ -0,0 +1,33 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Dist::Manager::Exception;
+
+unit class X::Dist::Manager::Clobber does Dist::Manager::Exception;
+
+has $.path;
+
+method code () { 3 }
+
+method message () {
+ "File or directory at $!path already exists, not going to clobber it."
+}
+
+=begin pod
+
+=NAME X::Dist::Manager::Clobber
+=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