| 1 | package App::PRT::CLI; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Class::Load qw(load_class); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Getopt::Long qw(GetOptionsFromArray); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use IO::Interactive qw(is_interactive); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Cwd (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use App::PRT::Collector::FileHandle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use App::PRT::Collector::Files; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use App::PRT::Collector::AllFiles; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use App::PRT::Collector::GitDirectory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 15 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 15 | 15 | my ($class) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 15 | bless {}, $class; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 3 | ▶ | sub set_io { | ||||||||
|
Subroutine
set_io: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | 3 | my ($self, $stdin, $stdout) = @_; | |||||||||
| 22 | 3 | $self->{input} = $stdin; | |||||||||
| 23 | 3 | $self->{output} = $stdout; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 12 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 27 | 12 | my ($self, @args) = @_; | |||||||||
| 28 | |||||||||||
| 29 | 12 | ▶ | my $command = shift @args or die 'prt <command> <args>'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | 11 | my $command_class = $self->_command_name_to_command_class($command); | |||||||||
| 32 | |||||||||||
| 33 | 11 | eval { | |||||||||
| 34 | 11 | load_class $command_class; | |||||||||
| 35 | }; | ||||||||||
| 36 | |||||||||||
| 37 | 11 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 1 | die "Command $command not found ($@)"; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 10 | $self->{command} = $command_class->new; | |||||||||
| 42 | |||||||||||
| 43 | 10 | my @rest_args = $self->{command}->parse_arguments(@args); | |||||||||
| 44 | |||||||||||
| 45 | 10 | my $collector = $self->_prepare_collector(@rest_args); | |||||||||
| 46 | 10 | ▶ | unless ($collector) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | die 'Cannot decide target files'; | |||||||||
| 48 | } | ||||||||||
| 49 | 9 | $self->{collector} = $collector; | |||||||||
| 50 | |||||||||||
| 51 | 9 | 1; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 4 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 55 | 4 | my ($self) = @_; | |||||||||
| 56 | |||||||||||
| 57 | 4 | my $collector = $self->collector; | |||||||||
| 58 | 4 | my $command = $self->command; | |||||||||
| 59 | |||||||||||
| 60 | 4 | ▶ | if ($command->can('execute_files')) { # TODO: create a base class for command? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 1 | $command->execute_files($collector->collect, $self->{output}); | |||||||||
| 62 | } else { | ||||||||||
| 63 | 3 | for my $file (@{$collector->collect}) { | |||||||||
| 64 | 3 | $command->execute($file, $self->{output}); | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 10 | ▶ | sub _prepare_collector { | ||||||||
|
Subroutine
_prepare_collector: called CC=5 SCAR=16.1
| |||||||||||
| 70 | 10 | my ($self, @args) = @_; | |||||||||
| 71 | |||||||||||
| 72 | # target files specified? | ||||||||||
| 73 | 10 | ▶ | if (@args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 3 | return App::PRT::Collector::Files->new(@args); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # STDIN from pipe? | ||||||||||
| 78 | 7 | ▶ | if ($self->_input_is_pipe) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 3 | return App::PRT::Collector::FileHandle->new($self->{input}); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 4 | my $cwd = Cwd::getcwd; | |||||||||
| 83 | |||||||||||
| 84 | # git directory? | ||||||||||
| 85 | 4 | my $git_root_directory = App::PRT::Collector::GitDirectory->find_git_root_directory($cwd); | |||||||||
| 86 | 4 | ▶ | if ($git_root_directory) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 1 | return App::PRT::Collector::GitDirectory->new($git_root_directory); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | # seems perl project? | ||||||||||
| 91 | 3 | my $project_root_directory = App::PRT::Collector::AllFiles->find_project_root_directory($cwd); | |||||||||
| 92 | 3 | ▶ | if ($project_root_directory) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 2 | return App::PRT::Collector::AllFiles->new($project_root_directory); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 1 | return; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | # -t Filehandle is opened to a tty. | ||||||||||
| 100 | 6 | ▶ | sub _input_is_pipe { | ||||||||
|
Subroutine
_input_is_pipe: called CC=2 SCAR=6.9
| |||||||||||
| 101 | 6 | my ($self) = @_; | |||||||||
| 102 | 6 | ▶ | $self->{input} && ! is_interactive($self->{input}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 8 | ▶ | sub command { | ||||||||
|
Subroutine
command: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 106 | 8 | my ($self) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 8 | $self->{command}; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 9 | ▶ | sub collector { | ||||||||
|
Subroutine
collector: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 112 | 9 | my ($self) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 9 | $self->{collector}; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 14 | ▶ | sub _command_name_to_command_class { | ||||||||
|
Subroutine
_command_name_to_command_class: called CC=2 SCAR=6.9
| |||||||||||
| 118 | 14 | my ($self, $name) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 14 | my $command_class = join '', map { ucfirst } split '_', $name; | |||||||||
| 121 | |||||||||||
| 122 | # XXX: Super hack to fix typo | ||||||||||
| 123 | 14 | ▶ | if ($command_class eq 'RenameNamespace') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 2 | $command_class = 'RenameNameSpace'; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 14 | 'App::PRT::Command::' . $command_class; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 2 | 1; | |||||||||