| 1 | package Any::Moose::Convert; | ||||||||||
| 2 | 3 | ▶ | use 5.008_001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | our $VERSION = '0.004'; | |||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | our @EXPORT = qw(moose2mouse mouse2moose); | |||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use PerlIO::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use File::Find; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use File::Path qw(mkpath); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | ▶ | use File::Basename qw(dirname); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 3 | my $IGNORE = qr/\A \. (?: git | svn | cvs | hg) \b/; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | sub moose2mouse { _moose2mouse(1, @_) } | ||||||||
|
Subroutine
moose2mouse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 20 | 1 | ▶ | sub mouse2moose { _moose2mouse(0, @_) } | ||||||||
|
Subroutine
mouse2moose: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | |||||||||||
| 22 | 2 | ▶ | sub _moose2mouse { | ||||||||
|
Subroutine
_moose2mouse: called CC=8 SCAR=21.1
| |||||||||||
| 23 | 2 | my $moose2mouse = shift; | |||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | my @paths = @_ ? @_ : @ARGV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | my $dry_run; | |||||||||
| 28 | 2 | ▶ | $dry_run = !!(shift @paths) if $paths[0] eq '--dry-run'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 2 | foreach my $file(_expand(@paths)){ | |||||||||
| 31 | 2 | my $new_file = $file; | |||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | if($moose2mouse){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 1 | $new_file =~ s/Moose/Mouse/g; | |||||||||
| 35 | } | ||||||||||
| 36 | else{ | ||||||||||
| 37 | 1 | $new_file =~ s/Mouse/Moose/g; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 2 | ▶ | if($new_file !~ /^mo[ou]se/){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 2 | ▶ | $new_file = File::Spec->catfile( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | $moose2mouse ? 'mouse' : 'moose', | ||||||||||
| 43 | $new_file | ||||||||||
| 44 | ); | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 2 | print "$file to $new_file\n"; | |||||||||
| 48 | |||||||||||
| 49 | 2 | ▶ | next if $dry_run; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 2 | _do_moose2mouse_to_file($moose2mouse, $file, $new_file); | |||||||||
| 52 | } | ||||||||||
| 53 | 2 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | sub _expand { | ||||||||
|
Subroutine
_expand: called CC=4 SCAR=14.2
| |||||||||||
| 57 | 2 | my @files; | |||||||||
| 58 | 2 | foreach my $path(@_){ | |||||||||
| 59 | 2 | ▶ | if(-e $path){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 2 | ▶ | if(-f $path){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | push @files, $path; | |||||||||
| 62 | } | ||||||||||
| 63 | else{ | ||||||||||
| 64 | 4 | ▶ | find(sub{ | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 65 | 4 | ▶ | return if !-f $_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2 | push @files, $File::Find::name; | |||||||||
| 67 | 2 | }, $path); | |||||||||
| 68 | } | ||||||||||
| 69 | } | ||||||||||
| 70 | else{ | ||||||||||
| 71 | 0 | die "The path not found: $path\n"; | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | 2 | my %seen; | |||||||||
| 75 | |||||||||||
| 76 | 2 | return grep { !$seen{$_}++ } | |||||||||
| 77 | 2 | map{ File::Spec->abs2rel($_) } @files; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | ▶ | sub _do_moose2mouse_to_file { | ||||||||
|
Subroutine
_do_moose2mouse_to_file: called CC=4 SCAR=13.9
| |||||||||||
| 81 | 2 | my($moose2mouse, $file, $new_file) = @_; | |||||||||
| 82 | |||||||||||
| 83 | 2 | my $content; | |||||||||
| 84 | { | ||||||||||
| 85 | 2 | my $in = PerlIO::Util->open('<:raw', $file); | |||||||||
| 86 | 2 | local $/; | |||||||||
| 87 | 2 | $content = <$in>; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 2 | ▶ | if($file !~ $IGNORE){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 2 | ▶ | if($moose2mouse){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 1 | _convert_moose_to_mouse(\$content); | |||||||||
| 93 | } | ||||||||||
| 94 | else{ | ||||||||||
| 95 | 1 | _convert_mouse_to_moose(\$content); | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 2 | mkpath(dirname($new_file), 1); | |||||||||
| 100 | |||||||||||
| 101 | 2 | my $out = PerlIO::Util->open('>:raw', $new_file); | |||||||||
| 102 | |||||||||||
| 103 | 2 | print $out $content; | |||||||||
| 104 | 2 | ▶ | close $out or die "Cannot close '$new_file': $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 2 | return; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 3 | my $cmop_utils = join '|', qw( | |||||||||
| 109 | is_class_loaded | ||||||||||
| 110 | load_class | ||||||||||
| 111 | load_first_existing_class | ||||||||||
| 112 | class_of | ||||||||||
| 113 | get_metaclass_by_name | ||||||||||
| 114 | get_code_info | ||||||||||
| 115 | ); | ||||||||||
| 116 | |||||||||||
| 117 | 1 | ▶ | sub _convert_moose_to_mouse { | ||||||||
|
Subroutine
_convert_moose_to_mouse: called CC=1 SCAR=0.0
| |||||||||||
| 118 | 1 | local(*_) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 1 | s{Moose}{Mouse}mxsg; | |||||||||
| 121 | |||||||||||
| 122 | # e.g. Class::MOP::load_class -> Mouse::Util::load_class | ||||||||||
| 123 | 1 | s{\b Class::MOP::($cmop_utils) \b} | |||||||||
| 124 | {Mouse::Util::$1}mxsgo; | ||||||||||
| 125 | |||||||||||
| 126 | 1 | s{\b use \s+ Class::MOP \b} | |||||||||
| 127 | {use Mouse::Meta::Class}xmsg; | ||||||||||
| 128 | |||||||||||
| 129 | # e.g. Class::MOP::Class -> Mouse::Meta::Class | ||||||||||
| 130 | 1 | s{\b Class::MOP:: \b} | |||||||||
| 131 | {Mouse::Meta::}xmsg; | ||||||||||
| 132 | |||||||||||
| 133 | 1 | return; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 1 | ▶ | sub _convert_mouse_to_moose { | ||||||||
|
Subroutine
_convert_mouse_to_moose: called CC=1 SCAR=0.0
| |||||||||||
| 137 | 1 | local(*_) = @_; | |||||||||
| 138 | |||||||||||
| 139 | 1 | s{\b Mouse::Util::($cmop_utils) \b} | |||||||||
| 140 | {Class::MOP::$1}mxsgo; | ||||||||||
| 141 | |||||||||||
| 142 | 1 | s{Mouse}{Moose}mxsg; | |||||||||
| 143 | |||||||||||
| 144 | 1 | return; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 3 | 1; | |||||||||
| 148 | __END__ | ||||||||||