blib/script/perl_types_refactor_names.pl untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 181 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 # bin/dev/perl_types_refactor_names.pl
3 # Apply refactor name mappings to input files, producing -MAPPED copies
4 use strict;
5 use warnings;
6 use File::Find;
7 use File::Basename;
8 use File::Spec;
9  
10 # Step 1: collect input files and directories
11 my @inputs = @ARGV;
12 unless (@inputs) {
13 die "ERROR: NO INPUT FILES\n";
14 }
15 my @files;
16 for my $input (@inputs) {
17 if (-d $input) {
18 find(sub { push @files, $File::Find::name if -f }, $input);
19 } elsif (-f $input) {
20 push @files, $input;
21 } else {
22 warn "ERROR: FILE DOES NOT EXIST: $input\n";
23 }
24 }
25 @files = do { my %seen; grep { !$seen{$_}++ } @files };
26  
27 # Step 2: load refactor names map
28 my %refactor_names_map;
29 {
30 # require the generated map module
31 use lib '.';
32 require 'bin/dev/perl_types_refactor_names_map.pm';
33 if (not defined $Perl::Types::RefactorNamesMap::refactor_names_map) {
34 die '$Perl::Types::RefactorNamesMap::refactor_names_map is not defined, dying';
35 }
36 %refactor_names_map = %{ $Perl::Types::RefactorNamesMap::refactor_names_map };
37 }
38 # prepare reverse-allowed and mapped trackers
39 my %refactor_names_map_reversable;
40 my %refactor_names_mapped;
41 for my $sfx (keys %refactor_names_map) {
42 $refactor_names_map_reversable{$sfx} = {};
43 $refactor_names_mapped{$sfx} = {};
44 }
45  
46 # Step 3: detect reversible mappings
47 my $broken = 0;
48 for my $sfx (keys %refactor_names_map) {
49 for my $old (keys %{ $refactor_names_map{$sfx} }) {
50 my $new = $refactor_names_map{$sfx}{$old};
51 if (exists $refactor_names_map{$sfx}{$new}) {
52 if ($refactor_names_map{$sfx}{$new} eq $old) {
53 print "REVERSABLE MAPPING FOUND [$sfx] $old <-> $new\n";
54 $refactor_names_map_reversable{$sfx}{$old} = $new;
55 $refactor_names_map_reversable{$sfx}{$new} = $old;
56 } else {
57 print "ERROR: BROKEN REVERSE MAPPING [$sfx] $old->$new, reverse maps to $refactor_names_map{$sfx}{$new}\n";
58 $broken = 1;
59 }
60 }
61 }
62 }
63 if ($broken) {
64 die "ERROR: BROKEN REVERSE MAPPINGS FOUND; please fix map and retry\n";
65 }
66  
67 # Step 4: apply mappings to each file
68 my %file_changes;
69 for my $file (@files) {
70 unless (-e $file) {
71 warn "ERROR: FILE DOES NOT EXIST: $file\n";
72 next;
73 }
74 unless (-r $file) {
75 warn "ERROR: FILE IS NOT READABLE: $file\n";
76 next;
77 }
78 # determine suffix and map to map suffix
79 my ($suffix) = $file =~ /\.([^.]+)$/;
80 my $map_sfx = $suffix;
81 if ($suffix =~ /^(pl|pmc|t)$/) {
82 $map_sfx = 'pm';
83 }
84 unless (exists $refactor_names_map{$map_sfx}) {
85 warn "ERROR: FILE SUFFIX UNKNOWN: $file\n";
86 next;
87 }
88 # read file
89 open my $in, '<', $file or next;
90 my @lines = <$in>;
91 close $in;
92 my $modified = 0;
93 $refactor_names_mapped{$map_sfx}{$file} = {};
94 # sort keys by length desc
95 my @keys = sort { length($b) <=> length($a) } keys %{ $refactor_names_map{$map_sfx} };
96 for my $old (@keys) {
97 my $new = $refactor_names_map{$map_sfx}{$old};
98 # find occurrences (whole hot names only)
99 my @occurs;
100 for my $i (0 .. $#lines) {
101 while ($lines[$i] =~ /(?<![A-Za-z0-9_:])\Q$old\E(?![A-Za-z0-9_:])/g) {
102 my $idx = $-[0];
103 push @occurs, [$i, $idx];
104 }
105 }
106 next unless @occurs;
107 # reversible prompt
108 if (exists $refactor_names_map_reversable{$map_sfx}{$old}) {
109 print "REVERSABLE HOT NAME [$file] $old -> $new found at ",
110 join(', ', map { '('.($_->[0]+1).', '.$_->[1].')' } @occurs), "\n";
111 print "Proceed with this mapping? (y/n) ";
112 chomp(my $ans = <STDIN>);
113 if (lc($ans) ne 'y') {
114 print "SKIPPING $old\n";
115 next;
116 }
117 }
118 # apply replacements from rightmost to leftmost to avoid shifting earlier offsets
119 for my $occ ( sort { $b->[0] <=> $a->[0] || $b->[1] <=> $a->[1] } @occurs ) {
120 my ($ln, $ch) = @$occ;
121 my $key = "$ln,$ch";
122 if ($refactor_names_mapped{$map_sfx}{$file}{$key}) {
123 print "RE-MATCHED HOT NAME, SKIPPING [$file] line ", $ln+1, " col $ch for $old\n";
124 next;
125 }
126 substr($lines[$ln], $ch, length($old)) = $new;
127 $refactor_names_mapped{$map_sfx}{$file}{$key} = 1;
128 $modified = 1;
129 }
130 }
131 # write mapped file
132 if ($modified) {
133 my ($name, $dir, $ext) = fileparse($file, qr/\.[^.]*$/);
134 my $newfile = File::Spec->catfile($dir, $name . '-MAPPED' . $ext);
135 open my $out, '>', $newfile or die "Cannot write '$newfile': $!";
136 print $out @lines;
137 close $out;
138 $file_changes{$file} = $newfile;
139 } else {
140 $file_changes{$file} = undef;
141 }
142 }
143  
144 # Step 5: final report
145 for my $orig (sort keys %file_changes) {
146 my $mapped = $file_changes{$orig};
147 if ($mapped) {
148 print "File $orig modified; diff:\n";
149 system('diff', '-u', $orig, $mapped);
150 } else {
151 print "File $orig not modified.\n";
152 }
153 }
154 exit 0;