2 # Copyright © 2006-2013 Jamie Zawinski <jwz@jwz.org>
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation. No representations are made about the suitability of this
9 # software for any purpose. It is provided "as is" without express or
12 # Updates the NAME.xml file of a .saver bundle to include the current year,
13 # version number, etc. Also updates the Info.plist file to include the
14 # short documentation, authors, etc. in the Finder "Get Info" properties.
16 # This is invoked by a final "Shell Script" build action on each of the
17 # .saver targets in the XCode project.
19 # Created: 8-Mar-2006.
22 #use diagnostics; # Fails on some MacOS 10.5 systems
25 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
26 use IO::Compress::Gzip qw(gzip $GzipError);
28 my ($exec_dir, $progname) = ($0 =~ m@^(.*?)/([^/]+)$@);
30 my $version = q{ $Revision: 1.28 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
32 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}"; # for seticon
34 my $thumbdir = $ENV{HOME} . '/www/xscreensaver/screenshots/';
40 sub convert_plist($$) {
41 my ($data, $to_binary_p) = @_;
42 my $is_binary_p = ($data =~ m/^bplist/s);
43 if ($data && (!$is_binary_p) != (!$to_binary_p)) {
44 print STDERR "$progname: converting plist\n" if ($verbose > 2);
45 my $which = ($to_binary_p ? 'binary1' : 'xml1');
46 my $cmd = "plutil -convert $which -s -o - -";
47 my $pid = open3 (my $in, my $out, undef, $cmd) || error ("pipe: $cmd: $!");
50 local $/ = undef; # read entire file
58 sub read_info_plist($) {
60 my $file = "$app_dir/Contents/Info.plist";
61 my $file2 = "$app_dir/Info.plist";
64 if (open ($in, '<', $file)) {
65 } elsif (open ($in, '<', $file2)) {
70 print STDERR "$progname: read $file\n" if ($verbose > 2);
71 local $/ = undef; # read entire file
75 $body = convert_plist ($body, 0); # convert to xml plist
76 return ($file, $body);
80 sub read_saver_xml($) {
82 error ("$app_dir: no name")
83 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
86 return () if ($name eq 'XScreenSaver');
87 return () if ($name eq 'SaverTester');
88 return () if ($name eq 'XScreenSaverUpdater');
90 my $file = "$app_dir/Contents/Resources/" . lc($name) . ".xml";
91 my $file2 = "$app_dir/" . lc($name) . ".xml";
92 my $file3 = "$app_dir/Contents/PlugIns/$name.saver/Contents/Resources/" .
96 if (open ($in, '<', $file)) {
97 } elsif (open ($in, '<', $file2)) { $file = $file2;
98 } elsif (open ($in, '<', $file3)) { $file = $file3;
102 print STDERR "$progname: read $file\n" if ($verbose > 2);
103 local $/ = undef; # read entire file
107 # Uncompress the XML if it is compressed.
109 gunzip (\$body, \$body2) || error ("$app_dir: xml gunzip: $GunzipError");
110 my $was_compressed_p = ($body ne $body2);
111 return ($file, $body2, $was_compressed_p);
115 sub update_saver_xml($$) {
116 my ($app_dir, $vers) = @_;
117 my ($filename, $body, $was_compressed_p) = read_saver_xml ($app_dir);
120 return () unless defined ($filename);
122 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
123 error ("$filename: no name label");
126 $body =~ m@<_description>(.*?)</_description>@s ||
127 error ("$filename: no description tag");
129 $desc =~ s/^([ \t]*\n)+//s;
132 # in case it's done already...
133 $desc =~ s@<!--.*?-->@@gs;
134 $desc =~ s/^.* version \d[^\n]*\n//s;
135 $desc =~ s/^From the XScreenSaver.*\n//m;
136 $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
138 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
141 error ("$filename: description contains bad characters")
142 if ($desc =~ m/([^\t\n -~]|[<>])/);
144 error ("$filename: can't extract authors")
145 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
151 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
156 error ("$filename: can't extract year") unless $year;
157 my $cyear = 1900 + ((localtime())[5]);
158 $year = "$cyear" unless $year;
159 if ($year && ! ($year =~ m/$cyear/)) {
160 $year = "$year-$cyear";
163 $authors =~ s/[.,;\s]+$//s;
165 # List me as a co-author on all of them, since I'm the one who
166 # did the OSX port, packaged it up, and built the executables.
168 my $curator = "Jamie Zawinski";
169 if (! ($authors =~ m/$curator/si)) {
170 if ($authors =~ m@^(.*?),? and (.*)$@s) {
171 $authors = "$1, $2, and $curator";
173 $authors .= " and $curator";
177 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
180 "From the XScreenSaver collection: " .
181 "http://www.jwz.org/xscreensaver/\n" .
182 "Copyright \302\251 $year by $authors.\n");
184 my $desc2 = ("$name $vers,\n" . # Info.plist
185 "\302\251 $year $authors.\n" .
186 "From the XScreenSaver collection:\n" .
187 "http://www.jwz.org/xscreensaver/\n" .
192 # unwrap lines, but only when it's obviously ok: leave blank lines,
193 # and don't unwrap if that would compress leading whitespace on a line.
195 $desc2 =~ s/^(From |http:)/\n$1/gm;
196 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
197 $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
199 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
201 # NSXMLParser doesn't seem to work properly on Latin1 XML documents,
202 # so we convert these to UTF8 when embedding them in the .saver bundle.
203 $body =~ s@encoding="ISO-8859-1"@encoding="UTF-8"@gsi;
205 if ($obody eq $body && $was_compressed_p) {
206 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
211 gzip (\$body, \$body2) || error ("$app_dir: xml gzip: $GzipError");
214 my $file_tmp = "$filename.tmp";
215 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
216 print $out $body || error ("$file_tmp: $!");
217 close $out || error ("$file_tmp: $!");
219 if (!rename ("$file_tmp", "$filename")) {
221 error ("mv \"$file_tmp\" \"$filename\": $!");
223 print STDERR "$progname: wrote $filename\n" if ($verbose);
226 return ($desc1, $desc2);
230 sub compress_all_xml_files($) {
232 opendir (my $dirp, $dir) || error ("$dir: $!");
233 my @files = readdir ($dirp);
235 foreach my $f (sort @files) {
236 next unless ($f =~ m/\.xml$/si);
237 my $filename = "$dir/$f";
238 open (my $in, '<', $filename) || error ("$filename: $!");
239 print STDERR "$progname: read $filename\n" if ($verbose > 2);
240 local $/ = undef; # read entire file
244 if ($body =~ m/^<\?xml/s) {
246 gzip (\$body, \$body2) || error ("$filename: xml gzip: $GzipError");
248 my $file_tmp = "$filename.tmp";
249 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
250 print $out $body || error ("$file_tmp: $!");
251 close $out || error ("$file_tmp: $!");
253 if (!rename ("$file_tmp", "$filename")) {
255 error ("mv \"$file_tmp\" \"$filename\": $!");
257 print STDERR "$progname: compressed $filename\n" if ($verbose);
258 } elsif ($verbose > 2) {
259 print STDERR "$filename: already compressed\n";
265 sub set_plist_key($$$$) {
266 my ($filename, $body, $key, $val) = @_;
270 \n\t<string>)([^<>]*)(</string>
272 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
273 $body = $1 . $val . $3;
275 error ("$filename: unparsable")
276 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
278 "\n\t<key>$key</key>" .
279 "\n\t<string>$val</string>" .
289 $app_dir =~ s@/+$@@s;
291 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
293 my $icon = ($app_dir =~ m/\.saver$/ ? 'XScreenSaver' : 'SaverRunner');
294 $icon = "$app_dir/../../../$icon.icns";
295 my @cmd = ("seticon", "-d", $icon, $app_dir);
296 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
305 return unless ($app_dir =~ m@\.saver/?$@s);
307 my @cmd = ("$exec_dir/update-thumbnail.pl", $thumbdir, $app_dir);
308 push @cmd, "-" . ("v" x $verbose) if ($verbose);
309 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
313 exit ($exit) if $exit;
320 error ("$app_dir: no name")
321 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
324 my ($filename, $plist) = read_info_plist ($app_dir);
327 error ("$filename: no version number")
328 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
329 <string>([^<>]+)</string>@sx);
331 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
333 # No, don't do this -- the iOS version reads the XML file in a few
334 # different places, and most of those places don't understand gzip.
336 if ($app_name eq 'XScreenSaver') {
337 compress_all_xml_files ($app_dir);
338 } elsif (! defined($info_str)) {
339 print STDERR "$progname: $filename: no XML file\n" if ($verbose > 1);
342 $info_str =~ m@^([^\n]+)\n@s ||
343 error ("$filename: unparsable copyright");
344 my $copyright = "$1";
345 $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
347 # Lose the Wikipedia URLs.
348 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
350 $info_str =~ s/(\n\n)\n+/$1/gs;
351 $info_str =~ s/(^\s+|\s+$)//gs;
352 $plist = set_plist_key ($filename, $plist,
353 "NSHumanReadableCopyright", $copyright);
354 $plist = set_plist_key ($filename, $plist,
355 "CFBundleLongVersionString",$copyright);
356 $plist = set_plist_key ($filename, $plist,
357 "CFBundleGetInfoString", $info_str);
359 if ($oplist eq $plist) {
360 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
362 $plist = convert_plist ($plist, 1); # convert to binary plist
363 my $file_tmp = "$filename.tmp";
364 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
365 print $out $plist || error ("$file_tmp: $!");
366 close $out || error ("$file_tmp: $!");
368 if (!rename ("$file_tmp", "$filename")) {
370 error ("mv \"$file_tmp\" \"$filename\": $!");
372 print STDERR "$progname: wrote $filename\n" if ($verbose);
377 set_thumb ($app_dir);
383 print STDERR "$progname: $err\n";
388 print STDERR "usage: $progname [--verbose] program.app ...\n";
395 while ($_ = $ARGV[0]) {
397 if (m/^--?verbose$/s) { $verbose++; }
398 elsif (m/^-v+$/) { $verbose += length($_)-1; }
399 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
400 elsif (m/^-/s) { usage(); }
401 else { push @files, $_; }
403 usage() unless ($#files >= 0);