| 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, $LOWER, $QUIET, $TEST, $UPPER ); | ||
| 10 | |||
| 11 | my $usage = <<EOF; | ||
| 12 | Usage: $0 -hlqtu [-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 | -l: Write all section names as lowercase (<%init>, etc.) | ||
| 16 | -q: Quiet mode, do not report normal processing of files | ||
| 17 | -t: Do not actually change files, just report what changes would be made | ||
| 18 | -u: Write all section names as uppercase (<%INIT>, etc.) | ||
| 19 | EOF | ||
| 20 | |||
| 21 | my $helpmsg = <<EOF; | ||
| 22 | This utility converts existing components to use two new syntactic | ||
| 23 | constructs introduced in Mason 0.6. | ||
| 24 | |||
| 25 | 1. Long section names (<%perl_init>, <%perl_args>, etc.) are | ||
| 26 | converted to short names (<%init>, <%args>, etc.) You have the option | ||
| 27 | of also standardizing to uppercase (with -u) or lowercase (with -l); | ||
| 28 | by default the case will be kept the same. | ||
| 29 | |||
| 30 | 2. Component calls of the form | ||
| 31 | <% mc_comp('path', args...) %> | ||
| 32 | are converted to | ||
| 33 | <& path, args... &> | ||
| 34 | We try to recognize the most common variations; less common ones will | ||
| 35 | need to be converted manually. | ||
| 36 | |||
| 37 | Warning: If you use <% mc_comp(...) %> for components that *return* | ||
| 38 | HTML rather than outputting it, this will erroneously be converted to | ||
| 39 | <& &> (which discards the return value). Unfortunately there is no | ||
| 40 | easy way for us to detect this. Please be aware of this case and QA | ||
| 41 | your site carefully after conversion. | ||
| 42 | |||
| 43 | All directories will be traversed recursively. We STRONGLY recommend | ||
| 44 | that you backup your components, and/or use the -t flag to preview, | ||
| 45 | before running this program for real. Files are modified | ||
| 46 | destructively and no automatic backups are created. | ||
| 47 | EOF | ||
| 48 | |||
| 49 | my $warning = <<EOF; | ||
| 50 | Warning: All directories will be traversed recursively. Files are | ||
| 51 | modified destructively and no automatic backups are created. | ||
| 52 | EOF | ||
| 53 | |||
| 54 | sub usage { | ||
| 55 | print $usage; | ||
| 56 | exit; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub main { | ||
| 60 | my (%opts); | ||
| 61 | getopts( 'e:hlqtu', \%opts ); | ||
| 62 | ( $EXCLUDE, $HELP, $LOWER, $QUIET, $TEST, $UPPER ) | ||
| 63 | = @opts{qw(e h l q t u)}; | ||
| 64 | if ($HELP) { print "$helpmsg\n$usage"; exit } | ||
| 65 | if ( !@ARGV ) { print "$usage\n$helpmsg"; exit } | ||
| 66 | my @dirs = @ARGV; | ||
| 67 | |||
| 68 | if ( !$TEST ) { | ||
| 69 | print "*** Mason 0.6 Conversion ***\n\n"; | ||
| 70 | print "Quiet mode.\n" if defined($QUIET); | ||
| 71 | print "Excluding paths matching ($EXCLUDE).\n" if defined($EXCLUDE); | ||
| 72 | print "Processing " | ||
| 73 | . ( @dirs == 1 ? "directory " : "directories " ) | ||
| 74 | . join( ",", @dirs ) . "\n"; | ||
| 75 | print $warning; | ||
| 76 | print "\nProceed? [n] "; | ||
| 77 | exit if ( ( my $ans = <STDIN> ) !~ /[Yy]/ ); | ||
| 78 | } | ||
| 79 | my $sub = sub { | ||
| 80 | if ( -f $_ && -s _ ) { | ||
| 81 | return | ||
| 82 | if defined($EXCLUDE) && "$File::Find::dir/$_" =~ /$EXCLUDE/i; | ||
| 83 | convert( $_, "$File::Find::dir/$_" ); | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | find( $sub, @dirs ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub convert { | ||
| 90 | my ( $file, $path ) = @_; | ||
| 91 | my $buf; | ||
| 92 | my $infh = new IO::File $file; | ||
| 93 | if ( !$infh ) { warn "cannot read $path: $!"; return } | ||
| 94 | { local $/ = undef; $buf = <$infh> } | ||
| 95 | |||
| 96 | my $c = 0; | ||
| 97 | my @changes; | ||
| 98 | my $report = sub { push( @changes, "$_[0] --> $_[1]" ) }; | ||
| 99 | |||
| 100 | # | ||
| 101 | # Convert section names to short versions | ||
| 102 | # | ||
| 103 | my $pat = "<(/?%)perl_(args|cleanup|doc|init|once|text)>"; | ||
| 104 | if ( !$TEST ) { | ||
| 105 | if ($UPPER) { | ||
| 106 | $c += ( $buf =~ s{$pat}{"<$1".uc($2).">"}geio ); | ||
| 107 | } | ||
| 108 | elsif ($LOWER) { | ||
| 109 | $c += ( $buf =~ s{$pat}{"<$1".lc($2).">"}geio ); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | $c += ( $buf =~ s{$pat}{<$1$2>}gio ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | while ( $buf =~ m{($pat)}gio ) { | ||
| 117 | $report->( | ||
| 118 | $1, | ||
| 119 | "<$2" . ( $UPPER ? uc($3) : $LOWER ? lc($3) : $3 ) . ">" | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | # | ||
| 125 | # Convert <% mc_comp ... %> to <& ... &> | ||
| 126 | # | ||
| 127 | if ( !$TEST ) { | ||
| 128 | $c | ||
| 129 | += ( $buf | ||
| 130 | =~ s{<%\s*mc_comp\s*\(\s*\'([^\']+)\'\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g | ||
| 131 | ); | ||
| 132 | $c | ||
| 133 | += ( $buf | ||
| 134 | =~ s{<%\s*mc_comp\s*\(\s*\"([^\"\$]+)\"\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g | ||
| 135 | ); | ||
| 136 | $c | ||
| 137 | += ( $buf | ||
| 138 | =~ s{<%\s*mc_comp\s*\(\s*(\"[^\"]+\")\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g | ||
| 139 | ); | ||
| 140 | $c += ( $buf =~ s{<%\s*mc_comp\s*\(\s*(.*?)\s*\)\s*%>} {<& $1 &>}g ); | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | while ( | ||
| 144 | $buf =~ m{(<%\s*mc_comp\s*\(\s*\'([^\']+)\'\s*(.*?)\s*\)\s*%>)}g ) | ||
| 145 | { | ||
| 146 | $report->( $1, "<& $2$3 &>" ); | ||
| 147 | } | ||
| 148 | $buf | ||
| 149 | =~ s{<%\s*mc_comp\s*\(\s*\'([^\']+)\'\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g; | ||
| 150 | while ( $buf | ||
| 151 | =~ m{(<%\s*mc_comp\s*\(\s*\"([^\"\$]+)\"\s*(.*?)\s*\)\s*%>)}g ) { | ||
| 152 | $report->( $1, "<& $2$3 &>" ); | ||
| 153 | } | ||
| 154 | $buf | ||
| 155 | =~ s{<%\s*mc_comp\s*\(\s*\"([^\"\$]+)\"\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g; | ||
| 156 | while ( | ||
| 157 | $buf =~ m{(<%\s*mc_comp\s*\(\s*(\"[^\"]+\")\s*(.*?)\s*\)\s*%>)}g ) | ||
| 158 | { | ||
| 159 | $report->( $1, "<& $2$3 &>" ); | ||
| 160 | } | ||
| 161 | $buf | ||
| 162 | =~ s{<%\s*mc_comp\s*\(\s*(\"[^\"]+\")\s*(.*?)\s*\)\s*%>} {<& $1$2 &>}g; | ||
| 163 | while ( $buf =~ m{(<%\s*mc_comp\s*\((.*?)\s*\)\s*%>)}g ) { | ||
| 164 | $report->( $1, "<& $2 &>" ); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | if ($TEST) { | ||
| 169 | if (@changes) { | ||
| 170 | print scalar(@changes) . " substitutions in $path:\n"; | ||
| 171 | print join( "\n", @changes ) . "\n\n"; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | if ( $c && !$TEST ) { | ||
| 176 | print "$c substitutions in $path\n" if !$QUIET; | ||
| 177 | my $outfh = new IO::File ">$file"; | ||
| 178 | if ( !$outfh ) { warn "cannot write $path: $!"; return } | ||
| 179 | $outfh->print($buf); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | main(); |