| 1 | package Util::Medley::Exec; | ||
| 2 | $Util::Medley::Exec::VERSION = '0.049'; | ||
| 3 | use Modern::Perl; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use Kavorka '-all'; | ||
| 7 | use Data::Printer alias => 'pdump'; | ||
| 8 | use Util::Medley::Crypt; | ||
| 9 | use Util::Medley::Number; | ||
| 10 | use Util::Medley::Module::Overview; | ||
| 11 | use Util::Medley::YAML; | ||
| 12 | use Text::ASCIITable; | ||
| 13 | use Text::Table; | ||
| 14 | |||
| 15 | with | ||
| 16 | 'Util::Medley::Roles::Attributes::Logger', | ||
| 17 | 'Util::Medley::Roles::Attributes::String', | ||
| 18 | 'Util::Medley::Roles::Attributes::List'; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Util::Medley::Exec - proxy for cmdline to libs | ||
| 23 | |||
| 24 | =head1 VERSION | ||
| 25 | |||
| 26 | version 0.049 | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | method moduleOverview (Str :$moduleName!, | ||
| 31 | Bool :$showInheritedPrivateAttributes, | ||
| 32 | Bool :$showInheritedPrivateMethods, | ||
| 33 | Bool :$viewClean, | ||
| 34 | Bool :$merged, | ||
| 35 | Str :$hideModules = 'Moose::Object') { | ||
| 36 | |||
| 37 | my @hide; | ||
| 38 | if ($hideModules) { | ||
| 39 | foreach my $pkg ( split( /,/, $hideModules ) ) { | ||
| 40 | push @hide, $self->String->trim($pkg); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | local $SIG{__WARN__} = 'ignore'; | ||
| 45 | my $mo; | ||
| 46 | $mo = Util::Medley::Module::Overview->new( | ||
| 47 | moduleName => $moduleName, | ||
| 48 | hideModules => \@hide | ||
| 49 | ); | ||
| 50 | |||
| 51 | if ($viewClean) { | ||
| 52 | if ($merged) { | ||
| 53 | $self->_moduleOverviewCleanViewMerged( | ||
| 54 | moduleName => $moduleName, | ||
| 55 | moduleOverview => $mo, | ||
| 56 | showInheritedPrivateAttributes => | ||
| 57 | $showInheritedPrivateAttributes, | ||
| 58 | showInheritedPrivateMethods => $showInheritedPrivateMethods, | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $self->_moduleOverviewCleanView( | ||
| 63 | moduleName => $moduleName, | ||
| 64 | moduleOverview => $mo, | ||
| 65 | showInheritedPrivateAttributes => | ||
| 66 | $showInheritedPrivateAttributes, | ||
| 67 | showInheritedPrivateMethods => $showInheritedPrivateMethods, | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | if ($merged) { | ||
| 73 | $self->Logger->fatal("merged not yet implemented for default view"); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $self->_moduleOverviewDefaultView( | ||
| 77 | moduleName => $moduleName, | ||
| 78 | moduleOverview => $mo, | ||
| 79 | showInheritedPrivateAttributes => | ||
| 80 | $showInheritedPrivateAttributes, | ||
| 81 | showInheritedPrivateMethods => $showInheritedPrivateMethods, | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | method commify (Num :$val!) { | ||
| 88 | |||
| 89 | my $num = Util::Medley::Number->new; | ||
| 90 | say $num->commify($val); | ||
| 91 | } | ||
| 92 | |||
| 93 | method decommify (Str :$val!) { | ||
| 94 | |||
| 95 | my $num = Util::Medley::Number->new; | ||
| 96 | say $num->decommify($val); | ||
| 97 | } | ||
| 98 | |||
| 99 | method encryptStr (Str :$str!, | ||
| 100 | Str :$key) { | ||
| 101 | |||
| 102 | my %a; | ||
| 103 | $a{str} = $str; | ||
| 104 | $a{key} = $key if $key; | ||
| 105 | |||
| 106 | my $crypt = Util::Medley::Crypt->new; | ||
| 107 | say $crypt->encryptStr(%a); | ||
| 108 | } | ||
| 109 | |||
| 110 | method decryptStr (Str :$str!, | ||
| 111 | Str :$key) { | ||
| 112 | |||
| 113 | my %a; | ||
| 114 | $a{str} = $str; | ||
| 115 | $a{key} = $key if $key; | ||
| 116 | |||
| 117 | my $crypt = Util::Medley::Crypt->new; | ||
| 118 | say $crypt->decryptStr(%a); | ||
| 119 | } | ||
| 120 | |||
| 121 | method fileType (Str :$file!) { | ||
| 122 | |||
| 123 | my $util = Util::Medley::File->new; | ||
| 124 | say $util->fileType($file); | ||
| 125 | } | ||
| 126 | |||
| 127 | method yamlBeautifyFile (Str :$file!, | ||
| 128 | Int :$sortDepth = 0) { | ||
| 129 | |||
| 130 | my $yaml = Util::Medley::YAML->new; | ||
| 131 | $yaml->yamlBeautifyFile(path => $file, sortDepth => $sortDepth); | ||
| 132 | } | ||
| 133 | |||
| 134 | ############################################################### | ||
| 135 | |||
| 136 | method _printTextTable (Int :$indent = 0, | ||
| 137 | ArrayRef :$rows, | ||
| 138 | Str :$onEmpty = '[none]') { | ||
| 139 | |||
| 140 | my $indentPadding = ' ' x $indent; | ||
| 141 | my @lines; | ||
| 142 | |||
| 143 | foreach my $row (@$rows) { | ||
| 144 | |||
| 145 | my @line; | ||
| 146 | push @line, $indentPadding; #' ' x $indent if $indent; | ||
| 147 | |||
| 148 | if ( ref($row) eq 'ARRAY' ) { | ||
| 149 | push @line, @$row; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | push @line, $row; | ||
| 153 | } | ||
| 154 | |||
| 155 | push @lines, \@line; | ||
| 156 | } | ||
| 157 | |||
| 158 | if ( !@lines ) { | ||
| 159 | push @lines, [ $indentPadding, $onEmpty ]; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (@lines) { | ||
| 163 | my $t = Text::Table->new; | ||
| 164 | $t->load(@lines); | ||
| 165 | print $t; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | method _moduleOverviewCleanViewMerged ( | ||
| 170 | Str :$moduleName!, | ||
| 171 | Bool :$showInheritedPrivateAttributes, | ||
| 172 | Bool :$showInheritedPrivateMethods, | ||
| 173 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 174 | ) { | ||
| 175 | |||
| 176 | my $mo = $moduleOverview; | ||
| 177 | my $isMooseModule = $mo->isMooseModule; | ||
| 178 | |||
| 179 | my $header = "MODULE OVERVIEW"; | ||
| 180 | my $remWidth = 80 - length($header); | ||
| 181 | my $padding = ' ' x int( $remWidth / 2 ); | ||
| 182 | $header = sprintf '%s%s%s', $padding, $header, $padding; | ||
| 183 | |||
| 184 | my $t = Text::ASCIITable->new( { headingText => $header } ); | ||
| 185 | $t->setOptions( { hide_HeadLine => 1, hide_HeadRow => 1 } ); | ||
| 186 | $t->setCols('a'); # must set a column even though we block it | ||
| 187 | print $t; | ||
| 188 | |||
| 189 | say "Module Name:"; | ||
| 190 | $self->_printTextTable( indent => 4, rows => [$moduleName] ); | ||
| 191 | |||
| 192 | say ''; | ||
| 193 | say "Parents:"; | ||
| 194 | $self->_printTextTable( indent => 4, rows => [ $mo->getParents ] ); | ||
| 195 | |||
| 196 | say ''; | ||
| 197 | say "Uses:"; | ||
| 198 | $self->_printTextTable( indent => 4, rows => [ $mo->getImportedModules ] ); | ||
| 199 | |||
| 200 | say ''; | ||
| 201 | say "Constants:"; | ||
| 202 | $self->_printTextTable( indent => 4, rows => [ $mo->getConstants ] ); | ||
| 203 | |||
| 204 | if ($isMooseModule) { | ||
| 205 | |||
| 206 | say ''; | ||
| 207 | say "Attributes:"; | ||
| 208 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 209 | $self->_printTextTable( | ||
| 210 | indent => 8, | ||
| 211 | rows => [ $mo->getAllPublicAttributes ], | ||
| 212 | ); | ||
| 213 | |||
| 214 | say ''; | ||
| 215 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 216 | $self->_printTextTable( | ||
| 217 | indent => 8, | ||
| 218 | rows => [ $mo->getPrivateAttributes ] | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | |||
| 222 | say ''; | ||
| 223 | say "Methods:"; | ||
| 224 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 225 | $self->_printTextTable( | ||
| 226 | indent => 8, | ||
| 227 | rows => [ $mo->getAllPublicMethods ] | ||
| 228 | ); | ||
| 229 | |||
| 230 | say ''; | ||
| 231 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 232 | $self->_printTextTable( | ||
| 233 | indent => 8, | ||
| 234 | rows => [ $mo->getPrivateMethods ] | ||
| 235 | ); | ||
| 236 | } | ||
| 237 | |||
| 238 | method _moduleOverviewCleanView ( | ||
| 239 | Str :$moduleName!, | ||
| 240 | Bool :$showInheritedPrivateAttributes, | ||
| 241 | Bool :$showInheritedPrivateMethods, | ||
| 242 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 243 | ) { | ||
| 244 | |||
| 245 | my $mo = $moduleOverview; | ||
| 246 | my $isMooseModule = $mo->isMooseModule; | ||
| 247 | |||
| 248 | my $header = "MODULE OVERVIEW"; | ||
| 249 | my $remWidth = 80 - length($header); | ||
| 250 | my $padding = ' ' x int( $remWidth / 2 ); | ||
| 251 | $header = sprintf '%s%s%s', $padding, $header, $padding; | ||
| 252 | |||
| 253 | my $t = Text::ASCIITable->new( { headingText => $header } ); | ||
| 254 | $t->setOptions( { hide_HeadLine => 1, hide_HeadRow => 1 } ); | ||
| 255 | $t->setCols('a'); # must set a column even though we block it | ||
| 256 | print $t; | ||
| 257 | |||
| 258 | say "Module Name:"; | ||
| 259 | $self->_printTextTable( indent => 4, rows => [$moduleName] ); | ||
| 260 | |||
| 261 | say ''; | ||
| 262 | say "Parents:"; | ||
| 263 | $self->_printTextTable( indent => 4, rows => [ $mo->getParents ] ); | ||
| 264 | |||
| 265 | say ''; | ||
| 266 | say "Uses:"; | ||
| 267 | $self->_printTextTable( indent => 4, rows => [ $mo->getImportedModules ] ); | ||
| 268 | |||
| 269 | say ''; | ||
| 270 | say "Constants:"; | ||
| 271 | $self->_printTextTable( indent => 4, rows => [ $mo->getConstants ] ); | ||
| 272 | |||
| 273 | if ($isMooseModule) { | ||
| 274 | |||
| 275 | say ''; | ||
| 276 | say "Attributes:"; | ||
| 277 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 278 | $self->_printTextTable( | ||
| 279 | indent => 8, | ||
| 280 | rows => [ $mo->getPublicAttributes ] | ||
| 281 | ); | ||
| 282 | |||
| 283 | say ''; | ||
| 284 | $self->_printTextTable( indent => 8, rows => ['inherited:'] ); | ||
| 285 | |||
| 286 | my %map; | ||
| 287 | foreach my $aref ( $mo->getInheritedPublicAttributes ) { | ||
| 288 | my ( $method, $from ) = @$aref; | ||
| 289 | push @{ $map{$from} }, $method; | ||
| 290 | } | ||
| 291 | |||
| 292 | my $first = 1; | ||
| 293 | foreach my $from ( $self->List->nsort( keys %map ) ) { | ||
| 294 | |||
| 295 | if ($first) { | ||
| 296 | $first = 0; | ||
| 297 | } | ||
| 298 | else { | ||
| 299 | say ''; | ||
| 300 | } | ||
| 301 | |||
| 302 | $self->_printTextTable( indent => 12, rows => [$from] ); | ||
| 303 | |||
| 304 | my @rows; | ||
| 305 | foreach my $method ( $self->List->nsort( @{ $map{$from} } ) ) { | ||
| 306 | push @rows, $method; | ||
| 307 | } | ||
| 308 | |||
| 309 | $self->_printTextTable( indent => 16, rows => \@rows ); | ||
| 310 | } | ||
| 311 | |||
| 312 | say ''; | ||
| 313 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 314 | $self->_printTextTable( | ||
| 315 | indent => 8, | ||
| 316 | rows => [ $mo->getPrivateAttributes ] | ||
| 317 | ); | ||
| 318 | } | ||
| 319 | |||
| 320 | say ''; | ||
| 321 | say "Methods:"; | ||
| 322 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 323 | $self->_printTextTable( | ||
| 324 | indent => 8, | ||
| 325 | rows => [ $mo->getPublicMethods ] | ||
| 326 | ); | ||
| 327 | |||
| 328 | say ''; | ||
| 329 | $self->_printTextTable( indent => 8, rows => ['inherited:'] ); | ||
| 330 | |||
| 331 | my %map; | ||
| 332 | foreach my $aref ( $mo->getInheritedPublicMethods ) { | ||
| 333 | my ( $method, $from ) = @$aref; | ||
| 334 | push @{ $map{$from} }, $method; | ||
| 335 | } | ||
| 336 | |||
| 337 | my $first = 1; | ||
| 338 | foreach my $from ( $self->List->nsort( keys %map ) ) { | ||
| 339 | |||
| 340 | if ($first) { | ||
| 341 | $first = 0; | ||
| 342 | } | ||
| 343 | else { | ||
| 344 | say ''; | ||
| 345 | } | ||
| 346 | |||
| 347 | $self->_printTextTable( indent => 12, rows => [$from] ); | ||
| 348 | |||
| 349 | my @rows; | ||
| 350 | foreach my $method ( $self->List->nsort( @{ $map{$from} } ) ) { | ||
| 351 | push @rows, $method; | ||
| 352 | } | ||
| 353 | |||
| 354 | $self->_printTextTable( indent => 16, rows => \@rows ); | ||
| 355 | } | ||
| 356 | |||
| 357 | say ''; | ||
| 358 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 359 | $self->_printTextTable( | ||
| 360 | indent => 8, | ||
| 361 | rows => [ $mo->getPrivateMethods ] | ||
| 362 | ); | ||
| 363 | } | ||
| 364 | |||
| 365 | method _moduleOverviewDefaultView ( | ||
| 366 | Str :$moduleName!, | ||
| 367 | Bool :$showInheritedPrivateAttributes, | ||
| 368 | Bool :$showInheritedPrivateMethods, | ||
| 369 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 370 | ) { | ||
| 371 | |||
| 372 | my $mo = $moduleOverview; | ||
| 373 | my $isMooseModule = $mo->isMooseModule; | ||
| 374 | |||
| 375 | my $t = Text::ASCIITable->new( { headingText => "\n$moduleName\n " } ); | ||
| 376 | $t->setOptions( { hide_HeadLine => 0, hide_HeadRow => 1 } ); | ||
| 377 | $t->setCols(qw(a b c d)); | ||
| 378 | |||
| 379 | $self->_moduleOverviewAddSection( | ||
| 380 | table => $t, | ||
| 381 | section => 'parents', | ||
| 382 | rows => [ $mo->getParents ] | ||
| 383 | ); | ||
| 384 | |||
| 385 | $t->addRowLine; | ||
| 386 | |||
| 387 | $self->_moduleOverviewAddSection( | ||
| 388 | table => $t, | ||
| 389 | section => 'uses', | ||
| 390 | rows => [ $mo->getImportedModules ], | ||
| 391 | ); | ||
| 392 | |||
| 393 | $t->addRowLine; | ||
| 394 | |||
| 395 | $self->_moduleOverviewAddSection( | ||
| 396 | table => $t, | ||
| 397 | section => 'constants', | ||
| 398 | rows => [ $mo->getConstants ], | ||
| 399 | ); | ||
| 400 | |||
| 401 | if ($isMooseModule) { | ||
| 402 | $t->addRowLine; | ||
| 403 | |||
| 404 | $self->_moduleOverviewAddSection( | ||
| 405 | table => $t, | ||
| 406 | section => 'attributes', | ||
| 407 | subsection => 'public', | ||
| 408 | rows => [ $mo->getPublicAttributes ] | ||
| 409 | ); | ||
| 410 | |||
| 411 | $t->addRow(''); | ||
| 412 | |||
| 413 | $self->_moduleOverviewAddSection( | ||
| 414 | table => $t, | ||
| 415 | section => '', | ||
| 416 | subsection => 'private', | ||
| 417 | rows => [ $mo->getPrivateAttributes ] | ||
| 418 | ); | ||
| 419 | } | ||
| 420 | |||
| 421 | $t->addRowLine; | ||
| 422 | |||
| 423 | $self->_moduleOverviewAddSection( | ||
| 424 | table => $t, | ||
| 425 | section => $isMooseModule ? 'methods' : 'subs', | ||
| 426 | subsection => 'public', | ||
| 427 | rows => [ $mo->getPublicMethods ] | ||
| 428 | ); | ||
| 429 | |||
| 430 | $t->addRow(''); | ||
| 431 | |||
| 432 | $self->_moduleOverviewAddSection( | ||
| 433 | table => $t, | ||
| 434 | section => '', | ||
| 435 | subsection => 'private', | ||
| 436 | rows => [ $mo->getPrivateMethods ] | ||
| 437 | ); | ||
| 438 | |||
| 439 | ########## inherited ########### | ||
| 440 | |||
| 441 | if ($isMooseModule) { | ||
| 442 | $t->addRowLine; | ||
| 443 | |||
| 444 | $self->_moduleOverviewAddSection( | ||
| 445 | table => $t, | ||
| 446 | section => 'inherited attributes', | ||
| 447 | subsection => 'public', | ||
| 448 | rows => [ $mo->getInheritedPublicAttributes ] | ||
| 449 | ); | ||
| 450 | |||
| 451 | if ($showInheritedPrivateAttributes) { | ||
| 452 | $t->addRow(''); | ||
| 453 | |||
| 454 | $self->_moduleOverviewAddSection( | ||
| 455 | table => $t, | ||
| 456 | section => '', | ||
| 457 | subsection => 'private', | ||
| 458 | rows => [ $mo->getInheritedPrivateAttributes ] | ||
| 459 | ); | ||
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | $t->addRowLine; | ||
| 464 | |||
| 465 | my $section; | ||
| 466 | if ($isMooseModule) { | ||
| 467 | $section = "inherited methods"; | ||
| 468 | } | ||
| 469 | else { | ||
| 470 | $section = "imported subs"; | ||
| 471 | } | ||
| 472 | |||
| 473 | $self->_moduleOverviewAddSection( | ||
| 474 | table => $t, | ||
| 475 | section => $section, | ||
| 476 | subsection => 'public', | ||
| 477 | rows => [ $mo->getInheritedPublicMethods ] | ||
| 478 | ); | ||
| 479 | |||
| 480 | if ($showInheritedPrivateMethods) { | ||
| 481 | $t->addRow(''); | ||
| 482 | |||
| 483 | $self->_moduleOverviewAddSection( | ||
| 484 | table => $t, | ||
| 485 | section => '', | ||
| 486 | subsection => 'private', | ||
| 487 | rows => [ $mo->getInheritedPrivateMethods ] | ||
| 488 | ); | ||
| 489 | } | ||
| 490 | |||
| 491 | print $t; | ||
| 492 | } | ||
| 493 | |||
| 494 | method _moduleOverviewAddSection (Object :$table!, | ||
| 495 | Str :$section, | ||
| 496 | Str :$subsection, | ||
| 497 | ArrayRef :$rows!) { | ||
| 498 | |||
| 499 | push @$rows, '' if !@$rows; | ||
| 500 | |||
| 501 | my $first = 1; | ||
| 502 | foreach my $row (@$rows) { | ||
| 503 | |||
| 504 | if ($first) { | ||
| 505 | $first = 0; | ||
| 506 | } | ||
| 507 | else { | ||
| 508 | $section = ''; | ||
| 509 | $subsection = ''; | ||
| 510 | } | ||
| 511 | |||
| 512 | if ( ref($row) eq 'ARRAY' ) { | ||
| 513 | $table->addRow( $section, $subsection, @$row ); | ||
| 514 | } | ||
| 515 | else { | ||
| 516 | $table->addRow( $section, $subsection, $row ); | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | 1; |