PDA

View Full Version : xtype conversion tool



frew
1 Dec 2009, 1:57 PM
Hello All!

Where I work we used xtypes for a long time the way that Ext do; shorter, lowercase names with _'s. Of course xtypes are also a performance boon and not just for nicer names. We decided that we'd like to convert all of our xtypes to be the actual name of our classes, so basically we have to look up *less*. Anyway, here's the short perl script to convert the classes and the files that use the classes. Don't do this without version control; you could lose your data.



#!perl
use strict;
use warnings;

use feature ':5.10';
use autodie;
use File::Copy;
use File::Find::Rule;

my @directories = @ARGV or die 'usage: fix_xtypes <directories of js files>';

my @files = File::Find::Rule->file->name( '*.js' )->in( @directories );

my $xtypes = find_and_fix_xtype_definitions(@files);
find_and_fix_xtype_usages($xtypes, \@files);

sub in_place_file_mod (&$) {
my $coderef = shift;
my $file = shift;
my $oldfile = "$file.old";
copy $file, $oldfile;

open my $out, '>', $file;
open my $in, '<', $oldfile;

while (<$in>) {
print {$out} $coderef->($_);
}

unlink $oldfile;
}

sub find_and_fix_xtype_usages {
my $xtypes = shift;
my $files = shift;

foreach my $file (@{$files}) {
in_place_file_mod {
if ($_ =~ /(.*)xtype\s*:\s*(['"])(.*?)\2([^\r\n]*)/ && $xtypes->{$3}) {
return "$1xtype: '$xtypes->{$3}'$4\r\n";
} else {
return $_;
}
} $file;
}
}

sub find_and_fix_xtype_definitions {
my @files = @_;

my $xtypes = {};

foreach my $file (@files) {
in_place_file_mod {
if ($_ =~ /(\s*)Ext\.reg\(\s*(['"])(.+)\2\s*,\s*([^\s]+)\s*\);/) {
$xtypes->{$3} = $4;
return "$1Ext.reg('$4', $4);";
} else {
return $_;
}
} $file;
}
return $xtypes;
}