| 1 | package CLI::Dispatch::Help; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw( CLI::Dispatch::Command ); | ||
| 6 | use Class::Unload; | ||
| 7 | use Class::Inspector; | ||
| 8 | use Encode; | ||
| 9 | use Pod::Simple::Text; | ||
| 10 | use Path::Tiny; | ||
| 11 | use String::CamelCase; | ||
| 12 | use Term::Encoding (); | ||
| 13 | use Try::Tiny; | ||
| 14 | |||
| 15 | my $term_encoding = eval { | ||
| 16 | find_encoding(Term::Encoding::get_encoding()) | ||
| 17 | } || 'utf8'; | ||
| 18 | |||
| 19 | |||
| 20 | sub options {qw( from|decode=s to|encode=s )} | ||
| 21 | |||
| 22 | sub extra_namespaces {} | ||
| 23 | |||
| 24 | sub run { | ||
| 25 | my ($self, @args) = @_; | ||
| 26 | |||
| 27 | my $text; | ||
| 28 | if ( @args ) { | ||
| 29 | $text = $self->extract_pod( @args ); | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | $text = $self->list_commands; | ||
| 33 | } | ||
| 34 | |||
| 35 | $self->output( $text ); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub output { | ||
| 39 | my ($self, $text, $no_print) = @_; | ||
| 40 | |||
| 41 | unless ( Encode::is_utf8( $text ) ) { | ||
| 42 | $text = decode( $self->option('from') || 'utf8', $text ) | ||
| 43 | } | ||
| 44 | $text = encode( $self->option('to') || $term_encoding, $text ); | ||
| 45 | |||
| 46 | print $text unless $no_print; | ||
| 47 | |||
| 48 | return $text; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub extract_pod { | ||
| 52 | my ($self, $command) = @_; | ||
| 53 | |||
| 54 | my $content = $self->_lookup( $command ); | ||
| 55 | |||
| 56 | unless ( $content ) { | ||
| 57 | $self->logger(1) unless $self->logger; | ||
| 58 | $self->log( warn => "$command is not found" ); | ||
| 59 | return $self->list_commands; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $pod = $self->_parse_pod($content); | ||
| 63 | |||
| 64 | return $self->extract_pod_body($pod); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub extract_pod_body { | ||
| 68 | my ($self, $pod) = @_; | ||
| 69 | |||
| 70 | # remove the first ("NAME") section as the command does not | ||
| 71 | # always belong to the same namespace as the dispatcher/script. | ||
| 72 | # (default CLI::Dispatch namespace may be confusing for end users) | ||
| 73 | $pod =~ s/^\S+\s+(.+?)\n(?=\S)//s; | ||
| 74 | |||
| 75 | return $pod; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub list_commands { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my @paths = map { s{::}{/}g; $_ } $self->_namespaces; | ||
| 82 | |||
| 83 | my %found; | ||
| 84 | my %classes; | ||
| 85 | my $maxlength = 0; | ||
| 86 | foreach my $inc ( @INC ) { | ||
| 87 | foreach my $path ( @paths ) { | ||
| 88 | my $dir = path( $inc, $path ); | ||
| 89 | next unless $dir->exists && $dir->is_dir; | ||
| 90 | my $iter = $dir->iterator({recurse => 1}); | ||
| 91 | while (my $file = $iter->()) { | ||
| 92 | next if $file->is_dir; | ||
| 93 | |||
| 94 | my $basename = $file->basename; | ||
| 95 | $basename =~ s/\.(?:pm|pod)$//; | ||
| 96 | |||
| 97 | next if defined $found{$basename}; | ||
| 98 | |||
| 99 | (my $class = $path) =~ s{/}{::}g; | ||
| 100 | $class .= '::'.$basename; | ||
| 101 | $classes{$class} = 1; | ||
| 102 | |||
| 103 | # ignore base class | ||
| 104 | next if $class eq 'CLI::Dispatch::Command'; | ||
| 105 | |||
| 106 | my $podfile = $file->parent->child($basename . '.pod'); | ||
| 107 | my $pmfile = $file->parent->child($basename . '.pm'); | ||
| 108 | |||
| 109 | # should always parse .pod file if it exists | ||
| 110 | my $pod = $self->_parse_pod($podfile->exists ? $podfile->slurp : $file->slurp); | ||
| 111 | |||
| 112 | $basename = $self->convert_command($basename); | ||
| 113 | |||
| 114 | $found{$basename} ||= $self->extract_brief_description($pod, $class); | ||
| 115 | |||
| 116 | # check availability | ||
| 117 | if ( $pmfile->exists ) { | ||
| 118 | my $loaded = Class::Inspector->loaded($class); | ||
| 119 | Class::Unload->unload($class) if $loaded; | ||
| 120 | my $error; | ||
| 121 | try { eval "require $class" or die } | ||
| 122 | catch { $error = $_ || 'Obscure error' }; | ||
| 123 | if ($error) { | ||
| 124 | if ($error =~ /^Can't locate /) { | ||
| 125 | # most probably this is a subcommand of some command | ||
| 126 | # (ie. in a wrong namespace) | ||
| 127 | delete $found{$basename}; | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $found{$basename} .= " [disabled: compile error]"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | elsif ( $class->can('check') ) { | ||
| 134 | try { $class->check } | ||
| 135 | catch { | ||
| 136 | $error = $_ || 'Obscure error'; | ||
| 137 | $error =~ s/\s+at .+? line \d+\.?\s*$//; | ||
| 138 | $found{$basename} .= " [disabled: $error]"; | ||
| 139 | }; | ||
| 140 | } | ||
| 141 | Class::Unload->unload($class) unless $loaded; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $len = length $basename; | ||
| 145 | $maxlength = $len if $maxlength < $len; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | my $text = ''; | ||
| 151 | my $format = "%-${maxlength}s - %s\n"; | ||
| 152 | foreach my $key ( sort keys %found ) { | ||
| 153 | $text .= sprintf($format, $key, $found{$key}); | ||
| 154 | } | ||
| 155 | return $text; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub convert_command { | ||
| 159 | my ($self, $command) = @_; | ||
| 160 | String::CamelCase::decamelize( $command ); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub extract_brief_description { | ||
| 164 | my ($self, $pod, $class) = @_; | ||
| 165 | |||
| 166 | # "NAME" header may be localized | ||
| 167 | my ($brief_desc) = $pod =~ /^\S+\s+$class\s+\-\s+(.+?)\n/s; | ||
| 168 | |||
| 169 | return $brief_desc || ''; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _parse_pod { | ||
| 173 | my ($self, $file) = @_; | ||
| 174 | |||
| 175 | my $parser = Pod::Simple::Text->new; | ||
| 176 | $parser->output_string( \my $pod ); | ||
| 177 | $parser->parse_string_document("$file"); | ||
| 178 | |||
| 179 | return $pod; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub _namespaces { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | my %seen; | ||
| 186 | return grep { !$seen{$_}++ } ( | ||
| 187 | $self->extra_namespaces, | ||
| 188 | @{ $self->option('_namespaces') || [] }, | ||
| 189 | 'CLI::Dispatch' | ||
| 190 | ); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _lookup { | ||
| 194 | my ($self, $command) = @_; | ||
| 195 | |||
| 196 | my @paths; | ||
| 197 | if ($command =~ s/^\+//) { | ||
| 198 | $command =~ s{::}{/}g; | ||
| 199 | @paths = $command; | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | @paths = map { s{::}{/}g; "$_/$command" } $self->_namespaces; | ||
| 203 | } | ||
| 204 | |||
| 205 | foreach my $inc ( @INC ) { | ||
| 206 | foreach my $path ( @paths ) { | ||
| 207 | foreach my $ext (qw( pod pm )) { | ||
| 208 | my $file = path( $inc, "$path.$ext" ); | ||
| 209 | return $file->slurp if $file->exists; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | # probably it's embedded in the caller... | ||
| 215 | my $ct = 0; | ||
| 216 | my %seen; | ||
| 217 | while (my @caller = caller($ct++)) { | ||
| 218 | next if $caller[0] =~ /^CLI::Dispatch(::.+)?$/; | ||
| 219 | next if $seen{$caller[0]}++; | ||
| 220 | my $content = path($caller[1])->slurp; | ||
| 221 | for my $path ( @paths ) { | ||
| 222 | (my $package = $path) =~ s{/}{::}g; | ||
| 223 | if ($content =~ /=head1\s+\S+\s+$package/s) { # hopefully NAME | ||
| 224 | return $content; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | __END__ |