1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#! /usr/bin/perl -w
# short tool to find out all used icons and allows deleting unused icons
# when building release files
my @default = (
"styles/standard/*.xml",
"styles/standard/*.mapcss",
"data/*.xml",
"src/org/openstreetmap/josm/*.java",
"src/org/openstreetmap/josm/*/*.java",
"src/org/openstreetmap/josm/*/*/*.java",
"src/org/openstreetmap/josm/*/*/*/*.java",
"src/org/openstreetmap/josm/*/*/*/*/*.java",
"src/org/openstreetmap/josm/*/*/*/*/*/*.java"
);
my %icons;
my $o = $/;
for my $arg (@ARGV ? @ARGV : @default)
{
for my $file (glob($arg))
{
my @defs;
open(FILE,"<",$file) or die "Could not open $file\n";
#print "Read file $file\n";
$/ = $file =~ /\.java$/ ? ";" : $o;
my $extends = "";
while(my $l = <FILE>)
{
if($l =~ /private static final String ([A-Z_]+) = ("[^"]+")/)
{
push(@defs, [$1, $2]);
}
next if $l =~ /NO-ICON/;
for my $d (@defs)
{
$l =~ s/$d->[0]/$d->[1]/g;
}
if($l =~ /icon\s*[:=]\s*["']([^"'+]+?)["']/)
{
++$icons{$1};
}
if(($l =~ /(?:icon-image|repeat-image|fill-image)\s*:\s*(\"?(.*?)\"?)\s*;/) && ($1 ne "none"))
{
my $img = $2;
++$icons{$img};
}
if($l =~ /ImageProvider(?:\.get)?\(\"([^\"]*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\)/)
{
my $i = $1;
++$icons{$i};
}
while($l =~ /\/\*\s*ICON\s*\*\/\s*\"(.*?)\"/g)
{
my $i = $1;
++$icons{$i};
}
while($l =~ /\/\*\s*ICON\((.*?)\)\s*\*\/\s*\"(.*?)\"/g)
{
my $i = "$1$2";
++$icons{$i};
}
if($l =~ /new\s+ImageLabel\(\"(.*?)\"/)
{
my $i = "statusline/$1";
++$icons{$i};
}
if($l =~ /setIcon\(\"(.*?)\"/)
{
my $i = "statusline/$1";
++$icons{$i};
}
if($l =~ /ImageProvider\.get(?:IfAvailable)?\(\"(.*?)\",\s*\"(.*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\s*\)/)
{
my $i = "$1/$2";
++$icons{$i};
}
if($l =~ /new ImageProvider\(\"(.*?)\",\s*\"(.*?)\"\s*\)/)
{
my $i = "$1/$2";
++$icons{$i};
}
if($l =~ /getCursor\(\"(.*?)\",\s*\"(.*?)\"/)
{
my $i = "cursor/modifier/$2";
++$icons{$i};
$i = "cursor/$1";
++$icons{$i};
}
if($l =~ /ImageProvider\.getCursor\(\"(.*?)\",\s*null\)/)
{
my $i = "cursor/$1";
++$icons{$i};
}
if($l =~ /super\(\s*tr\(\".*?\"\),\s*\"(.*?)\"/s)
{
my $i = "$extends$1";
++$icons{$i};
}
if($l =~ /super\(\s*trc\(\".*?\",\s*\".*?\"\),\s*\"(.*?)\"/s)
{
my $i = "$extends$1";
++$icons{$i};
}
if($l =~ /setButtonIcons.*\{(.*)\}/ || $l =~ /setButtonIcons\((.*)\)/ )
{
my $t = $1;
while($t =~ /\"(.*?)\"/g)
{
my $i = $1;
++$icons{$i};
}
}
if($l =~ /extends MapMode/)
{
$extends = "mapmode/";
}
elsif($l =~ /extends ToggleDialog/)
{
$extends = "dialogs/";
}
elsif($l =~ /extends JosmAction/)
{
$extends = "";
}
}
close FILE;
}
}
my %haveicons;
for($i = 1; my @ifiles = (glob("images".("/*" x $i).".png"), glob("images".("/*" x $i).".svg")); ++$i)
{
for my $ifile (sort @ifiles)
{
$ifile =~ s/^images\///;
# svg comes after png due to the glob, so only check for svg's
if($ifile =~ /^(.*)\.svg$/)
{
if($haveicons{"$1.png"})
{
print STDERR "$1: File exists twice as .svg and .png.\n";
}
# check for unwanted svg effects
if(open FILE, "<","images/$ifile")
{
undef $/;
my $f = <FILE>;
close FILE;
for my $sep ("'", '"')
{
while($f =~ /style\s*=\s*$sep([^$sep]+)$sep/g)
{
for my $x (split(/\s*;\s*/, $1))
{
print STDERR "$ifile: Style starts with minus: $x\n" if $x =~ /^-/;
}
}
}
if($f =~ /viewBox\s*=\s*["']([^"']+)["']/)
{
my $x = $1;
print STDERR "$ifile: ViewBox has float values: $x\n" if $x =~ /\./;
}
}
else
{
print STDERR "$ifile: Could not open file: $1";
}
}
$haveicons{$ifile} = 1;
}
}
for my $img (sort keys %icons)
{
if($img =~ /\.(png|svg)/)
{
print STDERR "$img: File does not exist!\n" if(!-f "images/$img");
delete $haveicons{$img};
}
else
{
print STDERR "$img(.svg|.png): File does not exist!\n" if(!-f "images/$img.png" && !-f "images/$img.svg");
delete $haveicons{"$img.svg"};
delete $haveicons{"$img.png"};
}
}
for my $img (sort keys %haveicons)
{
print "$img: Unused image.\n";
}
|