source: josm/trunk/scripts/geticons.pl@ 15648

Last change on this file since 15648 was 13857, checked in by stoecker, 6 years ago

see #15240 - detect another type of ImageProvider calls for in image check script

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