| 1 | #!/usr/bin/env perl | ||
| 2 | package MOP4Import::Util::Inspector; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | print STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2 | ||
| 11 | } | ||
| 12 | |||
| 13 | use MOP4Import::Base::CLI_JSON -as_base | ||
| 14 | , [fields => | ||
| 15 | [lib => | ||
| 16 | doc => "library directory list. SCALAR, ARRAY or ':' separated STRING", | ||
| 17 | zsh_completer => ": :_directories", | ||
| 18 | ] | ||
| 19 | ]; | ||
| 20 | |||
| 21 | use MOP4Import::NamedCodeAttributes (); | ||
| 22 | |||
| 23 | use MOP4Import::Util qw/terse_dump fields_hash fields_symbol | ||
| 24 | maybe_fields_hash | ||
| 25 | take_hash_opts_maybe/; | ||
| 26 | use MOP4Import::Util::FindMethods; | ||
| 27 | |||
| 28 | use List::Util (); | ||
| 29 | |||
| 30 | sub describe_commands_of { | ||
| 31 | (my MY $self, my $classOrObj) = @_; | ||
| 32 | my $class = ref $classOrObj || $self->require_module($classOrObj); | ||
| 33 | |||
| 34 | map {$self->format_command_of($class, $_)} $self->list_commands_of($class); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub describe_options_of { | ||
| 38 | (my MY $self, my $classOrObj) = @_; | ||
| 39 | my $class = ref $classOrObj || $self->require_module($classOrObj); | ||
| 40 | |||
| 41 | my @msg; | ||
| 42 | |||
| 43 | my @options = $self->group_options_of($class); | ||
| 44 | my $maxlen = $self->max_option_length($class); | ||
| 45 | |||
| 46 | foreach my $group (@options) { | ||
| 47 | my ($pkg, @fields) = @$group; | ||
| 48 | push @msg, <<END; | ||
| 49 | |||
| 50 | Options from $pkg: | ||
| 51 | END | ||
| 52 | foreach my FieldSpec $fs (@fields) { | ||
| 53 | push @msg, $self->format_option($fs, $maxlen); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | @msg; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub list_commands_of { | ||
| 60 | my ($self, $pack) = @_; | ||
| 61 | $self->require_module($pack); | ||
| 62 | FindMethods($pack, sub {s/^cmd_//}); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub format_command_of { | ||
| 66 | my ($self, $class, $name) = @_; | ||
| 67 | " ".join(" " | ||
| 68 | , $name | ||
| 69 | , $self->info_command_doc_of($class, $name) // '')."\n"; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub list_options_of { | ||
| 73 | my ($self, $pack) = @_; | ||
| 74 | $self->require_module($pack); | ||
| 75 | my $symbol = fields_symbol($pack); | ||
| 76 | if (my $array = *{$symbol}{ARRAY}) { | ||
| 77 | List::Util::uniq(@$array, $self->list_options_onconfigure_of($pack)) | ||
| 78 | } | ||
| 79 | elsif (my $hash = *{$symbol}{HASH}) { | ||
| 80 | sort keys %$hash; | ||
| 81 | } | ||
| 82 | elsif ($pack->can("meta")) { | ||
| 83 | $pack->meta->get_attribute_list | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | (); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub list_options_onconfigure_of { | ||
| 91 | my ($self, $pack) = @_; | ||
| 92 | $self->require_module($pack); | ||
| 93 | my $isa = mro::get_linear_isa($pack); | ||
| 94 | my %dup; | ||
| 95 | map { | ||
| 96 | my $symtab = MOP4Import::Util::symtab($_); | ||
| 97 | map { | ||
| 98 | if (/^onconfigure_(\w+)$/ and not $dup{$1}++) { | ||
| 99 | $1 | ||
| 100 | } else { | ||
| 101 | () | ||
| 102 | } | ||
| 103 | } sort keys %$symtab; | ||
| 104 | } @$isa; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub group_options_of { | ||
| 108 | my ($self, $pack, @opt_names) = @_; | ||
| 109 | $self->require_module($pack); | ||
| 110 | my $meta = $pack->can("meta") ? $pack->meta : undef; | ||
| 111 | @opt_names = $self->list_options_of($pack) unless @opt_names; | ||
| 112 | my %package; | ||
| 113 | my @unknown; | ||
| 114 | foreach my $name (@opt_names) { | ||
| 115 | next unless $name =~ /^[a-z]/; | ||
| 116 | # FieldSpec もどきをここで作る | ||
| 117 | my FieldSpec $spec = $self->get_field_spec_of($pack, $name) or do { | ||
| 118 | push @unknown, $name; | ||
| 119 | next | ||
| 120 | }; | ||
| 121 | push @{$package{$spec->{package}}}, $spec; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $isa = mro::get_linear_isa($pack); | ||
| 125 | |||
| 126 | my @grouped = map { | ||
| 127 | $package{$_} ? [$_, @{$package{$_}}] : (); | ||
| 128 | } @$isa; | ||
| 129 | |||
| 130 | ((@unknown ? ['', @unknown] : ()), @grouped); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub get_field_spec_of { | ||
| 134 | (my MY $self, my ($pack, $name)) = @_; | ||
| 135 | $self->require_module($pack); | ||
| 136 | if (my $fields = maybe_fields_hash($pack)) { | ||
| 137 | my $spec = $fields->{$name}; | ||
| 138 | return $spec if ref $spec; | ||
| 139 | $self->get_field_spec_for_onconfigure($pack, $name) | ||
| 140 | } elsif (my $sub = $pack->can("meta")) { | ||
| 141 | my $attMeta = $sub->($pack)->get_attribute($name); | ||
| 142 | my FieldSpec $spec = +{}; | ||
| 143 | $spec->{package} = $pack; | ||
| 144 | $spec->{name} = $name; | ||
| 145 | if (my $doc = $attMeta->{documentation}) { | ||
| 146 | $spec->{doc} = $doc; | ||
| 147 | } | ||
| 148 | elsif ($doc = $attMeta->{isa}) { | ||
| 149 | $spec->{doc} = "isa $doc"; | ||
| 150 | } | ||
| 151 | $spec; | ||
| 152 | } else { | ||
| 153 | undef; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub get_field_spec_for_onconfigure { | ||
| 158 | (my MY $self, my ($pack, $name)) = @_; | ||
| 159 | my $code = $pack->can("onconfigure_$name") | ||
| 160 | or return; | ||
| 161 | |||
| 162 | if (my $atts = MOP4Import::NamedCodeAttributes->m4i_CODE_ATTR_dict($code)) { | ||
| 163 | $self->create_field_spec_from_code_attribute($pack, $name, $atts); | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | +{package => $pack, name => $name}; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub create_field_spec_from_code_attribute { | ||
| 171 | (my MY $self, my ($pack, $name, $atts)) = @_; | ||
| 172 | my FieldSpec $spec = +{}; | ||
| 173 | $spec->{package} = $pack; | ||
| 174 | $spec->{name} = $name; | ||
| 175 | if ($atts->{Doc}) { | ||
| 176 | $spec->{doc} = $atts->{Doc}; | ||
| 177 | } | ||
| 178 | if ($atts->{ZshCompleter}) { | ||
| 179 | $spec->{zsh_completer} = $atts->{ZshCompleter}; | ||
| 180 | } | ||
| 181 | $spec; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub max_option_length { | ||
| 185 | my ($self, $pack) = @_; | ||
| 186 | my $fields = fields_hash($pack); | ||
| 187 | my @name = grep {/^[a-z]/} $self->list_options_of($pack); | ||
| 188 | List::Util::max(map {length} @name); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub format_option { | ||
| 192 | (my MY $self, my FieldSpec $fs, my $maxlen) = @_; | ||
| 193 | my $len = ($maxlen // 16); | ||
| 194 | sprintf " --%-${len}s %s\n", $fs->{name}, $fs->{doc} // ""; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub is_getter_of { | ||
| 198 | my ($self, $pack, $subName) = @_; | ||
| 199 | my $fields = fields_hash($pack); | ||
| 200 | exists $fields->{$subName} && $pack->can($subName); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub info_command_doc_of { | ||
| 204 | my ($self, $class, $name) = @_; | ||
| 205 | $self->info_method_doc_of($class, "cmd_$name"); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub info_method_doc_of { | ||
| 209 | my ($self, $class, $name, $allow_missing) = @_; | ||
| 210 | |||
| 211 | my $atts = $self->info_code_attributes_of($class, $name, $allow_missing) | ||
| 212 | or return undef; | ||
| 213 | |||
| 214 | $atts->{Doc}; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub info_code_attribute { | ||
| 218 | my ($self, $attName, $code) = @_; | ||
| 219 | my $atts = MOP4Import::NamedCodeAttributes->m4i_CODE_ATTR_dict($code) | ||
| 220 | or return undef; | ||
| 221 | $atts->{$attName}; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub info_code_attributes_of { | ||
| 225 | my ($self, $class, $name, $allow_missing) = @_; | ||
| 226 | unless (defined $class and defined $name) { | ||
| 227 | Carp::croak "Usage: \$self->info_code_attributes_of(\$class, \$name, ?\$allow_missing\?)" | ||
| 228 | } | ||
| 229 | $self->require_module($class); | ||
| 230 | if ($class->can('meta')) { | ||
| 231 | my $meta = $class->meta->get_method($name) or do { | ||
| 232 | return if $allow_missing; | ||
| 233 | Carp::croak "No such method: $name"; | ||
| 234 | }; | ||
| 235 | $meta->can('attributes') | ||
| 236 | or return; | ||
| 237 | my $atts = $meta->attributes; | ||
| 238 | MOP4Import::NamedCodeAttributes->m4i_parse_attributes(@$atts); | ||
| 239 | } else { | ||
| 240 | my $sub = $class->can($name) or do { | ||
| 241 | return if $allow_missing; | ||
| 242 | Carp::croak "No such method: $name"; | ||
| 243 | }; | ||
| 244 | MOP4Import::NamedCodeAttributes->m4i_CODE_ATTR_dict($sub); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | sub info_methods :method { | ||
| 249 | (my MY $self, my ($methodPattern, %opts)) = @_; | ||
| 250 | |||
| 251 | my $groupByClass = delete $opts{group}; | ||
| 252 | my $detail = delete $opts{detail}; | ||
| 253 | my $all = delete $opts{all}; | ||
| 254 | my $inc = delete $opts{inc}; | ||
| 255 | my $pack = do { | ||
| 256 | if (my $name = delete $opts{pack}) { | ||
| 257 | $self->require_module($name, MOP4Import::Util::lexpand($inc)); | ||
| 258 | } else { | ||
| 259 | ref $self; | ||
| 260 | } | ||
| 261 | }; | ||
| 262 | |||
| 263 | unless (keys %opts == 0) { | ||
| 264 | Carp::croak "Unknown options: ".join(", ", sort keys %opts); | ||
| 265 | } | ||
| 266 | |||
| 267 | my $re = do { | ||
| 268 | if (not defined $methodPattern or $methodPattern eq '') { | ||
| 269 | qr{^[a-z]\w+\z} | ||
| 270 | } | ||
| 271 | elsif (ref $methodPattern eq 'Regexp') { | ||
| 272 | $methodPattern | ||
| 273 | } | ||
| 274 | elsif ($methodPattern =~ m{^/(.*)/\z}) { | ||
| 275 | qr{$1} | ||
| 276 | } elsif ($methodPattern =~ /\*/) { | ||
| 277 | require Text::Glob; | ||
| 278 | Text::Glob::glob_to_regex($methodPattern); | ||
| 279 | } else { | ||
| 280 | qr{^$methodPattern}; | ||
| 281 | } | ||
| 282 | }; | ||
| 283 | |||
| 284 | my $isa = mro::get_linear_isa($pack); | ||
| 285 | |||
| 286 | my @all = map { | ||
| 287 | my $symtab = MOP4Import::Util::symtab($_); | ||
| 288 | my @names = grep {not /\W/ and $_ =~ $re} keys %$symtab; | ||
| 289 | my @found = grep { | ||
| 290 | my $entry = $symtab->{$_}; | ||
| 291 | if (ref \$entry eq 'GLOB' and my $code = *{$entry}{CODE}) { | ||
| 292 | $all || MOP4Import::Util::has_method_attr($code); | ||
| 293 | } | ||
| 294 | } @names; | ||
| 295 | if (not @found) { | ||
| 296 | () | ||
| 297 | } elsif ($detail) { | ||
| 298 | +{class => $_, methods => [map { | ||
| 299 | my $doc = $self->info_method_doc($_); | ||
| 300 | [$_, $doc ? $doc : ()]; | ||
| 301 | } sort @found]} | ||
| 302 | } elsif ($groupByClass) { | ||
| 303 | [$_, sort @found] | ||
| 304 | } else { | ||
| 305 | @found; | ||
| 306 | } | ||
| 307 | } @$isa; | ||
| 308 | |||
| 309 | if (not $detail and not $groupByClass) { | ||
| 310 | my %dup; | ||
| 311 | sort grep {not $dup{$_}++}@all; | ||
| 312 | } else { | ||
| 313 | @all; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | sub require_module { | ||
| 318 | (my MY $self, my ($moduleName, @inc)) = @_; | ||
| 319 | |||
| 320 | my $fn = Module::Runtime::module_notional_filename($moduleName); | ||
| 321 | |||
| 322 | # Use symtab test to avoid require_module() against already loaded modules. | ||
| 323 | # In modulino, cmd_help may be called during module loading. | ||
| 324 | # At that time, $INC{$moduleFileName} is not yet initialized. | ||
| 325 | # This leads to double-require. | ||
| 326 | if (MOP4Import::Util::maybe_symtab($moduleName)) { | ||
| 327 | return wantarray ? ($moduleName => $INC{$fn}) : $moduleName; | ||
| 328 | } | ||
| 329 | |||
| 330 | @inc = map {split /:/} MOP4Import::Util::lexpand($self->{lib}) | ||
| 331 | if not @inc and ref $self; | ||
| 332 | { | ||
| 333 | require Module::Runtime; | ||
| 334 | local @INC = (@inc, @INC); | ||
| 335 | Module::Runtime::require_module($moduleName); | ||
| 336 | } | ||
| 337 | wantarray ? ($moduleName => $INC{$fn}) : $moduleName; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub list_validator_in_module { | ||
| 341 | (my MY $self, my ($typeName, $moduleName, @rest)) = @_; | ||
| 342 | my $pack = $self->require_module($moduleName, @rest); | ||
| 343 | my $sub = $pack->can($typeName); | ||
| 344 | my $realType = $sub ? $sub->() : $typeName; | ||
| 345 | MOP4Import::Util::list_validator($realType); | ||
| 346 | } | ||
| 347 | |||
| 348 | sub list_module_path { | ||
| 349 | (my MY $self, my @modName) = @_; | ||
| 350 | my %modules; | ||
| 351 | foreach my $modName (@modName) { | ||
| 352 | (undef, my $path) = $self->require_module($modName); | ||
| 353 | $modules{$modName} = $path; | ||
| 354 | } | ||
| 355 | \%modules | ||
| 356 | } | ||
| 357 | |||
| 358 | sub list_module_version { | ||
| 359 | (my MY $self, my @modName) = @_; | ||
| 360 | my %modules; | ||
| 361 | foreach my $modName (@modName) { | ||
| 362 | $self->require_module($modName); | ||
| 363 | $modules{$modName} = $modName->VERSION; | ||
| 364 | } | ||
| 365 | \%modules | ||
| 366 | } | ||
| 367 | |||
| 368 | unless (caller) { | ||
| 369 | # To avoid redefinition wornings: | ||
| 370 | # cli_run -> cli_help -> cli_inspector -> require MOP4Import::Util::Inspector; | ||
| 371 | require File::Spec; | ||
| 372 | $INC{"MOP4Import/Util/Inspector.pm"} = File::Spec->rel2abs(__FILE__); | ||
| 373 | |||
| 374 | MY->cli_run(\@ARGV); | ||
| 375 | } | ||
| 376 | |||
| 377 | 1; |