| 1 | # ABSTRACT: Opinionated Command Line Interface | ||
| 2 | package oCLI; | ||
| 3 | use Moo; | ||
| 4 | use Import::Into; | ||
| 5 | use Package::Stash; | ||
| 6 | use oCLI::Context; | ||
| 7 | use oCLI::Request; | ||
| 8 | use oCLI::Plugin; | ||
| 9 | use Module::Runtime qw( use_module ); | ||
| 10 | use Try::Tiny; | ||
| 11 | |||
| 12 | has root => ( | ||
| 13 | is => 'ro' | ||
| 14 | ); | ||
| 15 | |||
| 16 | has render_root => ( | ||
| 17 | is => 'ro', | ||
| 18 | lazy => 1, | ||
| 19 | default => sub { shift->root }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub import { | ||
| 23 | my ( $class, @plugins ) = @_; | ||
| 24 | |||
| 25 | my $target = caller; | ||
| 26 | Package::Stash->new($target)->add_symbol( '%stash', { class => $target, plugins => [ @plugins ] } ); | ||
| 27 | Moo->import::into($target); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub run { | ||
| 31 | my ( $self, @in ) = @_; | ||
| 32 | |||
| 33 | |||
| 34 | my $req = oCLI::Request->new_from_command_line( @in ); | ||
| 35 | |||
| 36 | my $c = oCLI::Context->new( req => $req, root => $self->root ); | ||
| 37 | |||
| 38 | # Load Plugins | ||
| 39 | my $info = (Package::Stash->new($self->root)->get_symbol('%stash')); | ||
| 40 | foreach my $plugin ( @{$info->{plugins}} ) { | ||
| 41 | $c->trace( "Loading plugin $plugin" ); | ||
| 42 | $c->plugin->add( use_module( $plugin )->new ); | ||
| 43 | } | ||
| 44 | |||
| 45 | $c->plugin->hook_after_context( $c ); | ||
| 46 | |||
| 47 | use Data::Dumper; | ||
| 48 | $c->trace("Entering oCLI::Dispatch::dispatch"); | ||
| 49 | $c->trace("Processing The Following Request:" ); | ||
| 50 | $c->trace( $c->req->command_class ); | ||
| 51 | $c->trace( $c->req->command_name ); | ||
| 52 | $c->trace(Dumper($c->req) ); | ||
| 53 | |||
| 54 | # Load Command Class. | ||
| 55 | my $controller = try { | ||
| 56 | if ( $c->req->command_class ) { | ||
| 57 | $c->trace("Trying to load module: " . $self->root . '::Command::' . $c->req->command_class ); | ||
| 58 | use_module( $self->root . '::Command::' . $c->req->command_class )->new(); | ||
| 59 | } else { | ||
| 60 | $c->trace("Trying to load module: " . $self->root ); | ||
| 61 | use_module( $self->root )->new(); | ||
| 62 | } | ||
| 63 | } catch { | ||
| 64 | $c->trace("Failed to load module in oCLI::Dispatch::dispatch"); | ||
| 65 | die "Error: Failed to load command class: $_\n"; | ||
| 66 | }; | ||
| 67 | |||
| 68 | # Load Code | ||
| 69 | my $stash = (Package::Stash->new(ref($controller))->get_symbol('%stash')); | ||
| 70 | my $command = $stash->{command}->{$c->req->command_name}; | ||
| 71 | |||
| 72 | |||
| 73 | $c->plugin->hook_before_code( $c, $command ); | ||
| 74 | |||
| 75 | $c->trace( "Request after hooks ran:" ); | ||
| 76 | $c->trace(Dumper($c->req) ); | ||
| 77 | |||
| 78 | $command->{code}->( $self, $c ); | ||
| 79 | |||
| 80 | $c->plugin->hook_after_code($c); | ||
| 81 | |||
| 82 | $self->render($c) | ||
| 83 | unless $c->req->overrides->{quiet}; | ||
| 84 | |||
| 85 | return $c; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub render { | ||
| 89 | my ( $self, $c ) = @_; | ||
| 90 | |||
| 91 | # Load View Class. | ||
| 92 | $c->stash->{view} ||= 'Text'; | ||
| 93 | my $view = try { | ||
| 94 | use_module( $self->render_root . '::View::' . $c->stash->{view} )->new; | ||
| 95 | } catch { | ||
| 96 | die "Error: Failed to render command class: $_\n"; | ||
| 97 | }; | ||
| 98 | |||
| 99 | $view->render($c); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub config { | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub model { | ||
| 107 | my ( $class, $name, $data ) = @_; | ||
| 108 | |||
| 109 | my $stash = (Package::Stash->new($class)->get_symbol('%stash')); | ||
| 110 | |||
| 111 | $stash->{model}->{$name} = { | ||
| 112 | name => $name, | ||
| 113 | %{$data}, | ||
| 114 | }; | ||
| 115 | |||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | =encoding UTF-8 | ||
| 122 | |||
| 123 | =head1 NAME | ||
| 124 | |||
| 125 | oCLI - Opinionated Command Line Interface | ||
| 126 | |||
| 127 | =head1 DESCRIPTION | ||
| 128 | |||
| 129 | This is an early version of an Command Line Interface framework. It needs more documenting. | ||
| 130 | |||
| 131 |