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