Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 23 additions & 49 deletions lib/CLI/Osprey.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,10 @@ sub import {
my (undef, @import_options) = @_;
my $target = caller;

for my $method (qw(with around has)) {
next if $target->can($method);
croak "Can't find the method '$method' in package '$target'. CLI::Osprey requires a Role::Tiny-compatible object system like Moo or Moose.";
}

my $with = $target->can('with');
my $around = $target->can('around');
my $has = $target->can('has');

if ( ! Moo::Role->is_role( $target ) ) { # not in a role
eval "package $target;\n" . q{
sub _osprey_options {
my $class = shift;
return $class->maybe::next::method(@_);
}

sub _osprey_config {
my $class = shift;
return $class->maybe::next::method(@_);
}

sub _osprey_subcommands {
my $class = shift;
return $class->maybe::next::method(@_);
}
1;
} || croak($@);
}
my ( $with, $around, $has ) =
map { $target->can($_)
or croak "Can't find the method '$_' in package '$target'. CLI::Osprey requires a Role::Tiny-compatible object system like Moo or Moose.";
} qw[ with around has ];

my $osprey_config = {
preserve_argv => 1,
Expand All @@ -58,27 +34,9 @@ sub import {
@import_options,
};

$around->(_osprey_config => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$osprey_config;
});

my $options_data = { };
my $subcommands = { };

my $apply_modifiers = sub {
return if $target->can('new_with_options');
$with->('CLI::Osprey::Role');
$around->(_osprey_options => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$options_data;
});
$around->(_osprey_subcommands => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$subcommands;
});
};

my $added_order = 0;

my $option = sub {
Expand All @@ -87,7 +45,6 @@ sub import {
$has->($name => _non_option_attributes(%attributes));
$options_data->{$name} = _option_attributes($name, %attributes);
$options_data->{$name}{added_order} = ++$added_order;
$apply_modifiers->();
};

my $subcommand = sub {
Expand All @@ -105,7 +62,6 @@ sub import {
}

$subcommands->{$name} = $subobject;
$apply_modifiers->();
};

if (my $info = $Role::Tiny::INFO{$target}) {
Expand All @@ -119,7 +75,25 @@ sub import {
*{"${target}::subcommand"} = $subcommand;
}

$apply_modifiers->();
unless ( Moo::Role::does_role( $target, 'CLI::Osprey::Role') ) {

$with->('CLI::Osprey::Role');

$around->(_osprey_config => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$osprey_config;
});

$around->(_osprey_options => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$options_data;
});

$around->(_osprey_subcommands => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$subcommands;
});
}

return;
}
Expand Down
17 changes: 15 additions & 2 deletions lib/CLI/Osprey/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ sub _osprey_fix_argv {

use Moo::Role;

requires qw(_osprey_config _osprey_options _osprey_subcommands);

has 'parent_command' => (
is => 'ro',
);
Expand All @@ -130,6 +128,21 @@ has 'invoked_as' => (
is => 'ro',
);

sub _osprey_options {
my $class = shift;
return $class->maybe::next::method(@_);
}

sub _osprey_config {
my $class = shift;
return $class->maybe::next::method(@_);
}

sub _osprey_subcommands {
my $class = shift;
return $class->maybe::next::method(@_);
}

sub new_with_options {
my ($class, %params) = @_;
my %config = $class->_osprey_config;
Expand Down