blib/lib/Command/V2Deprecated.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 261 SCAR 64.8
CC
25
Cov
0%
help_sub_commands
110.0
class_for_sub_command
72.0
_build_sub_command_mapping
56.0
CRAP 650.0
1 package Command::V2; # additional methods to dispatch from a command-line
2 use strict;
3 use warnings;
4  
5 sub sorted_sub_command_classes {
6 no warnings;
7  
8 my @c = shift->sub_command_classes;
9 my @commands_with_position = map { [ $_->sub_command_sort_position, $_ ] } @c;
10  
11 return map { $_->[1] }
12 sort { ($a->[0] <=> $b->[0])
13 ||
14 ($a->[0] cmp $b->[0])
15 }
16 @commands_with_position;
17 }
18  
19 sub sorted_sub_command_names {
20 my $class = shift;
21 my @sub_command_classes = $class->sorted_sub_command_classes;
22 my @sub_command_names = map { $_->command_name_brief } @sub_command_classes;
23 return @sub_command_names;
24 }
25  
26 sub sub_commands_table {
27 my $class = shift;
28 my @sub_command_names = $class->sorted_sub_command_names;
29  
30 my $max_length = 0;
31 for (@sub_command_names) {
32 $max_length = length($_) if ($max_length < length($_));
33 }
34 $max_length ||= 79;
35 my $col_spacer = '_'x$max_length;
36  
37 my $n_cols = floor(80/$max_length);
38 my $n_rows = ceil(@sub_command_names/$n_cols);
39 my @tb_rows;
40 for (my $i = 0; $i < @sub_command_names; $i += $n_cols) {
41 my $end = $i + $n_cols - 1;
42 $end = $#sub_command_names if ($end > $#sub_command_names);
43 push @tb_rows, [@sub_command_names[$i..$end]];
44 }
45 my @col_alignment;
46 for (my $i = 0; $i < $n_cols; $i++) {
47 push @col_alignment, { sample => "&$col_spacer" };
48 }
49 my $tb = Text::Table->new(@col_alignment);
50 $tb->load(@tb_rows);
51 return $tb;
52 }
53  
54 sub help_sub_commands {
55 my $class = shift;
56 my %params = @_;
57 my $command_name_method = 'command_name_brief';
58 #my $command_name_method = ($params{brief} ? 'command_name_brief' : 'command_name');
59
60 my @sub_command_classes = $class->sorted_sub_command_classes;
61  
62 my %categories;
63 my @categories;
64 for my $sub_command_class (@sub_command_classes) {
65 my $category = $sub_command_class->sub_command_category;
66 $category = '' if not defined $category;
67 next if $sub_command_class->_is_hidden_in_docs();
68 my $sub_commands_within_category = $categories{$category};
69 unless ($sub_commands_within_category) {
70 if (defined $category and length $category) {
71 push @categories, $category;
72 }
73 else {
74 unshift @categories,'';
75 }
76 $sub_commands_within_category = $categories{$category} = [];
77 }
78 push @$sub_commands_within_category,$sub_command_class;
79 }
80  
81 no warnings;
82 local $Text::Wrap::columns = 60;
83
84 my $full_text = '';
85 my @full_data;
86 for my $category (@categories) {
87 my $sub_commands_within_this_category = $categories{$category};
88 my @data = map {
89 my @rows = split("\n",Text::Wrap::wrap('', ' ', $_->help_brief));
90 chomp @rows;
91 (
92 [
93 $_->$command_name_method,
94 $_->_shell_args_usage_string_abbreviated,
95 $rows[0],
96 ],
97 map {
98 [
99 '',
100 ' ',
101 $rows[$_],
102 ]
103 } (1..$#rows)
104 );
105 }
106 @$sub_commands_within_this_category;
107  
108 if ($category) {
109 # add a space between categories
110 push @full_data, ['','',''] if @full_data;
111  
112 if ($category =~ /\D/) {
113 # non-numeric categories show their category as a header
114 $category .= ':' if $category =~ /\S/;
115 push @full_data,
116 [
117 Term::ANSIColor::colored(uc($category), 'blue'),
118 '',
119 ''
120 ];
121  
122 }
123 else {
124 # numeric categories just sort
125 }
126 }
127  
128 push @full_data, @data;
129 }
130  
131 my @max_width_found = (0,0,0);
132 for (@full_data) {
133 for my $c (0..2) {
134 $max_width_found[$c] = length($_->[$c]) if $max_width_found[$c] < length($_->[$c]);
135 }
136 }
137  
138 my @colors = (qw/ red bold /);
139 my $text = '';
140 for my $row (@full_data) {
141 for my $c (0..2) {
142 $text .= ' ';
143 $text .= Term::ANSIColor::colored($row->[$c], $colors[$c]),
144 $text .= ' ';
145 $text .= ' ' x ($max_width_found[$c]-length($row->[$c]));
146 }
147 $text .= "\n";
148 }
149 #$DB::single = 1;
150 return $text;
151 }
152  
153 sub sub_command_dirs {
154 my $class = shift;
155 my $subdir = ref($class) || $class;
156 $subdir =~ s|::|\/|g;
157 my @dirs = grep { -d $_ } map { $_ . '/' . $subdir } @INC;
158 return @dirs;
159 }
160  
161 sub sub_command_classes {
162 my $class = shift;
163 my $mapping = $class->_build_sub_command_mapping;
164 return values %$mapping;
165 }
166  
167 sub _build_sub_command_mapping {
168 my $class = shift;
169 $class = ref($class) || $class;
170
171 my $mapping;
172 do {
173 no strict 'refs';
174 $mapping = ${ $class . '::SUB_COMMAND_MAPPING'};
175 };
176
177 unless (defined $mapping) {
178 my $subdir = $class;
179 $subdir =~ s|::|\/|g;
180  
181 for my $lib (@INC) {
182 my $subdir_full_path = $lib . '/' . $subdir;
183 next unless -d $subdir_full_path;
184 my @files = glob($subdir_full_path . '/*');
185 next unless @files;
186 for my $file (@files) {
187 my $basename = basename($file);
188 $basename =~ s/.pm$//;
189 my $sub_command_class_name = $class . '::' . $basename;
190 my $sub_command_class_meta = UR::Object::Type->get($sub_command_class_name);
191 unless ($sub_command_class_meta) {
192 local $SIG{__DIE__};
193 local $SIG{__WARN__};
194 eval "use $sub_command_class_name";
195 }
196 $sub_command_class_meta = UR::Object::Type->get($sub_command_class_name);
197 next unless $sub_command_class_name->isa("Command");
198 next if $sub_command_class_meta->is_abstract;
199 my $name = $class->_command_name_for_class_word($basename);
200 $mapping->{$name} = $sub_command_class_name;
201 }
202 }
203 }
204 return $mapping;
205 }
206  
207 sub sub_command_names {
208 my $class = shift;
209 my $mapping = $class->_build_sub_command_mapping;
210 return keys %$mapping;
211 }
212  
213 sub class_for_sub_command
214 {
215 my $self = shift;
216 my $class = ref($self) || $self;
217 my $sub_command = shift;
218  
219  
220 return if $sub_command =~ /^\-/;
221  
222 my $sub_class = join("", map { ucfirst($_) } split(/-/, $sub_command));
223 $sub_class = $class . "::" . $sub_class;
224  
225 my $meta = UR::Object::Type->get($sub_class); # allow in memory classes
226 unless ( $meta ) {
227 eval "use $sub_class;";
228 if ($@) {
229 if ($@ =~ /^Can't locate .*\.pm in \@INC/) {
230 #die "Failed to find $sub_class! $class_for_sub_command.pm!\n$@";
231 return;
232 }
233 else {
234 my @msg = split("\n",$@);
235 pop @msg;
236 pop @msg;
237 $self->error_message("$sub_class failed to compile!:\n@msg\n\n");
238 return;
239 }
240 }
241 }
242 elsif (my $isa = $sub_class->isa("Command")) {
243 if (ref($isa)) {
244 # dumb modules (Test::Class) mess with the standard isa() API
245 if ($sub_class->SUPER::isa("Command")) {
246 return $sub_class;
247 }
248 else {
249 return;
250 }
251 }
252 return $sub_class;
253 }
254 else {
255 return;
256 }
257 }
258  
259  
260 1;
261  
262  
263 1;
264