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