Created
February 14, 2013 06:02
-
-
Save 9re/4950879 to your computer and use it in GitHub Desktop.
add prefix 'nakamap_' to all the resources.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use File::Find; | |
use IO::All; | |
use Data::Dumper; | |
use XML::Parser; | |
use utf8; | |
use Scalar::Util qw(looks_like_number); | |
my $root_dir = "../.."; | |
my $custom_attributes = {}; | |
&parse_attrs; | |
&replace_resources; | |
&replace_java_sources; | |
sub replace_resources { | |
find(\&replace_resource, "$root_dir/res"); | |
$_ = "$root_dir/AndroidManifest.xml"; | |
&replace_resource; | |
} | |
sub replace_resource { | |
return if -d; | |
if (m/\.xml$/) { | |
my $file < io($_); | |
$file =~ s|@([\w-]+)/(\w+)|&get_reference($1, $2)|eg; | |
$file =~ s|name="(\w+)"|&get_name($1)|eg; | |
$file =~ s|android:id="\@\+id/(\w+)"|&get_id($1)|eg; | |
$file =~ s|nakamap:(\w+)="(\w+)"|&get_custom_attribute($1, $2)|eg; | |
io($_) < $file; | |
} else { | |
#print "$File::Find::name\n"; | |
} | |
if (!m/^nakamap_/ && !(-e "nakamap_$_")) { | |
rename $_, "nakamap_$_"; | |
} | |
} | |
sub parse_attrs { | |
my $attr_xml = "$root_dir/res/values/attrs.xml"; | |
return unless -e $attr_xml; | |
my $parser = new XML::Parser(Style => 'Tree'); | |
my $tree = $parser->parsefile($attr_xml)->[1]; | |
for (my $i = 0; $i < scalar @$tree; ++$i) { | |
my $item = $tree->[$i]; | |
if ($item eq 'declare-styleable') { | |
++$i; | |
$item = $tree->[$i]; | |
my $name = $item->[0]->{'name'}; | |
$custom_attributes->{$name} = "nakamap_$name"; | |
for (my $j = 1; $j < scalar @$item; ++$j) { | |
my $attr = $item->[$j]; | |
if ($attr eq 'attr') { | |
++$j; | |
$attr = $item->[$j]; | |
my $attr_name = $attr->[0]->{'name'}; | |
$custom_attributes->{$name.'_'.$attr_name} = "nakamap_$name".'_'."nakamap_$attr_name"; | |
} | |
} | |
} | |
} | |
} | |
sub replace_java_sources { | |
find(\&replace_java_source, "$root_dir/src"); | |
} | |
sub replace_java_source { | |
return if -d; | |
my $file < io($_); | |
$file =~ s|((?:\w+\.)*)R\.(\w+)\.(\w+)|&get_identifier($1, $2, $3)|eg; | |
io($_) < $file; | |
} | |
sub get_id { | |
my ($identifier) = @_; | |
if (!($identifier =~ m/^nakamap_/)){ | |
return "android:id=\"\@+id/nakamap_$identifier\""; | |
} else { | |
return "android:id=\"\@+id/$identifier\""; | |
} | |
} | |
sub get_custom_attribute { | |
my ($identifier, $value) = @_; | |
if (!($identifier =~ m/^nakamap_/)){ | |
if ($value ne "true" && $value ne "false" && !looks_like_number($value)) { | |
$value = "nakamap_$value"; | |
} | |
return "nakamap:nakamap_$identifier=\"$value\""; | |
} else { | |
return "nakamap:$identifier=\"$value\""; | |
} | |
} | |
sub get_name { | |
my ($identifier) = @_; | |
if (!($identifier =~ m/^nakamap_/)){ | |
return "name=\"nakamap_$identifier\""; | |
} else { | |
return "name=\"$identifier\""; | |
} | |
} | |
sub get_reference { | |
my ($klass, $identifier) = @_; | |
if (!($identifier =~ m/^nakamap_/)){ | |
return "\@$klass/nakamap_$identifier"; | |
} else { | |
return "\@$klass/$identifier"; | |
} | |
} | |
sub get_identifier { | |
my ($pkg, $klass, $identifier) = @_; | |
$pkg = '' unless $pkg; | |
if (($pkg eq '' || $pkg eq 'com.kayac.nakamap.') && !($identifier =~ m/^nakamap_/)){ | |
if ($klass eq 'styleable') { | |
$identifier = $custom_attributes->{$identifier}; | |
} else { | |
$identifier = "nakamap_$identifier"; | |
} | |
return $pkg."R.$klass.$identifier"; | |
} else { | |
return $pkg."R.$klass.$identifier"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment