File Coverage

lib/App/PRT/CLI.pm
Criterion Covered Total %
statement 69 69 100.0
branch 16 16 100.0
condition n/a
subroutine 15 15 100.0
pod 0 5 0.0
total 100 105 95.2


line stmt bran cond sub pod time code
1             package App::PRT::CLI;
2 1     1   4111 use strict;
  1         3  
  1         48  
3 1     1   5 use warnings;
  1         2  
  1         40  
4              
5 1     1   17 use Class::Load qw(load_class);
  1         2  
  1         60  
6 1     1   1162 use Getopt::Long qw(GetOptionsFromArray);
  1         14081  
  1         8  
7 1     1   228 use Cwd ();
  1         2  
  1         18  
8 1     1   2415 use App::PRT::Collector::Files;
  1         3  
  1         37  
9 1     1   5969 use App::PRT::Collector::AllFiles;
  1         3  
  1         33  
10 1     1   1127 use App::PRT::Collector::GitDirectory;
  1         2  
  1         534  
11              
12             sub new {
13 11     11 0 473898 my ($class) = @_;
14              
15 11         69 bless {}, $class;
16             }
17              
18             sub parse {
19 9     9 0 12611 my ($self, @args) = @_;
20              
21 9 100       56 my $command = shift @args or die 'prt ';
22              
23 8         40 my $command_class = $self->_command_name_to_command_class($command);
24              
25 8         22 eval {
26 8         36 load_class $command_class;
27             };
28              
29 8 100       1863 if ($@) {
30 1         46 die "Command $command not found ($@)";
31             }
32              
33 7         66 $self->{command} = $command_class->new;
34              
35 7         100 my @rest_args = $self->{command}->parse_arguments(@args);
36              
37 7         41 my $collector = $self->_prepare_collector(@rest_args);
38 7 100       50 unless ($collector) {
39 1         28 die 'Cannot decide target files';
40             }
41 6         21 $self->{collector} = $collector;
42              
43 6         24 1;
44             }
45              
46             sub run {
47 2     2 0 362 my ($self) = @_;
48              
49 2         8 my $collector = $self->collector;
50 2         8 my $command = $self->command;
51              
52 2 100       17 if ($command->can('execute_files')) { # TODO: create a base class for command?
53 1         5 $command->execute_files($collector->collect);
54             } else {
55 1         2 for my $file (@{$collector->collect}) {
  1         5  
56 1         4 $command->execute($file);
57             }
58             }
59             }
60              
61             sub _prepare_collector {
62 7     7   18 my ($class, @args) = @_;
63              
64             # target files specified?
65 7 100       27 if (@args) {
66 3         264 return App::PRT::Collector::Files->new(@args);
67             }
68              
69 4         21 my $cwd = Cwd::getcwd;
70              
71             # git directory?
72 4         431 my $git_root_directory = App::PRT::Collector::GitDirectory->find_git_root_directory($cwd);
73 4 100       41 if ($git_root_directory) {
74 1         10 return App::PRT::Collector::GitDirectory->new($git_root_directory);
75             }
76              
77             # seems perl project?
78 3         42 my $project_root_directory = App::PRT::Collector::AllFiles->find_project_root_directory($cwd);
79 3 100       59 if ($project_root_directory) {
80 2         21 return App::PRT::Collector::AllFiles->new($project_root_directory);
81             }
82              
83 1         4 return;
84             }
85              
86             sub command {
87 6     6 0 27 my ($self) = @_;
88              
89 6         61 $self->{command};
90             }
91              
92             sub collector {
93 6     6 0 8213 my ($self) = @_;
94              
95 6         33 $self->{collector};
96             }
97              
98             sub _command_name_to_command_class {
99 11     11   694 my ($self, $name) = @_;
100              
101 11         62 my $command_class = join '', map { ucfirst } split '_', $name;
  21         75  
102              
103             # XXX: Super hack to fix typo
104 11 100       39 if ($command_class eq 'RenameNamespace') {
105 2         8 $command_class = 'RenameNameSpace';
106             }
107              
108 11         44 'App::PRT::Command::' . $command_class;
109             }
110              
111             1;