Skip to content

Commit

Permalink
allow subcommands for poet via MooseX::App::Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jonswar committed Mar 12, 2012
1 parent 74fc02e commit 4d3f960
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
10 changes: 2 additions & 8 deletions bin/poet
@@ -1,12 +1,6 @@
#!/usr/bin/env perl
use Poet::Environment::Generator;

my ( $app_dir, $app_name ) = @ARGV;
die "usage: $0 app_dir AppName" unless defined($app_dir) && defined($app_name);
Poet::Environment::Generator->generate_environment_directory(
root_dir => $app_dir,
app_name => $app_name
);
use Poet::App;
Poet::App->run;

__END__
Expand Down
1 change: 1 addition & 0 deletions dist.ini
Expand Up @@ -39,6 +39,7 @@ Log::Any::Adapter = 0
Mason = 2.00
Mason::Plugin::PSGIHandler = 0.06
Mason::Plugin::RouterSimple = 0
MooseX::App::Cmd = 0

[Prereqs / RuntimeRecommends]
Log::Log4perl = 0
Expand Down
6 changes: 6 additions & 0 deletions lib/Poet/App.pm
@@ -0,0 +1,6 @@
package Poet::App;
use Moose;

extends qw(MooseX::App::Cmd);

1;
40 changes: 40 additions & 0 deletions lib/Poet/App/Command/new.pm
@@ -0,0 +1,40 @@
package Poet::App::Command::new;
use Poet::Moose;
use Poet::Types;

extends 'MooseX::App::Cmd::Command';

has 'app_name' => ( required => 1, isa => 'Poet::Types::AppName', traits => ['Getopt'], cmd_flag => 'app-name', cmd_aliases => 'a', documentation => 'Name of app, e.g. MyApp or ABC (required)' );
has 'dir' => ( isa => 'Str', traits => ['Getopt'], cmd_aliases => 'd', lazy_build => 1, documentation => 'Directory to create; will adapt from app-name if ommitted' );
has 'quiet' => ( isa => 'Bool', traits => ['Getopt'], cmd_aliases => 'q', documentation => 'Suppress most messages' );

method _build_dir () {
return $self->app_name_to_dir( $self->app_name );
}

method app_name_to_dir ($app_name) {
my $dir;
if ( $app_name =~ /^[A-Z]+$/ ) {
$dir = lc($app_name);
}
else {
$dir = lcfirst($app_name);
$dir =~ s/([A-Z])/"_" . lc($1)/ge;
}
return $dir;
}

method abstract () {
"Create a new Poet installation";
}

method execute () {
require Poet::Environment::Generator;
Poet::Environment::Generator->generate_environment_directory(
root_dir => $self->dir,
app_name => $self->app_name,
quiet => $self->quiet
);
}

1;
1 change: 0 additions & 1 deletion lib/Poet/Environment/Generator.pm
Expand Up @@ -22,7 +22,6 @@ method generate_environment_directory ($class: %params) {
die
"cannot generate environment in $root_dir - directory exists and is non-empty"
if ( -d $root_dir && @{ read_dir($root_dir) } );
$root_dir = realpath($root_dir);

my $share_dir =
realpath( $ENV{POET_SHARE_DIR} || File::ShareDir::dist_dir('Poet') );
Expand Down
11 changes: 11 additions & 0 deletions lib/Poet/Types.pm
@@ -0,0 +1,11 @@
package Poet::Types;
use Moose::Util::TypeConstraints;
use strict;
use warnings;

subtype
'Poet::Types::AppName' => as 'Str',
where { /^[[:alpha:]_]\w*$/ },
message { "The app name you provided, '$_', was not a valid identifier" };

1;
18 changes: 18 additions & 0 deletions lib/Poet/t/App.pm
@@ -0,0 +1,18 @@
package Poet::t::App;
use Poet::Test::Util;
use Test::Most;
use strict;
use warnings;
use base qw(Test::Class);

sub test_app_name_to_dir : Tests {
require Poet::App::Command::new;

my $try = sub {
return Poet::App::Command::new->app_name_to_dir( $_[0] );
};
is( $try->("FooBar"), "foo_bar" );
is( $try->("HM"), "hm" );
}

1;

0 comments on commit 4d3f960

Please sign in to comment.