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

Last change on this file since 17496 was 16876, checked in by stoecker, 4 years ago

see #15240 - detect CSS in icons checker

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.0 KB
RevLine 
[2808]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 = (
[16006]6 "resources/styles/standard/*.xml",
7 "resources/styles/standard/*.mapcss",
8 "resources/data/*.xml",
[2808]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 {
[13277]25 my @defs;
[2808]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 {
[13277]32 if($l =~ /private static final String ([A-Z_]+) = ("[^"]+")/)
33 {
34 push(@defs, [$1, $2]);
35 }
[7668]36 next if $l =~ /NO-ICON/;
[13277]37 for my $d (@defs)
38 {
39 $l =~ s/$d->[0]/$d->[1]/g;
40 }
[10069]41 if($l =~ /icon\s*[:=]\s*["']([^"'+]+?)["']/)
[2808]42 {
43 ++$icons{$1};
44 }
45
[7670]46 if(($l =~ /(?:icon-image|repeat-image|fill-image)\s*:\s*(\"?(.*?)\"?)\s*;/) && ($1 ne "none"))
[4172]47 {
[10566]48 my $img = $2;
[4172]49 ++$icons{$img};
50 }
[13857]51 if($l =~ /ImageProvider(?:\.get)?\(\"([^\"]*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\)/)
[2808]52 {
53 my $i = $1;
54 ++$icons{$i};
55 }
[4172]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 }
[2808]66 if($l =~ /new\s+ImageLabel\(\"(.*?)\"/)
67 {
68 my $i = "statusline/$1";
69 ++$icons{$i};
70 }
[10069]71 if($l =~ /setIcon\(\"(.*?)\"/)
[2808]72 {
[10069]73 my $i = "statusline/$1";
74 ++$icons{$i};
75 }
[13857]76 if($l =~ /ImageProvider\.get(?:IfAvailable)?\(\"(.*?)\",\s*\"(.*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\s*\)/)
[10069]77 {
[2808]78 my $i = "$1/$2";
79 ++$icons{$i};
80 }
[10069]81 if($l =~ /new ImageProvider\(\"(.*?)\",\s*\"(.*?)\"\s*\)/)
82 {
83 my $i = "$1/$2";
84 ++$icons{$i};
85 }
[2811]86 if($l =~ /getCursor\(\"(.*?)\",\s*\"(.*?)\"/)
87 {
[2808]88 my $i = "cursor/modifier/$2";
89 ++$icons{$i};
90 $i = "cursor/$1";
91 ++$icons{$i};
92 }
[2811]93 if($l =~ /ImageProvider\.getCursor\(\"(.*?)\",\s*null\)/)
94 {
95 my $i = "cursor/$1";
96 ++$icons{$i};
97 }
[2808]98 if($l =~ /super\(\s*tr\(\".*?\"\),\s*\"(.*?)\"/s)
99 {
100 my $i = "$extends$1";
101 ++$icons{$i};
102 }
[2981]103 if($l =~ /super\(\s*trc\(\".*?\",\s*\".*?\"\),\s*\"(.*?)\"/s)
104 {
105 my $i = "$extends$1";
106 ++$icons{$i};
107 }
[13277]108 if($l =~ /setButtonIcons.*\{(.*)\}/ || $l =~ /setButtonIcons\((.*)\)/ )
[2808]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 }
[7668]121 elsif($l =~ /extends ToggleDialog/)
[2808]122 {
123 $extends = "dialogs/";
124 }
[10069]125 elsif($l =~ /extends JosmAction/)
126 {
127 $extends = "";
128 }
[2808]129 }
130 close FILE;
131 }
132}
133
134my %haveicons;
135
[16006]136for($i = 1; my @ifiles = (glob("resources/images".("/*" x $i).".png"), glob("resources/images".("/*" x $i).".svg")); ++$i)
[2808]137{
138 for my $ifile (sort @ifiles)
139 {
[16006]140 $ifile =~ s/^resources\/images\///;
[10561]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 {
[13279]146 print STDERR "$1: File exists twice as .svg and .png.\n";
[10561]147 }
148 # check for unwanted svg effects
[16006]149 if(open FILE, "<","resources/images/$ifile")
[10561]150 {
151 undef $/;
152 my $f = <FILE>;
153 close FILE;
[13384]154 for my $sep ("'", '"')
[10561]155 {
[13384]156 while($f =~ /style\s*=\s*$sep([^$sep]+)$sep/g)
[10561]157 {
[13384]158 for my $x (split(/\s*;\s*/, $1))
159 {
160 print STDERR "$ifile: Style starts with minus: $x\n" if $x =~ /^-/;
161 }
[10561]162 }
163 }
[16876]164 if($f =~ /<style[^>]+type=['"]text\/css['"][^>]*>/m)
165 {
166 print STDERR "$ifile: CSS-Style in SVG icon not supported\n";
167 }
[10561]168 if($f =~ /viewBox\s*=\s*["']([^"']+)["']/)
169 {
170 my $x = $1;
[13279]171 print STDERR "$ifile: ViewBox has float values: $x\n" if $x =~ /\./;
[10561]172 }
173 }
174 else
175 {
[13279]176 print STDERR "$ifile: Could not open file: $1";
[10561]177 }
178 }
[2808]179 $haveicons{$ifile} = 1;
180 }
181}
182
183for my $img (sort keys %icons)
184{
[7668]185 if($img =~ /\.(png|svg)/)
186 {
[16006]187 print STDERR "$img: File does not exist!\n" if(!-f "resources/images/$img");
[7668]188 delete $haveicons{$img};
189 }
190 else
191 {
[16006]192 print STDERR "$img(.svg|.png): File does not exist!\n" if(!-f "resources/images/$img.png" && !-f "resources/images/$img.svg");
[7668]193 delete $haveicons{"$img.svg"};
194 delete $haveicons{"$img.png"};
195 }
[2808]196}
197
198for my $img (sort keys %haveicons)
199{
[13279]200 print "$img: Unused image.\n";
[2808]201}
Note: See TracBrowser for help on using the repository browser.