| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use Data::Dumper; | ||
| 4 | use File::Find; | ||
| 5 | use Getopt::Std; | ||
| 6 | use IO::File; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | my ( $EXCLUDE, $HELP, $QUIET, $TEST ); | ||
| 10 | |||
| 11 | my $usage = <<EOF; | ||
| 12 | Usage: $0 -hqt [-e <regexp>] <directory> [<directory>...] | ||
| 13 | -e <regexp>: Exclude paths matching <regexp> case-insensitive. e.g. "(.gif|.jpg)$" | ||
| 14 | -h: Display help message and exit | ||
| 15 | -q: Quiet mode, do not report normal processing of files | ||
| 16 | -t: Do not actually change files, just report what changes would be made | ||
| 17 | EOF | ||
| 18 | |||
| 19 | my $helpmsg = <<EOF; | ||
| 20 | This utility converts existing components to use new syntax | ||
| 21 | introduced in Mason 0.8. | ||
| 22 | |||
| 23 | 1. Old-style mc_commands (mc_comp, mc_file, etc.) are converted to | ||
| 24 | new-style \$m methods (\$m->comp, \$m->file, etc.) See Commands.pod for | ||
| 25 | all the conversions to be performed. | ||
| 26 | |||
| 27 | 2. References to request variable \$REQ are converted to \$m. | ||
| 28 | |||
| 29 | All directories will be traversed recursively. We STRONGLY recommend | ||
| 30 | that you backup your components, and/or use the -t flag to preview, | ||
| 31 | before running this program for real. Files are modified | ||
| 32 | destructively and no automatic backups are created. | ||
| 33 | EOF | ||
| 34 | |||
| 35 | my $warning = <<EOF; | ||
| 36 | Warning: All directories will be traversed recursively. Files are | ||
| 37 | modified destructively and no automatic backups are created. | ||
| 38 | EOF | ||
| 39 | |||
| 40 | sub usage { | ||
| 41 | print $usage; | ||
| 42 | exit; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub main { | ||
| 46 | my (%opts); | ||
| 47 | getopts( 'e:hlqtu', \%opts ); | ||
| 48 | ( $EXCLUDE, $HELP, $QUIET, $TEST ) = @opts{qw(e h q t)}; | ||
| 49 | if ($HELP) { print "$helpmsg\n$usage"; exit } | ||
| 50 | if ( !@ARGV ) { print "$usage\n$helpmsg"; exit } | ||
| 51 | my @dirs = @ARGV; | ||
| 52 | |||
| 53 | if ( !$TEST ) { | ||
| 54 | print "*** Mason 0.8 Conversion ***\n\n"; | ||
| 55 | print "Quiet mode.\n" if defined($QUIET); | ||
| 56 | print "Excluding paths matching ($EXCLUDE).\n" if defined($EXCLUDE); | ||
| 57 | print "Processing " | ||
| 58 | . ( @dirs == 1 ? "directory " : "directories " ) | ||
| 59 | . join( ",", @dirs ) . "\n"; | ||
| 60 | print $warning; | ||
| 61 | print "\nProceed? [n] "; | ||
| 62 | exit if ( ( my $ans = <STDIN> ) !~ /[Yy]/ ); | ||
| 63 | } | ||
| 64 | my $sub = sub { | ||
| 65 | if ( -f $_ && -s _ ) { | ||
| 66 | return | ||
| 67 | if defined($EXCLUDE) && "$File::Find::dir/$_" =~ /$EXCLUDE/i; | ||
| 68 | convert( $_, "$File::Find::dir/$_" ); | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | find( $sub, @dirs ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub convert { | ||
| 75 | my ( $file, $path ) = @_; | ||
| 76 | my $buf; | ||
| 77 | my $infh = new IO::File $file; | ||
| 78 | if ( !$infh ) { warn "cannot read $path: $!"; return } | ||
| 79 | { local $/ = undef; $buf = <$infh> } | ||
| 80 | |||
| 81 | my $c = 0; | ||
| 82 | my ( @changes, @failures ); | ||
| 83 | my $report = sub { | ||
| 84 | push( @changes, $_[1] ? "$_[0] --> $_[1]" : "removed $_[0]" ); | ||
| 85 | }; | ||
| 86 | my $report_failure = sub { push( @failures, $_[0] ) }; | ||
| 87 | |||
| 88 | # | ||
| 89 | # Convert mc_ commands to $m-> method equivalents | ||
| 90 | # | ||
| 91 | # Easy substitutions | ||
| 92 | # | ||
| 93 | my $easy_cmds = join( | ||
| 94 | "|", | ||
| 95 | qw(abort cache cache_self call_self comp comp_exists dhandler_arg file file_root out time) | ||
| 96 | ); | ||
| 97 | if ( !$TEST ) { | ||
| 98 | $c += ( $buf =~ s{mc_($easy_cmds)(?![A-Za-z0-9 _])}{"\$m->$1"}geo ); | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | while ( $buf =~ m{(mc_($easy_cmds)(?![A-Za-z0-9 _]))}go ) { | ||
| 102 | $report->( $1, "\$m->$2" ); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | # Boilerplate substitutions for methods with no arguments | ||
| 107 | my @subs = ( | ||
| 108 | [ 'mc_auto_comp', '$m->fetch_next->path' ], | ||
| 109 | [ 'mc_caller', '$m->callers(1)->path' ], | ||
| 110 | [ 'mc_comp_source', '$m->current_comp->source_file' ], | ||
| 111 | [ 'mc_comp_stack', 'map($_->title,$m->callers)' ], | ||
| 112 | ); | ||
| 113 | foreach my $sub (@subs) { | ||
| 114 | my ( $mc_cmd, $repl ) = @$sub; | ||
| 115 | if ( !$TEST ) { | ||
| 116 | $c += ( $buf =~ s{$mc_cmd(\s*\(\))?(?!\s*[\(])}{$repl}ge ); | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | while ( $buf =~ m{($mc_cmd(\s*\(\))?(?!\s*[\(]))}g ) { | ||
| 120 | $report->( $1, $repl ); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | # Boilerplate substitutions for methods with arguments | ||
| 126 | @subs = ( | ||
| 127 | [ 'mc_auto_next', '$m->call_next' ], | ||
| 128 | ); | ||
| 129 | foreach my $sub (@subs) { | ||
| 130 | my ( $mc_cmd, $repl ) = @$sub; | ||
| 131 | if ( !$TEST ) { | ||
| 132 | $c += ( $buf =~ s{$mc_cmd}{$repl}ge ); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | while ( $buf =~ m{($mc_cmd)}g ) { | ||
| 136 | $report->( $1, $repl ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | # mc_comp_source with simple argument | ||
| 142 | if ( !$TEST ) { | ||
| 143 | $c | ||
| 144 | += ( $buf | ||
| 145 | =~ s{mc_comp_source\s*\(([^\(\)]+)\)}{"\$m->fetch_comp($1)->source_file"}ge | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | while ( $buf =~ m{(mc_comp_source\s*\(([^\(\)]+)\))}g ) { | ||
| 150 | $report->( $1, "\$m->fetch_comp($2)->source_file" ); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | # mc_suppress_http_header with and without arguments | ||
| 155 | if ( !$TEST ) { | ||
| 156 | $c += ( $buf =~ s{mc_suppress_http_header\s*(?!\s*\();?}{}g ); | ||
| 157 | $c += ( $buf =~ s{mc_suppress_http_header\s*\([^\(\)]*\)\s*;?}{}g ); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | while ( $buf =~ m{(mc_suppress_http_header\s*(?!\s*\();?)}g ) { | ||
| 161 | $report->( $1, "" ); | ||
| 162 | } | ||
| 163 | while ( $buf =~ m{(mc_suppress_http_header\s*\([^\(\)]*\)\s*;?)}g ) { | ||
| 164 | $report->( $1, "" ); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | # | ||
| 169 | # Convert $REQ to $m | ||
| 170 | # | ||
| 171 | if ( !$TEST ) { | ||
| 172 | $c += ( $buf =~ s{\$REQ(?![A-Za-z0-9_])}{\$m}go ); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | while ( $buf =~ m{(\$REQ(?![A-Za-z0-9_]))}go ) { | ||
| 176 | $report->( $1, "\$m" ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | # Report substitutions we can't handle | ||
| 181 | foreach my $cmd (qw(mc_comp_source mc_suppress_http_header)) { | ||
| 182 | if ( $buf =~ m{$cmd\s*\([^\)]*\(} ) { | ||
| 183 | $report_failure->("Can't convert $cmd with complex arguments"); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | if ( $buf =~ m{mc_date} ) { | ||
| 187 | $report_failure->("Can't convert mc_date"); | ||
| 188 | } | ||
| 189 | |||
| 190 | if ($TEST) { | ||
| 191 | if (@changes) { | ||
| 192 | print scalar(@changes) . " substitutions in $path:\n"; | ||
| 193 | print join( "\n", @changes ) . "\n"; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | if ( $c && !$TEST ) { | ||
| 198 | print "$c substitutions in $path\n" if !$QUIET; | ||
| 199 | my $outfh = new IO::File ">$file"; | ||
| 200 | if ( !$outfh ) { warn "cannot write $path: $!"; return } | ||
| 201 | $outfh->print($buf); | ||
| 202 | } | ||
| 203 | |||
| 204 | foreach my $failure (@failures) { | ||
| 205 | print "** Warning: $failure; must fix manually\n"; | ||
| 206 | } | ||
| 207 | |||
| 208 | print "\n" if ( ( $TEST && @changes ) || @failures ); | ||
| 209 | } | ||
| 210 | |||
| 211 | main(); |