| 1 | package PPIx::EditorTools::RenamePackageFromPath; | ||||||||||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||
| 3 | # ABSTRACT: Change the package name based on the files path | ||||||||||||||||||||
| 4 | 3 | $PPIx::EditorTools::RenamePackageFromPath::VERSION = '0.20'; | |||||||||||||||||||
| 5 | 3 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 3 | use Class::XSAccessor accessors => { | |||||||||||||||||||
| 11 | 'replacement' => 'replacement', | ||||||||||||||||||||
| 12 | 'filename' => 'filename', | ||||||||||||||||||||
| 13 | 3 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | ▶ | use base 'PPIx::EditorTools'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 3 | ▶ | use PPIx::EditorTools::RenamePackage; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 3 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 3 | ▶ | use File::Basename; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub rename { | ||||||||||||||||||||
| 23 | 4 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||
|
Subroutine
rename: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 24 | 4 | $self->process_doc(%args); | |||||||||||||||||||
| 25 | 4 | ▶ | my $path = $args{filename} || croak "filename required"; | ||||||||||||||||||
|
Condition33%1/3
$args{'filename'} || croak('filename required')
MC/DC0%0/2
$args{'filename'} || croak('filename required')
$args{'filename'}
croak('filename required')
Truth table33%1/3
A || B
A= $args{'filename'}B= croak('filename required')
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 4 | my $dir = dirname $path; | |||||||||||||||||||
| 28 | 4 | my $file = basename $path, qw/.pm .PM .Pm/; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | my @directories = | ||||||||||||||||||||
| 31 | 4 | ▶ | grep { $_ && !/^\.$/ } File::Spec->splitdir( File::Spec->rel2abs($dir) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 4 | my $replacement; | |||||||||||||||||||
| 33 | 4 | ▶ | if ( grep {/^lib$/} @directories ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 3 | ▶ | while ( shift(@directories) !~ /^lib$/ ) { } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||
| 36 | 1 | ▶ | @directories = grep { $_ && !/^\.$/ } File::Spec->splitdir($dir); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | 4 | $replacement = join( '::', @directories, $file ); | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 4 | return PPIx::EditorTools::RenamePackage->new( ppi => $self->ppi )->rename( replacement => $replacement ); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 3 | 1; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | __END__ | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =pod | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =encoding UTF-8 | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 NAME | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | PPIx::EditorTools::RenamePackageFromPath - Change the package name based on the files path | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 VERSION | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | version 0.20 | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | my $munged = PPIx::EditorTools::RenamePackageFromPath->new->rename( | ||||||||||||||||||||
| 63 | code => "package TestPackage;\nuse strict;\nBEGIN { | ||||||||||||||||||||
| 64 | $^W = 1; | ||||||||||||||||||||
| 65 | }\n1;\n", | ||||||||||||||||||||
| 66 | filename => './lib/Test/Code/Path.pm', | ||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | my $new_code_as_string = $munged->code; | ||||||||||||||||||||
| 70 | my $package_ppi_element = $munged->element; | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | This module uses PPI to change the package name of code. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head1 METHODS | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =over 4 | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item new() | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Constructor. Generally shouldn't be called with any arguments. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =item rename( ppi => PPI::Document $ppi, filename => Str ) | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =item rename( code => Str $code, filename => Str ) | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | Accepts either a C<PPI::Document> to process or a string containing | ||||||||||||||||||||
| 89 | the code (which will be converted into a C<PPI::Document>) to process. | ||||||||||||||||||||
| 90 | Replaces the package name with that supplied in the C<filename> | ||||||||||||||||||||
| 91 | parameter and returns a C<PPIx::EditorTools::ReturnObject> with the | ||||||||||||||||||||
| 92 | new code available via the C<ppi> or C<code> accessors, as a | ||||||||||||||||||||
| 93 | C<PPI::Document> or C<string>, respectively. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | An attempt will be made to derive the package name from the filename passed | ||||||||||||||||||||
| 96 | as a parameter. The filename's path will converted to an absolute path and | ||||||||||||||||||||
| 97 | it will be searched for a C<lib> directory which will be assumed the start | ||||||||||||||||||||
| 98 | of the package name. If no C<lib> directory can be found in the absolute | ||||||||||||||||||||
| 99 | path, the relative path will be used. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Croaks with a "package name not found" exception if unable to find the | ||||||||||||||||||||
| 102 | package name. | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head1 SEE ALSO | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | This class inherits from C<PPIx::EditorTools>. | ||||||||||||||||||||
| 109 | Also see L<App::EditorTools>, L<Padre>, and L<PPI>. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 AUTHORS | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =over 4 | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =item * | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Steffen Mueller C<smueller@cpan.org> | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =item * | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Mark Grimes C<mgrimes@cpan.org> | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =item * | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Ahmad M. Zawawi <ahmad.zawawi@gmail.com> | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =item * | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Gabor Szabo <gabor@szabgab.com> | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =item * | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm.. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 142 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||