| 1 | package Util::Medley::Exec; | ||
| 2 | $Util::Medley::Exec::VERSION = '0.047'; | ||
| 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.047 | ||
| 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 yamlBeautifyFile (Str :$file!, | ||
| 122 | Int :$sortDepth = 0) { | ||
| 123 | |||
| 124 | my $yaml = Util::Medley::YAML->new; | ||
| 125 | $yaml->yamlBeautifyFile(path => $file, sortDepth => $sortDepth); | ||
| 126 | } | ||
| 127 | |||
| 128 | ############################################################### | ||
| 129 | |||
| 130 | method _printTextTable (Int :$indent = 0, | ||
| 131 | ArrayRef :$rows, | ||
| 132 | Str :$onEmpty = '[none]') { | ||
| 133 | |||
| 134 | my $indentPadding = ' ' x $indent; | ||
| 135 | my @lines; | ||
| 136 | |||
| 137 | foreach my $row (@$rows) { | ||
| 138 | |||
| 139 | my @line; | ||
| 140 | push @line, $indentPadding; #' ' x $indent if $indent; | ||
| 141 | |||
| 142 | if ( ref($row) eq 'ARRAY' ) { | ||
| 143 | push @line, @$row; | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | push @line, $row; | ||
| 147 | } | ||
| 148 | |||
| 149 | push @lines, \@line; | ||
| 150 | } | ||
| 151 | |||
| 152 | if ( !@lines ) { | ||
| 153 | push @lines, [ $indentPadding, $onEmpty ]; | ||
| 154 | } | ||
| 155 | |||
| 156 | if (@lines) { | ||
| 157 | my $t = Text::Table->new; | ||
| 158 | $t->load(@lines); | ||
| 159 | print $t; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | method _moduleOverviewCleanViewMerged ( | ||
| 164 | Str :$moduleName!, | ||
| 165 | Bool :$showInheritedPrivateAttributes, | ||
| 166 | Bool :$showInheritedPrivateMethods, | ||
| 167 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 168 | ) { | ||
| 169 | |||
| 170 | my $mo = $moduleOverview; | ||
| 171 | my $isMooseModule = $mo->isMooseModule; | ||
| 172 | |||
| 173 | my $header = "MODULE OVERVIEW"; | ||
| 174 | my $remWidth = 80 - length($header); | ||
| 175 | my $padding = ' ' x int( $remWidth / 2 ); | ||
| 176 | $header = sprintf '%s%s%s', $padding, $header, $padding; | ||
| 177 | |||
| 178 | my $t = Text::ASCIITable->new( { headingText => $header } ); | ||
| 179 | $t->setOptions( { hide_HeadLine => 1, hide_HeadRow => 1 } ); | ||
| 180 | $t->setCols('a'); # must set a column even though we block it | ||
| 181 | print $t; | ||
| 182 | |||
| 183 | say "Module Name:"; | ||
| 184 | $self->_printTextTable( indent => 4, rows => [$moduleName] ); | ||
| 185 | |||
| 186 | say ''; | ||
| 187 | say "Parents:"; | ||
| 188 | $self->_printTextTable( indent => 4, rows => [ $mo->getParents ] ); | ||
| 189 | |||
| 190 | say ''; | ||
| 191 | say "Uses:"; | ||
| 192 | $self->_printTextTable( indent => 4, rows => [ $mo->getImportedModules ] ); | ||
| 193 | |||
| 194 | say ''; | ||
| 195 | say "Constants:"; | ||
| 196 | $self->_printTextTable( indent => 4, rows => [ $mo->getConstants ] ); | ||
| 197 | |||
| 198 | if ($isMooseModule) { | ||
| 199 | |||
| 200 | say ''; | ||
| 201 | say "Attributes:"; | ||
| 202 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 203 | $self->_printTextTable( | ||
| 204 | indent => 8, | ||
| 205 | rows => [ $mo->getAllPublicAttributes ], | ||
| 206 | ); | ||
| 207 | |||
| 208 | say ''; | ||
| 209 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 210 | $self->_printTextTable( | ||
| 211 | indent => 8, | ||
| 212 | rows => [ $mo->getPrivateAttributes ] | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | |||
| 216 | say ''; | ||
| 217 | say "Methods:"; | ||
| 218 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 219 | $self->_printTextTable( | ||
| 220 | indent => 8, | ||
| 221 | rows => [ $mo->getAllPublicMethods ] | ||
| 222 | ); | ||
| 223 | |||
| 224 | say ''; | ||
| 225 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 226 | $self->_printTextTable( | ||
| 227 | indent => 8, | ||
| 228 | rows => [ $mo->getPrivateMethods ] | ||
| 229 | ); | ||
| 230 | } | ||
| 231 | |||
| 232 | method _moduleOverviewCleanView ( | ||
| 233 | Str :$moduleName!, | ||
| 234 | Bool :$showInheritedPrivateAttributes, | ||
| 235 | Bool :$showInheritedPrivateMethods, | ||
| 236 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 237 | ) { | ||
| 238 | |||
| 239 | my $mo = $moduleOverview; | ||
| 240 | my $isMooseModule = $mo->isMooseModule; | ||
| 241 | |||
| 242 | my $header = "MODULE OVERVIEW"; | ||
| 243 | my $remWidth = 80 - length($header); | ||
| 244 | my $padding = ' ' x int( $remWidth / 2 ); | ||
| 245 | $header = sprintf '%s%s%s', $padding, $header, $padding; | ||
| 246 | |||
| 247 | my $t = Text::ASCIITable->new( { headingText => $header } ); | ||
| 248 | $t->setOptions( { hide_HeadLine => 1, hide_HeadRow => 1 } ); | ||
| 249 | $t->setCols('a'); # must set a column even though we block it | ||
| 250 | print $t; | ||
| 251 | |||
| 252 | say "Module Name:"; | ||
| 253 | $self->_printTextTable( indent => 4, rows => [$moduleName] ); | ||
| 254 | |||
| 255 | say ''; | ||
| 256 | say "Parents:"; | ||
| 257 | $self->_printTextTable( indent => 4, rows => [ $mo->getParents ] ); | ||
| 258 | |||
| 259 | say ''; | ||
| 260 | say "Uses:"; | ||
| 261 | $self->_printTextTable( indent => 4, rows => [ $mo->getImportedModules ] ); | ||
| 262 | |||
| 263 | say ''; | ||
| 264 | say "Constants:"; | ||
| 265 | $self->_printTextTable( indent => 4, rows => [ $mo->getConstants ] ); | ||
| 266 | |||
| 267 | if ($isMooseModule) { | ||
| 268 | |||
| 269 | say ''; | ||
| 270 | say "Attributes:"; | ||
| 271 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 272 | $self->_printTextTable( | ||
| 273 | indent => 8, | ||
| 274 | rows => [ $mo->getPublicAttributes ] | ||
| 275 | ); | ||
| 276 | |||
| 277 | say ''; | ||
| 278 | $self->_printTextTable( indent => 8, rows => ['inherited:'] ); | ||
| 279 | |||
| 280 | my %map; | ||
| 281 | foreach my $aref ( $mo->getInheritedPublicAttributes ) { | ||
| 282 | my ( $method, $from ) = @$aref; | ||
| 283 | push @{ $map{$from} }, $method; | ||
| 284 | } | ||
| 285 | |||
| 286 | my $first = 1; | ||
| 287 | foreach my $from ( $self->List->nsort( keys %map ) ) { | ||
| 288 | |||
| 289 | if ($first) { | ||
| 290 | $first = 0; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | say ''; | ||
| 294 | } | ||
| 295 | |||
| 296 | $self->_printTextTable( indent => 12, rows => [$from] ); | ||
| 297 | |||
| 298 | my @rows; | ||
| 299 | foreach my $method ( $self->List->nsort( @{ $map{$from} } ) ) { | ||
| 300 | push @rows, $method; | ||
| 301 | } | ||
| 302 | |||
| 303 | $self->_printTextTable( indent => 16, rows => \@rows ); | ||
| 304 | } | ||
| 305 | |||
| 306 | say ''; | ||
| 307 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 308 | $self->_printTextTable( | ||
| 309 | indent => 8, | ||
| 310 | rows => [ $mo->getPrivateAttributes ] | ||
| 311 | ); | ||
| 312 | } | ||
| 313 | |||
| 314 | say ''; | ||
| 315 | say "Methods:"; | ||
| 316 | $self->_printTextTable( indent => 4, rows => ['public:'] ); | ||
| 317 | $self->_printTextTable( | ||
| 318 | indent => 8, | ||
| 319 | rows => [ $mo->getPublicMethods ] | ||
| 320 | ); | ||
| 321 | |||
| 322 | say ''; | ||
| 323 | $self->_printTextTable( indent => 8, rows => ['inherited:'] ); | ||
| 324 | |||
| 325 | my %map; | ||
| 326 | foreach my $aref ( $mo->getInheritedPublicMethods ) { | ||
| 327 | my ( $method, $from ) = @$aref; | ||
| 328 | push @{ $map{$from} }, $method; | ||
| 329 | } | ||
| 330 | |||
| 331 | my $first = 1; | ||
| 332 | foreach my $from ( $self->List->nsort( keys %map ) ) { | ||
| 333 | |||
| 334 | if ($first) { | ||
| 335 | $first = 0; | ||
| 336 | } | ||
| 337 | else { | ||
| 338 | say ''; | ||
| 339 | } | ||
| 340 | |||
| 341 | $self->_printTextTable( indent => 12, rows => [$from] ); | ||
| 342 | |||
| 343 | my @rows; | ||
| 344 | foreach my $method ( $self->List->nsort( @{ $map{$from} } ) ) { | ||
| 345 | push @rows, $method; | ||
| 346 | } | ||
| 347 | |||
| 348 | $self->_printTextTable( indent => 16, rows => \@rows ); | ||
| 349 | } | ||
| 350 | |||
| 351 | say ''; | ||
| 352 | $self->_printTextTable( indent => 4, rows => ['private:'] ); | ||
| 353 | $self->_printTextTable( | ||
| 354 | indent => 8, | ||
| 355 | rows => [ $mo->getPrivateMethods ] | ||
| 356 | ); | ||
| 357 | } | ||
| 358 | |||
| 359 | method _moduleOverviewDefaultView ( | ||
| 360 | Str :$moduleName!, | ||
| 361 | Bool :$showInheritedPrivateAttributes, | ||
| 362 | Bool :$showInheritedPrivateMethods, | ||
| 363 | Util::Medley::Module::Overview :$moduleOverview! | ||
| 364 | ) { | ||
| 365 | |||
| 366 | my $mo = $moduleOverview; | ||
| 367 | my $isMooseModule = $mo->isMooseModule; | ||
| 368 | |||
| 369 | my $t = Text::ASCIITable->new( { headingText => "\n$moduleName\n " } ); | ||
| 370 | $t->setOptions( { hide_HeadLine => 0, hide_HeadRow => 1 } ); | ||
| 371 | $t->setCols(qw(a b c d)); | ||
| 372 | |||
| 373 | $self->_moduleOverviewAddSection( | ||
| 374 | table => $t, | ||
| 375 | section => 'parents', | ||
| 376 | rows => [ $mo->getParents ] | ||
| 377 | ); | ||
| 378 | |||
| 379 | $t->addRowLine; | ||
| 380 | |||
| 381 | $self->_moduleOverviewAddSection( | ||
| 382 | table => $t, | ||
| 383 | section => 'uses', | ||
| 384 | rows => [ $mo->getImportedModules ], | ||
| 385 | ); | ||
| 386 | |||
| 387 | $t->addRowLine; | ||
| 388 | |||
| 389 | $self->_moduleOverviewAddSection( | ||
| 390 | table => $t, | ||
| 391 | section => 'constants', | ||
| 392 | rows => [ $mo->getConstants ], | ||
| 393 | ); | ||
| 394 | |||
| 395 | if ($isMooseModule) { | ||
| 396 | $t->addRowLine; | ||
| 397 | |||
| 398 | $self->_moduleOverviewAddSection( | ||
| 399 | table => $t, | ||
| 400 | section => 'attributes', | ||
| 401 | subsection => 'public', | ||
| 402 | rows => [ $mo->getPublicAttributes ] | ||
| 403 | ); | ||
| 404 | |||
| 405 | $t->addRow(''); | ||
| 406 | |||
| 407 | $self->_moduleOverviewAddSection( | ||
| 408 | table => $t, | ||
| 409 | section => '', | ||
| 410 | subsection => 'private', | ||
| 411 | rows => [ $mo->getPrivateAttributes ] | ||
| 412 | ); | ||
| 413 | } | ||
| 414 | |||
| 415 | $t->addRowLine; | ||
| 416 | |||
| 417 | $self->_moduleOverviewAddSection( | ||
| 418 | table => $t, | ||
| 419 | section => $isMooseModule ? 'methods' : 'subs', | ||
| 420 | subsection => 'public', | ||
| 421 | rows => [ $mo->getPublicMethods ] | ||
| 422 | ); | ||
| 423 | |||
| 424 | $t->addRow(''); | ||
| 425 | |||
| 426 | $self->_moduleOverviewAddSection( | ||
| 427 | table => $t, | ||
| 428 | section => '', | ||
| 429 | subsection => 'private', | ||
| 430 | rows => [ $mo->getPrivateMethods ] | ||
| 431 | ); | ||
| 432 | |||
| 433 | ########## inherited ########### | ||
| 434 | |||
| 435 | if ($isMooseModule) { | ||
| 436 | $t->addRowLine; | ||
| 437 | |||
| 438 | $self->_moduleOverviewAddSection( | ||
| 439 | table => $t, | ||
| 440 | section => 'inherited attributes', | ||
| 441 | subsection => 'public', | ||
| 442 | rows => [ $mo->getInheritedPublicAttributes ] | ||
| 443 | ); | ||
| 444 | |||
| 445 | if ($showInheritedPrivateAttributes) { | ||
| 446 | $t->addRow(''); | ||
| 447 | |||
| 448 | $self->_moduleOverviewAddSection( | ||
| 449 | table => $t, | ||
| 450 | section => '', | ||
| 451 | subsection => 'private', | ||
| 452 | rows => [ $mo->getInheritedPrivateAttributes ] | ||
| 453 | ); | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | $t->addRowLine; | ||
| 458 | |||
| 459 | my $section; | ||
| 460 | if ($isMooseModule) { | ||
| 461 | $section = "inherited methods"; | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | $section = "imported subs"; | ||
| 465 | } | ||
| 466 | |||
| 467 | $self->_moduleOverviewAddSection( | ||
| 468 | table => $t, | ||
| 469 | section => $section, | ||
| 470 | subsection => 'public', | ||
| 471 | rows => [ $mo->getInheritedPublicMethods ] | ||
| 472 | ); | ||
| 473 | |||
| 474 | if ($showInheritedPrivateMethods) { | ||
| 475 | $t->addRow(''); | ||
| 476 | |||
| 477 | $self->_moduleOverviewAddSection( | ||
| 478 | table => $t, | ||
| 479 | section => '', | ||
| 480 | subsection => 'private', | ||
| 481 | rows => [ $mo->getInheritedPrivateMethods ] | ||
| 482 | ); | ||
| 483 | } | ||
| 484 | |||
| 485 | print $t; | ||
| 486 | } | ||
| 487 | |||
| 488 | method _moduleOverviewAddSection (Object :$table!, | ||
| 489 | Str :$section, | ||
| 490 | Str :$subsection, | ||
| 491 | ArrayRef :$rows!) { | ||
| 492 | |||
| 493 | push @$rows, '' if !@$rows; | ||
| 494 | |||
| 495 | my $first = 1; | ||
| 496 | foreach my $row (@$rows) { | ||
| 497 | |||
| 498 | if ($first) { | ||
| 499 | $first = 0; | ||
| 500 | } | ||
| 501 | else { | ||
| 502 | $section = ''; | ||
| 503 | $subsection = ''; | ||
| 504 | } | ||
| 505 | |||
| 506 | if ( ref($row) eq 'ARRAY' ) { | ||
| 507 | $table->addRow( $section, $subsection, @$row ); | ||
| 508 | } | ||
| 509 | else { | ||
| 510 | $table->addRow( $section, $subsection, $row ); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | 1; |