| 1 | package Sumu::Perl::Modules; | ||
| 2 | |||
| 3 | # ABSTRACT: Sumu Perl Modules : List all installed Perl Modules | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Sumu Perl Modules | ||
| 8 | |||
| 9 | =head1 VERSION | ||
| 10 | |||
| 11 | version 0.4.9 | ||
| 12 | |||
| 13 | =head2 SYNOPSIS | ||
| 14 | |||
| 15 | Usage: | ||
| 16 | |||
| 17 | In a full mojo app | ||
| 18 | |||
| 19 | my $module = $c->param('module'); | ||
| 20 | |||
| 21 | chomp $module; | ||
| 22 | |||
| 23 | my $modules = Sumu::Perl::Modules->new(); | ||
| 24 | |||
| 25 | my ($mod, $dirs, $files) = $modules->_dirs(current_user => $current_user, module => "$module"); | ||
| 26 | |||
| 27 | my ($mod, $out) = $modules->_doc( module => "$module" ); | ||
| 28 | |||
| 29 | For /_inc_dir | ||
| 30 | |||
| 31 | my $module = $c->param('module'); | ||
| 32 | my $out = $modules->_inc_dir( inc_dir => "$module") | ||
| 33 | |||
| 34 | |||
| 35 | =head2 DESCRIPTION | ||
| 36 | |||
| 37 | List all installed Perl Modules on your system | ||
| 38 | |||
| 39 | =head2 For Developer | ||
| 40 | |||
| 41 | Being tested in: | ||
| 42 | |||
| 43 | Ubuntu 22.04 (WSL): | ||
| 44 | |||
| 45 | Dist: | ||
| 46 | |||
| 47 | Folder/Path: | ||
| 48 | |||
| 49 | ~/p/perl/Sumu-Perl-Modules | ||
| 50 | |||
| 51 | GitLab Repo: | ||
| 52 | |||
| 53 | http://ns44:40225/ns21u2204/sumu-perl-modules-dist-zilla | ||
| 54 | |||
| 55 | UI: | ||
| 56 | |||
| 57 | Folder/Path: | ||
| 58 | |||
| 59 | ~/p/perl/Perl-Modules-Companion | ||
| 60 | |||
| 61 | GitLab Repo: | ||
| 62 | |||
| 63 | http://ns44:40225/ns21u2204/sumu-perl-modules-companion | ||
| 64 | |||
| 65 | |||
| 66 | Rocky Linux 9 (Hyper-V): | ||
| 67 | |||
| 68 | Dist: | ||
| 69 | |||
| 70 | UI: | ||
| 71 | |||
| 72 | =head2 Strictures | ||
| 73 | |||
| 74 | Enable strict and warnings | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | use warnings; | ||
| 80 | |||
| 81 | =head2 our VERSION | ||
| 82 | |||
| 83 | our $VERSION = '0.4.9' | ||
| 84 | |||
| 85 | This version number is updated automatically! | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | our $VERSION = '0.4.9'; | ||
| 90 | |||
| 91 | |||
| 92 | =head2 Internals | ||
| 93 | |||
| 94 | Exporter | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | use parent qw(Exporter); | ||
| 99 | require Exporter; | ||
| 100 | our @ISA = ("Exporter"); | ||
| 101 | |||
| 102 | our @EXPORT_OK = qw( | ||
| 103 | NAME | ||
| 104 | new | ||
| 105 | _extutils | ||
| 106 | _dirs | ||
| 107 | _doc | ||
| 108 | _inc | ||
| 109 | _inc_dir | ||
| 110 | ); | ||
| 111 | |||
| 112 | =head2 sub NAME | ||
| 113 | |||
| 114 | Returns NAME: | ||
| 115 | |||
| 116 | The name of the app/module | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub NAME { my $self = shift; my $NAME = "Sumu Perl Modules"; return $NAME; } | ||
| 121 | |||
| 122 | |||
| 123 | =head2 Sub new | ||
| 124 | |||
| 125 | Bless the classes | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub new { | ||
| 130 | my $class = shift; | ||
| 131 | my $self = bless { @_ }, $class; | ||
| 132 | return $self; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | =head2 Required Modules | ||
| 137 | |||
| 138 | Eporter | ||
| 139 | |||
| 140 | ExtUtils::Installed | ||
| 141 | |||
| 142 | Pod::Html; | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | use ExtUtils::Installed; | ||
| 147 | |||
| 148 | my $inst = ExtUtils::Installed->new(); | ||
| 149 | |||
| 150 | use Pod::Html; | ||
| 151 | |||
| 152 | |||
| 153 | =head2 Sub _extutils | ||
| 154 | |||
| 155 | Returns List of modules as a table | ||
| 156 | |||
| 157 | including version number and number of dirs/files under given module | ||
| 158 | |||
| 159 | Usage: | ||
| 160 | |||
| 161 | my $modules_list = $modules->_extutils( module => "$module" ); | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | sub _extutils { | ||
| 166 | |||
| 167 | my $c = shift; | ||
| 168 | |||
| 169 | my @modules = $inst->modules(); | ||
| 170 | |||
| 171 | my $out; | ||
| 172 | |||
| 173 | my $mods; | ||
| 174 | my $serial = '0'; | ||
| 175 | $mods .= qq{}; | ||
| 176 | for (@modules) { | ||
| 177 | # | ||
| 178 | chomp; | ||
| 179 | # | ||
| 180 | my $module_version = $inst->version("$_"); | ||
| 181 | # | ||
| 182 | my $module_files = $inst->files("$_"); | ||
| 183 | # | ||
| 184 | my $mod_dirs = $inst->directories("$_"); | ||
| 185 | # | ||
| 186 | my $mod_packlist = $inst->packlist("$_"); # shows hash error | ||
| 187 | |||
| 188 | $serial++ if $_; | ||
| 189 | |||
| 190 | $mods .= qq{<tr> | ||
| 191 | <td>$serial</td> | ||
| 192 | <td> <a href="_dirs/$_" title="$_">$_</a> </td> | ||
| 193 | <td>$module_version</td> | ||
| 194 | <td>$module_files</td> | ||
| 195 | <td>$mod_dirs</td> | ||
| 196 | </tr> | ||
| 197 | }; | ||
| 198 | } | ||
| 199 | $mods .= qq{}; | ||
| 200 | |||
| 201 | return "$mods"; | ||
| 202 | |||
| 203 | } | ||
| 204 | # end | ||
| 205 | |||
| 206 | |||
| 207 | =head2 Sub _dirs | ||
| 208 | |||
| 209 | Show/Get Dirs in a given module | ||
| 210 | |||
| 211 | Returns an ordered list | ||
| 212 | |||
| 213 | Usage: | ||
| 214 | |||
| 215 | my ($module, $dirs, $files) = $modules->_dirs( module => "$module"); | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub _dirs { | ||
| 220 | |||
| 221 | my %in = ( | ||
| 222 | current_user => '', | ||
| 223 | module => '', | ||
| 224 | @_, | ||
| 225 | ); | ||
| 226 | |||
| 227 | # | ||
| 228 | my $current_user = $in{current_user}; | ||
| 229 | |||
| 230 | my $module = $in{module}; | ||
| 231 | chomp $module; | ||
| 232 | |||
| 233 | #$module =~ s!\\!\/!g; | ||
| 234 | |||
| 235 | # | ||
| 236 | my @mod_dirs = $inst->directories("$module"); | ||
| 237 | |||
| 238 | # files | ||
| 239 | my @mod_files = $inst->files("$module"); | ||
| 240 | |||
| 241 | my $dirs; | ||
| 242 | $dirs .= qq{<ol>}; | ||
| 243 | for (@mod_dirs) { | ||
| 244 | chomp; | ||
| 245 | next if $_ =~ /man3$/; | ||
| 246 | # | ||
| 247 | my $cust_filename = $_; | ||
| 248 | $cust_filename =~ s!^\/home\/$current_user!!g; | ||
| 249 | # | ||
| 250 | $dirs .= qq{<li> $cust_filename </li>}; | ||
| 251 | } | ||
| 252 | $dirs .= qq{</ol>}; | ||
| 253 | |||
| 254 | my $files; | ||
| 255 | $files .= qq{<ol>}; | ||
| 256 | for (@mod_files) { | ||
| 257 | chomp; | ||
| 258 | next if $_ =~ /\.3$/; | ||
| 259 | # | ||
| 260 | my $cust_filename = $_; | ||
| 261 | $cust_filename =~ s!^\/home\/$current_user!!g; | ||
| 262 | # | ||
| 263 | $files .= qq{<li> <a href="_doc/$_" title="$cust_filename">$cust_filename</a> </li>}; | ||
| 264 | } | ||
| 265 | $files .= qq{</ol>}; | ||
| 266 | |||
| 267 | # | ||
| 268 | return ($module, $dirs, $files); | ||
| 269 | |||
| 270 | } | ||
| 271 | # end _dirs | ||
| 272 | |||
| 273 | |||
| 274 | =head2 Sub _doc | ||
| 275 | |||
| 276 | Show Doc for a given module | ||
| 277 | |||
| 278 | Returns results from command | ||
| 279 | |||
| 280 | perldoc Module | ||
| 281 | |||
| 282 | as text with a line break (<br>) appended to end of each line | ||
| 283 | Usage: | ||
| 284 | |||
| 285 | my ($total_lines, $out, $error) = $modules->_doc( module => "$module" ); | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub _doc { | ||
| 290 | |||
| 291 | my %in = ( | ||
| 292 | module => '', | ||
| 293 | @_, | ||
| 294 | ); | ||
| 295 | |||
| 296 | # | ||
| 297 | my $out; | ||
| 298 | # | ||
| 299 | my $total_lines; | ||
| 300 | # | ||
| 301 | my $error; | ||
| 302 | # | ||
| 303 | my $module = $in{module}; | ||
| 304 | chomp $module; | ||
| 305 | |||
| 306 | #$module =~ s!\\!\/!g; | ||
| 307 | |||
| 308 | if ( -f "$module" ) { | ||
| 309 | if ( open( my $FILE, "<", "$module" ) ) { | ||
| 310 | while ( my $line = <$FILE> ) { | ||
| 311 | # | ||
| 312 | $total_lines++ if $line; | ||
| 313 | # | ||
| 314 | $line =~ s!\<!<!g; | ||
| 315 | $line =~ s!\>!>!g; | ||
| 316 | # | ||
| 317 | $out .= qq{$line<br>}; | ||
| 318 | } | ||
| 319 | close $FILE; | ||
| 320 | } else { | ||
| 321 | $error .= qq{<div class="alert alert-danger">Unable to open module</div>}; | ||
| 322 | } | ||
| 323 | } else { | ||
| 324 | $error = qq{<div class="alert alert-danger">Not a file or incorrect file path</div>}; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | return ($total_lines, $out, $error); | ||
| 329 | |||
| 330 | # | ||
| 331 | |||
| 332 | } | ||
| 333 | # end _doc | ||
| 334 | |||
| 335 | |||
| 336 | =head2 Sub _inc | ||
| 337 | |||
| 338 | List all directories in the @INC | ||
| 339 | |||
| 340 | Returns an ordered list | ||
| 341 | |||
| 342 | with link to given URL | ||
| 343 | |||
| 344 | default is '/_inc_dir' | ||
| 345 | |||
| 346 | see sub _inc_dir | ||
| 347 | |||
| 348 | Creates URLs like the following: | ||
| 349 | |||
| 350 | <a | ||
| 351 | href="http://loclhost:20202/_inc_dir//home/ns21u2204/perl5/perlbrew/perls/perl-5.38.0/lib/5.38.0/x86_64-linux" | ||
| 352 | title="x86_64-linux" | ||
| 353 | > | ||
| 354 | x86_64-linux | ||
| 355 | </a> | ||
| 356 | |||
| 357 | <a | ||
| 358 | href="http://UxUrl/_inc_dir//home/ns21u2204/p/perl/Perl-Modules-Companion/lib" | ||
| 359 | title="lib" | ||
| 360 | > | ||
| 361 | lib | ||
| 362 | </a> | ||
| 363 | |||
| 364 | Usage: | ||
| 365 | |||
| 366 | my $out = $modules->_inc( module = "$module" ); | ||
| 367 | |||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub _inc { | ||
| 372 | |||
| 373 | my %in = ( | ||
| 374 | url => '/_inc_dir', | ||
| 375 | @_, | ||
| 376 | ); | ||
| 377 | |||
| 378 | chomp $in{_inc_dir}; | ||
| 379 | |||
| 380 | my $out; | ||
| 381 | |||
| 382 | $out .= qq{<ol>}; | ||
| 383 | |||
| 384 | for (@INC) { | ||
| 385 | # link text | ||
| 386 | my @name = split(/\//, $_); | ||
| 387 | # link | ||
| 388 | $out .= qq{<li>}; | ||
| 389 | $out .= qq{<a href="$in{url}/$_" }; | ||
| 390 | $out .= qq{title="$name[$#name]">}; | ||
| 391 | $out .= qq{$name[$#name]}; | ||
| 392 | $out .= qq{</a>}; | ||
| 393 | $out .= qq{</li> }; | ||
| 394 | } | ||
| 395 | |||
| 396 | $out .= qq{</ol>}; | ||
| 397 | |||
| 398 | return "$out"; | ||
| 399 | |||
| 400 | } | ||
| 401 | # end sub _inc | ||
| 402 | |||
| 403 | |||
| 404 | =head2 Sub _inc_dir | ||
| 405 | |||
| 406 | Do stuff with a given directory path from the @INC | ||
| 407 | |||
| 408 | Show files and subdirectories in the given dir. | ||
| 409 | |||
| 410 | Given directory should be an absolute path | ||
| 411 | |||
| 412 | Usage: | ||
| 413 | |||
| 414 | In a full mojo app | ||
| 415 | |||
| 416 | my $modules = Sumu::Perl::Modules->new(); | ||
| 417 | my $module = $c->param('module'); | ||
| 418 | my $out = $modules->_inc_dir( inc_dir => "$module") | ||
| 419 | |||
| 420 | =cut | ||
| 421 | |||
| 422 | sub _inc_dir { | ||
| 423 | # | ||
| 424 | my %in = ( | ||
| 425 | inc_dir => '', | ||
| 426 | @_, | ||
| 427 | ); | ||
| 428 | |||
| 429 | my $out; | ||
| 430 | $out .= qq{}; | ||
| 431 | |||
| 432 | my @directories; | ||
| 433 | my @files; | ||
| 434 | |||
| 435 | my $dir = "$in{inc_dir}"; | ||
| 436 | chomp $dir; | ||
| 437 | |||
| 438 | if (-d "$dir") { | ||
| 439 | if ( opendir (my $DIR, "$dir") ) { | ||
| 440 | # | ||
| 441 | my @dir = readdir($DIR); | ||
| 442 | # | ||
| 443 | while ( my $item = <@dir> ) { | ||
| 444 | #Directories | ||
| 445 | push(@directories, "$dir/$item") if (-d "$dir/$item"); | ||
| 446 | # Files | ||
| 447 | push(@files, "$dir/$item") if (-f "$dir/$item"); | ||
| 448 | } | ||
| 449 | # | ||
| 450 | } else { | ||
| 451 | $out = qq{<div class="alert alert-warning">Unable to open dir</div>}; | ||
| 452 | } | ||
| 453 | } else { | ||
| 454 | $out = qq{<div class="alert alert-warning">Not a Dir $dir</div>}; | ||
| 455 | } | ||
| 456 | # | ||
| 457 | |||
| 458 | # | ||
| 459 | $out .= qq{<article class="container"><h2>Files</h2>}; | ||
| 460 | for (@files) { | ||
| 461 | my @file = split(/\//, $_); | ||
| 462 | $out .= qq{<a href="/_doc/$_" title="$file[$#file]">$file[$#file]</a> }; | ||
| 463 | } | ||
| 464 | $out .= qq{</article>}; | ||
| 465 | |||
| 466 | # | ||
| 467 | $out .= qq{<article class="container"><h2>Directories</h2>}; | ||
| 468 | for (@directories) { | ||
| 469 | # | ||
| 470 | my @d_name = split(/\//, $_); | ||
| 471 | $out .= qq{<a href="/_inc/$_" title="$d_name[$#d_name]">$d_name[$#d_name]</a> }; | ||
| 472 | } | ||
| 473 | $out .= qq{</article>}; | ||
| 474 | |||
| 475 | return "$out"; | ||
| 476 | |||
| 477 | } | ||
| 478 | # end _inc_dir | ||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | 1; |