| 1 | package Command::Tree; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | use File::Basename qw/basename/; | ||
| 7 | |||
| 8 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 9 | |||
| 10 | class Command::Tree { | ||
| 11 | is => 'Command::V2', | ||
| 12 | is_abstract => 1, | ||
| 13 | doc => 'base class for commands which delegate to sub-commands', | ||
| 14 | }; | ||
| 15 | |||
| 16 | sub resolve_class_and_params_for_argv { | ||
| 17 | # This is used by execute_with_shell_params_and_exit, but might be used within an application. | ||
| 18 | my $self = shift; | ||
| 19 | my @argv = @_; | ||
| 20 | |||
| 21 | if ( $argv[0] and $argv[0] !~ /^\-/ | ||
| 22 | and my $class_for_sub_command = $self->class_for_sub_command($argv[0]) ) { | ||
| 23 | # delegate | ||
| 24 | shift @argv; | ||
| 25 | return $class_for_sub_command->resolve_class_and_params_for_argv(@argv); | ||
| 26 | } | ||
| 27 | elsif ( @argv == 1 and $argv[0] =~ /^(\-)?\-h(elp)?$/ ) { # HELP ME! | ||
| 28 | return ($self, { help => 1 }); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | # error | ||
| 32 | return ($self,undef); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub resolve_option_completion_spec { | ||
| 37 | my $class = shift; | ||
| 38 | my @completion_spec; | ||
| 39 | |||
| 40 | my @sub = eval { $class->sub_command_names }; | ||
| 41 | if ($@) { | ||
| 42 | $class->warning_message("Couldn't load class $class: $@\nSkipping $class..."); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | for my $sub (@sub) { | ||
| 46 | my $sub_class = $class->class_for_sub_command($sub); | ||
| 47 | my $sub_tree = $sub_class->resolve_option_completion_spec() if defined($sub_class); | ||
| 48 | |||
| 49 | # Hack to fix several broken commands, this should be removed once commands are fixed. | ||
| 50 | # If the commands were not broken then $sub_tree will always exist. | ||
| 51 | # Basically if $sub_tree is undef then we need to remove '>' to not break the OPTS_SPEC | ||
| 52 | if ($sub_tree) { | ||
| 53 | push @completion_spec, '>' . $sub => $sub_tree; | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | if (defined $sub_class) { | ||
| 57 | print "WARNING: $sub has sub_class $sub_class of ($class) but could not resolve option completion spec for it.\n". | ||
| 58 | "Setting $sub to non-delegating command, investigate to correct tab completion.\n"; | ||
| 59 | } else { | ||
| 60 | print "WARNING: $sub has no sub_class so could not resolve option completion spec for it.\n". | ||
| 61 | "Setting $sub to non-delegating command, investigate to correct tab completion.\n"; | ||
| 62 | } | ||
| 63 | push @completion_spec, $sub => undef; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | push @completion_spec, "help!" => undef; | ||
| 67 | |||
| 68 | return \@completion_spec | ||
| 69 | } | ||
| 70 | |||
| 71 | sub help_brief { | ||
| 72 | my $self = shift; | ||
| 73 | if (my $doc = $self->__meta__->doc) { | ||
| 74 | return $doc; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | my @parents = $self->__meta__->ancestry_class_metas; | ||
| 78 | for my $parent (@parents) { | ||
| 79 | if (my $doc = $parent->doc) { | ||
| 80 | return $doc; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return ""; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub doc_help { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | my $command_name = $self->command_name; | ||
| 91 | my $text; | ||
| 92 | |||
| 93 | # show the list of sub-commands | ||
| 94 | $text = sprintf( | ||
| 95 | "Sub-commands for %s:\n%s", | ||
| 96 | Term::ANSIColor::colored($command_name, 'bold'), | ||
| 97 | $self->help_sub_commands, | ||
| 98 | ); | ||
| 99 | |||
| 100 | return $text; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub doc_manual { | ||
| 105 | my $self = shift; | ||
| 106 | my $pod = $self->_doc_name_version; | ||
| 107 | |||
| 108 | my $manual = $self->_doc_manual_body; | ||
| 109 | my $help = $self->help_detail; | ||
| 110 | if ($manual or $help) { | ||
| 111 | $pod .= "=head1 DESCRIPTION:\n\n"; | ||
| 112 | |||
| 113 | my $txt = $manual || $help; | ||
| 114 | if ($txt =~ /^\=/) { | ||
| 115 | # pure POD | ||
| 116 | $pod .= $manual; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | $txt =~ s/\n/\n\n/g; | ||
| 120 | $pod .= $txt; | ||
| 121 | #$pod .= join('', map { " $_\n" } split ("\n",$txt)) . "\n"; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | my $sub_commands = $self->help_sub_commands(brief => 1); | ||
| 127 | $pod .= "=head1 SUB-COMMANDS\n\n" . $sub_commands . "\n\n"; | ||
| 128 | |||
| 129 | $pod .= $self->_doc_footer(); | ||
| 130 | $pod .= "\n\n=cut\n\n"; | ||
| 131 | return "\n$pod"; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub sorted_sub_command_classes { | ||
| 135 | no warnings; | ||
| 136 | my @c = map { [ $_->sub_command_sort_position, $_ ] } shift->sub_command_classes; | ||
| 137 | return map { $_->[1] } | ||
| 138 | sort { | ||
| 139 | ($a->[0] <=> $b->[0]) | ||
| 140 | || | ||
| 141 | ($a->[0] cmp $b->[0]) | ||
| 142 | } | ||
| 143 | @c; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub sorted_sub_command_names { | ||
| 147 | my $class = shift; | ||
| 148 | my @sub_command_classes = $class->sorted_sub_command_classes; | ||
| 149 | my @sub_command_names = map { $_->command_name_brief } @sub_command_classes; | ||
| 150 | return @sub_command_names; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub sub_commands_table { | ||
| 154 | my $class = shift; | ||
| 155 | my @sub_command_names = $class->sorted_sub_command_names; | ||
| 156 | |||
| 157 | my $max_length = 0; | ||
| 158 | for (@sub_command_names) { | ||
| 159 | $max_length = length($_) if ($max_length < length($_)); | ||
| 160 | } | ||
| 161 | $max_length ||= 79; | ||
| 162 | my $col_spacer = '_'x$max_length; | ||
| 163 | |||
| 164 | my $n_cols = floor(80/$max_length); | ||
| 165 | my $n_rows = ceil(@sub_command_names/$n_cols); | ||
| 166 | my @tb_rows; | ||
| 167 | for (my $i = 0; $i < @sub_command_names; $i += $n_cols) { | ||
| 168 | my $end = $i + $n_cols - 1; | ||
| 169 | $end = $#sub_command_names if ($end > $#sub_command_names); | ||
| 170 | push @tb_rows, [@sub_command_names[$i..$end]]; | ||
| 171 | } | ||
| 172 | my @col_alignment; | ||
| 173 | for (my $i = 0; $i < $n_cols; $i++) { | ||
| 174 | push @col_alignment, { sample => "&$col_spacer" }; | ||
| 175 | } | ||
| 176 | my $tb = Text::Table->new(@col_alignment); | ||
| 177 | $tb->load(@tb_rows); | ||
| 178 | return $tb; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _categorize_sub_commands { | ||
| 182 | my $class = shift; | ||
| 183 | |||
| 184 | my @sub_command_classes = $class->sorted_sub_command_classes; | ||
| 185 | my %categories; | ||
| 186 | my @order; | ||
| 187 | for my $sub_command_class (@sub_command_classes) { | ||
| 188 | next if $sub_command_class->_is_hidden_in_docs(); | ||
| 189 | my $category = $sub_command_class->sub_command_category || ''; | ||
| 190 | unless (exists $categories{$category}) { | ||
| 191 | if ($category) { | ||
| 192 | push(@order, $category) | ||
| 193 | } else { | ||
| 194 | unshift(@order, ''); | ||
| 195 | } | ||
| 196 | $categories{$category} = []; | ||
| 197 | } | ||
| 198 | push(@{$categories{$category}}, $sub_command_class); | ||
| 199 | } | ||
| 200 | |||
| 201 | return (\@order, \%categories); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub help_sub_commands { | ||
| 205 | my ($self, %params) = @_; | ||
| 206 | my ($order, $categories) = $self->_categorize_sub_commands(@_); | ||
| 207 | my $command_name_method = 'command_name_brief'; | ||
| 208 | |||
| 209 | no warnings; | ||
| 210 | local $Text::Wrap::columns = 60; | ||
| 211 | |||
| 212 | my @full_data; | ||
| 213 | for my $category (@$order) { | ||
| 214 | my $sub_commands_within_this_category = $categories->{$category}; | ||
| 215 | my @data = map { | ||
| 216 | my @rows = split("\n",Text::Wrap::wrap('', ' ', $_->help_brief)); | ||
| 217 | chomp @rows; | ||
| 218 | ( | ||
| 219 | [ | ||
| 220 | $_->$command_name_method, | ||
| 221 | ($_->isa('Command::Tree') ? '...' : ''), #$_->_shell_args_usage_string_abbreviated, | ||
| 222 | $rows[0], | ||
| 223 | ], | ||
| 224 | map { | ||
| 225 | [ | ||
| 226 | '', | ||
| 227 | ' ', | ||
| 228 | $rows[$_], | ||
| 229 | ] | ||
| 230 | } (1..$#rows) | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | @$sub_commands_within_this_category; | ||
| 234 | |||
| 235 | if ($category) { | ||
| 236 | # add a space between categories | ||
| 237 | push @full_data, ['','',''] if @full_data; | ||
| 238 | |||
| 239 | if ($category =~ /\D/) { | ||
| 240 | # non-numeric categories show their category as a header | ||
| 241 | $category .= ':' if $category =~ /\S/; | ||
| 242 | push @full_data, | ||
| 243 | [ | ||
| 244 | Term::ANSIColor::colored(uc($category), 'blue'), | ||
| 245 | '', | ||
| 246 | '' | ||
| 247 | ]; | ||
| 248 | |||
| 249 | } | ||
| 250 | else { | ||
| 251 | # numeric categories just sort | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | push @full_data, @data; | ||
| 256 | } | ||
| 257 | |||
| 258 | my @max_width_found = (0,0,0); | ||
| 259 | for (@full_data) { | ||
| 260 | for my $c (0..2) { | ||
| 261 | $max_width_found[$c] = length($_->[$c]) if $max_width_found[$c] < length($_->[$c]); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | my @colors = (qw/ red bold /); | ||
| 266 | my $text = ''; | ||
| 267 | for my $row (@full_data) { | ||
| 268 | for my $c (0..2) { | ||
| 269 | $text .= ' '; | ||
| 270 | $text .= $colors[$c] ? Term::ANSIColor::colored($row->[$c], $colors[$c]) : $row->[$c]; | ||
| 271 | $text .= ' '; | ||
| 272 | $text .= ' ' x ($max_width_found[$c]-length($row->[$c])); | ||
| 273 | } | ||
| 274 | $text .= "\n"; | ||
| 275 | } | ||
| 276 | return $text; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub doc_sub_commands { | ||
| 280 | my $self = shift; | ||
| 281 | my ($order, $categories) = $self->_categorize_sub_commands(@_); | ||
| 282 | my $text = ""; | ||
| 283 | my $indent_lvl = 4; | ||
| 284 | for my $category (@$order) { | ||
| 285 | my $category_name = ($category ? uc $category : "GENERAL"); | ||
| 286 | $text .= "=head2 $category_name\n\n"; | ||
| 287 | for my $cmd (@{$categories->{$category}}) { | ||
| 288 | $text .= "=over $indent_lvl\n\n"; | ||
| 289 | my $name = $cmd->command_name_brief; | ||
| 290 | my $link = $cmd->command_name; | ||
| 291 | $link =~ s/ /-/g; | ||
| 292 | my $description = $cmd->help_brief; | ||
| 293 | $text .= "=item B<L<$name|$link>>\n\n=over 2\n\n=item $description\n\n=back\n\n"; | ||
| 294 | $text .= "=back\n\nE<10>\n\n"; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | return $text; | ||
| 299 | } | ||
| 300 | |||
| 301 | # | ||
| 302 | # The following methods build allow a command to determine its | ||
| 303 | # sub-commands, if there are any. | ||
| 304 | # | ||
| 305 | |||
| 306 | # This is for cases in which the Foo::Bar command delegates to | ||
| 307 | # Foo::Bar::Baz, Foo::Bar::Buz or Foo::Bar::Doh, depending on its paramters. | ||
| 308 | |||
| 309 | sub sub_command_dirs { | ||
| 310 | my $class = shift; | ||
| 311 | my $subdir = ref($class) || $class; | ||
| 312 | $subdir =~ s|::|\/|g; | ||
| 313 | my @dirs = grep { -d $_ } map { $_ . '/' . $subdir } @INC; | ||
| 314 | return @dirs; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub sub_command_classes { | ||
| 318 | my $class = shift; | ||
| 319 | my $mapping = $class->_build_sub_command_mapping; | ||
| 320 | return values %$mapping; | ||
| 321 | } | ||
| 322 | |||
| 323 | # For compatability with Command::V1-based callers | ||
| 324 | sub is_sub_command_delegator { | ||
| 325 | return scalar(shift->sub_command_classes); | ||
| 326 | } | ||
| 327 | |||
| 328 | sub command_tree_source_classes { | ||
| 329 | # override in subclass if you want different sources | ||
| 330 | my $class = shift; | ||
| 331 | return $class; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub _build_sub_command_mapping { | ||
| 335 | my $class = shift; | ||
| 336 | $class = ref($class) || $class; | ||
| 337 | |||
| 338 | my @source_classes = $class->command_tree_source_classes; | ||
| 339 | |||
| 340 | my $mapping; | ||
| 341 | do { | ||
| 342 | no strict 'refs'; | ||
| 343 | $mapping = ${ $class . '::SUB_COMMAND_MAPPING'}; | ||
| 344 | if (ref($mapping) eq 'HASH') { | ||
| 345 | return $mapping; | ||
| 346 | } | ||
| 347 | }; | ||
| 348 | |||
| 349 | for my $source_class (@source_classes) { | ||
| 350 | # check if this class is valid | ||
| 351 | eval{ $source_class->class; }; | ||
| 352 | if ( $@ ) { | ||
| 353 | warn $@; | ||
| 354 | } | ||
| 355 | |||
| 356 | # for My::Foo::Command::* commands and sub-trees | ||
| 357 | my $subdir = $source_class; | ||
| 358 | $subdir =~ s|::|\/|g; | ||
| 359 | |||
| 360 | # for My::Foo::*::Command sub-trees | ||
| 361 | my $source_class_above = $source_class; | ||
| 362 | $source_class_above =~ s/::Command//; | ||
| 363 | my $subdir2 = $source_class_above; | ||
| 364 | $subdir2 =~ s|::|/|g; | ||
| 365 | |||
| 366 | # check everywhere | ||
| 367 | for my $lib (@INC) { | ||
| 368 | my $subdir_full_path = $lib . '/' . $subdir; | ||
| 369 | |||
| 370 | # find My::Foo::Command::* | ||
| 371 | if (-d $subdir_full_path) { | ||
| 372 | my @files = glob($subdir_full_path . '/*'); | ||
| 373 | for my $file (@files) { | ||
| 374 | my $basename = basename($file); | ||
| 375 | $basename =~ s/.pm$// or next; | ||
| 376 | my $sub_command_class_name = $source_class . '::' . $basename; | ||
| 377 | my $sub_command_class_meta = UR::Object::Type->get($sub_command_class_name); | ||
| 378 | unless ($sub_command_class_meta) { | ||
| 379 | local $SIG{__DIE__}; | ||
| 380 | local $SIG{__WARN__}; | ||
| 381 | # until _use_safe is refactored to be permissive, use directly... | ||
| 382 | print ">> $sub_command_class_name\n"; | ||
| 383 | eval "use $sub_command_class_name"; | ||
| 384 | } | ||
| 385 | $sub_command_class_meta = UR::Object::Type->get($sub_command_class_name); | ||
| 386 | next unless $sub_command_class_name->isa("Command"); | ||
| 387 | next if $sub_command_class_meta->is_abstract; | ||
| 388 | next if $sub_command_class_name eq $class; | ||
| 389 | my $name = $source_class->_command_name_for_class_word($basename); | ||
| 390 | $mapping->{$name} = $sub_command_class_name; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | # find My::Foo::*::Command | ||
| 395 | $subdir_full_path = $lib . '/' . $subdir2; | ||
| 396 | my $pattern = $subdir_full_path . '/*/Command.pm'; | ||
| 397 | my @paths = glob($pattern); | ||
| 398 | for my $file (@paths) { | ||
| 399 | next unless defined $file; | ||
| 400 | next unless length $file; | ||
| 401 | next unless -f $file; | ||
| 402 | my $last_word = File::Basename::basename($file); | ||
| 403 | $last_word =~ s/.pm$// or next; | ||
| 404 | my $dir = File::Basename::dirname($file); | ||
| 405 | my $second_to_last_word = File::Basename::basename($dir); | ||
| 406 | my $sub_command_class_name = $source_class_above . '::' . $second_to_last_word . '::' . $last_word; | ||
| 407 | next unless $sub_command_class_name->isa('Command'); | ||
| 408 | next if $sub_command_class_name->__meta__->is_abstract; | ||
| 409 | next if $sub_command_class_name eq $class; | ||
| 410 | my $basename = $second_to_last_word; | ||
| 411 | $basename =~ s/.pm$//; | ||
| 412 | my $name = $source_class->_command_name_for_class_word($basename); | ||
| 413 | $mapping->{$name} = $sub_command_class_name; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | return $mapping; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub sub_command_names { | ||
| 421 | my $class = shift; | ||
| 422 | my $mapping = $class->_build_sub_command_mapping; | ||
| 423 | return keys %$mapping; | ||
| 424 | } | ||
| 425 | |||
| 426 | |||
| 427 | |||
| 428 | sub _try_command_class_named { | ||
| 429 | my $self = shift; | ||
| 430 | |||
| 431 | my $sub_class = join('::', @_); | ||
| 432 | |||
| 433 | my $meta = UR::Object::Type->get($sub_class); # allow in memory classes | ||
| 434 | unless ( $meta ) { | ||
| 435 | eval "use $sub_class;"; | ||
| 436 | if ($@) { | ||
| 437 | if ($@ =~ /^Can't locate .*\.pm in \@INC/) { | ||
| 438 | #die "Failed to find $sub_class! $class_for_sub_command.pm!\n$@"; | ||
| 439 | return; | ||
| 440 | } | ||
| 441 | else { | ||
| 442 | my @msg = split("\n",$@); | ||
| 443 | pop @msg; | ||
| 444 | pop @msg; | ||
| 445 | $self->error_message("$sub_class failed to compile!:\n@msg\n\n"); | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | } | ||
| 450 | elsif (my $isa = $sub_class->isa("Command")) { | ||
| 451 | if (ref($isa)) { | ||
| 452 | # dumb modules (Test::Class) mess with the standard isa() API | ||
| 453 | if ($sub_class->SUPER::isa("Command")) { | ||
| 454 | return $sub_class; | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | return; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | return $sub_class; | ||
| 461 | } | ||
| 462 | else { | ||
| 463 | return; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 468 | sub class_for_sub_command { | ||
| 469 | my $self = shift; | ||
| 470 | my $class = ref($self) || $self; | ||
| 471 | my $sub_command = shift; | ||
| 472 | |||
| 473 | return if $sub_command =~ /^\-/; # If it starts with a "-", then it's a command-line option | ||
| 474 | |||
| 475 | # First attempt is to convert $sub_command into a camel-case module name | ||
| 476 | # and just try loading it | ||
| 477 | |||
| 478 | my $name_for_sub_command = join("", map { ucfirst($_) } split(/-/, $sub_command)); | ||
| 479 | my @class_name_parts = (split(/::/,$class), $name_for_sub_command); | ||
| 480 | my $sub_command_class = $self->_try_command_class_named(@class_name_parts); | ||
| 481 | return $sub_command_class if $sub_command_class; | ||
| 482 | |||
| 483 | # Remove "Command" if it's embedded in the middle and try inserting it in other places, starting at the end | ||
| 484 | @class_name_parts = ( ( map { $_ eq 'Command' ? () : $_ } @class_name_parts) , 'Command'); | ||
| 485 | for(my $i = $#class_name_parts; $i > 0; $i--) { | ||
| 486 | $sub_command_class = $self->_try_command_class_named(@class_name_parts); | ||
| 487 | return $sub_command_class if $sub_command_class; | ||
| 488 | $class_name_parts[$i] = $class_name_parts[$i-1]; | ||
| 489 | $class_name_parts[$i-1] = 'Command'; | ||
| 490 | } | ||
| 491 | |||
| 492 | # Didn't find it yet. Try exhaustively loading all the command modules under $class | ||
| 493 | my $mapping = $class->_build_sub_command_mapping; | ||
| 494 | if (my $sub_command_class = $mapping->{$sub_command}) { | ||
| 495 | return $sub_command_class; | ||
| 496 | } else { | ||
| 497 | return; | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | my $depth = 0; | ||
| 502 | sub __extend_namespace__ { | ||
| 503 | my ($self,$ext) = @_; | ||
| 504 | |||
| 505 | my $meta = $self->SUPER::__extend_namespace__($ext); | ||
| 506 | return $meta if $meta; | ||
| 507 | |||
| 508 | $depth++; | ||
| 509 | if ($depth>1) { | ||
| 510 | $depth--; | ||
| 511 | return; | ||
| 512 | } | ||
| 513 | |||
| 514 | my $class = Command::Tree::class_for_sub_command((ref $self || $self), $self->_command_name_for_class_word($ext)); | ||
| 515 | return $class->__meta__ if $class; | ||
| 516 | return; | ||
| 517 | } | ||
| 518 | |||
| 519 | 1; | ||
| 520 | |||
| 521 | __END__ |