From http://www.jwz.org/xscreensaver/xscreensaver-5.33.tar.gz
[xscreensaver] / OSX / update-info-plist.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2006-2014 Jamie Zawinski <jwz@jwz.org>
3 #
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 
10 # implied warranty.
11 #
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.
15 #
16 # This is invoked by a final "Shell Script" build action on each of the
17 # .saver targets in the XCode project.
18 #
19 # Created:  8-Mar-2006.
20
21 require 5;
22 #use diagnostics;       # Fails on some MacOS 10.5 systems
23 use strict;
24 use IPC::Open3;
25 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
26 use IO::Compress::Gzip qw(gzip $GzipError);
27
28 my ($exec_dir, $progname) = ($0 =~ m@^(.*?)/([^/]+)$@);
29
30 my ($version) = ('$Revision: 1.38 $' =~ m/\s(\d[.\d]+)\s/s);
31
32 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}";   # for seticon
33
34 my $thumbdir = $ENV{HOME} . '/www/xscreensaver/screenshots/';
35
36
37
38 my $verbose = 1;
39
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) ||
48       error ("pipe: $cmd[0]: $!");
49     error ("$cmd[0]: $!") unless $pid;
50     print $in $data;
51     close $in;
52     local $/ = undef;  # read entire file
53     $data = <$out>;
54     close $out;
55     waitpid ($pid, 0);
56     if ($?) {
57       my $exit_value  = $? >> 8;
58       my $signal_num  = $? & 127;
59       my $dumped_core = $? & 128;
60       error ("$cmd[0]: core dumped!") if ($dumped_core);
61       error ("$cmd[0]: signal $signal_num!") if ($signal_num);
62       error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
63     }
64   }
65   return $data;
66 }
67
68
69 sub read_info_plist($) {
70   my ($app_dir) = @_;
71   my $file  = "$app_dir/Contents/Info.plist";
72   my $file2 = "$app_dir/Info.plist";
73   $file =~ s@/+@/@g;
74   my $in;
75   if (open ($in, '<', $file)) {
76   } elsif (open ($in, '<', $file2)) {
77     $file = $file2;
78   } else {
79     error ("$file: $!");
80   }
81   print STDERR "$progname: read $file\n" if ($verbose > 2);
82   local $/ = undef;  # read entire file
83   my $body = <$in>;
84   close $in;
85
86   $body = convert_plist ($body, 0);  # convert to xml plist
87   return ($file, $body);
88 }
89
90
91 sub read_saver_xml($) {
92   my ($app_dir) = @_;
93   error ("$app_dir: no name") 
94     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
95   my $name  = $1;
96
97   return () if ($name eq 'XScreenSaver');
98   return () if ($name eq 'SaverTester');
99   return () if ($name eq 'XScreenSaverUpdater');
100
101   my $file  = "$app_dir/Contents/Resources/" . lc($name) . ".xml";
102   my $file2 = "$app_dir/" . lc($name) . ".xml";
103   my $file3 = "$app_dir/Contents/PlugIns/$name.saver/Contents/Resources/" .
104               lc($name) . ".xml";
105   $file =~ s@/+@/@g;
106   my $in;
107   if (open ($in, '<', $file)) {
108   } elsif (open ($in, '<', $file2)) { $file = $file2;
109   } elsif (open ($in, '<', $file3)) { $file = $file3;
110   } else {
111     error ("$file: $!");
112   }
113   print STDERR "$progname: read $file\n" if ($verbose > 2);
114   local $/ = undef;  # read entire file
115   my $body = <$in>;
116   close $in;
117
118   # Uncompress the XML if it is compressed.
119   my $body2 = '';
120   gunzip (\$body, \$body2) || error ("$app_dir: xml gunzip: $GunzipError");
121   my $was_compressed_p = ($body ne $body2);
122   return ($file, $body2, $was_compressed_p);
123 }
124
125
126 sub update_saver_xml($$) {
127   my ($app_dir, $vers) = @_;
128   my ($filename, $body, $was_compressed_p) = read_saver_xml ($app_dir);
129   my $obody = $body;
130
131   return () unless defined ($filename);
132
133   $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
134     error ("$filename: no name label");
135   my $name = $1;
136
137   $body =~ m@<_description>(.*?)</_description>@s ||
138     error ("$filename: no description tag");
139   my $desc = $1;
140   $desc =~ s/^([ \t]*\n)+//s;
141   $desc =~ s/\s*$//s;
142
143   # in case it's done already...
144   $desc =~ s@<!--.*?-->@@gs;
145   $desc =~ s/^.* version \d[^\n]*\n//s;
146   $desc =~ s/^From the XScreenSaver.*\n//m;
147   $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
148   $desc =~
149        s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
150   $desc =~ s/^\n+//s;
151
152   error ("$filename: description contains markup: $1")
153     if ($desc =~ m/([<>&][^<>&\s]*)/s);
154   error ("$filename: description contains ctl chars: $1")
155     if ($desc =~ m/([\000-\010\013-\037])/s);
156   error ("$filename: description contains non-ASCII and is not UTF-8: $1")
157     if ($body !~ m/\Q<?xml version="1.0" encoding="UTF-8"/s &&
158         $desc =~ m/([^\000-\176])/s);
159
160   error ("$filename: can't extract authors")
161     unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
162   $desc = $1;
163   my $authors = $2;
164   $desc =~ s/\s*$//s;
165
166   my $year = undef;
167   if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
168     $authors = $1;
169     $year = $2;
170   }
171
172   error ("$filename: can't extract year") unless $year;
173   my $cyear = 1900 + ((localtime())[5]);
174   $year = "$cyear" unless $year;
175   if ($year && ! ($year =~ m/$cyear/)) {
176     $year = "$year-$cyear";
177   }
178
179   $authors =~ s/[.,;\s]+$//s;
180
181   # List me as a co-author on all of them, since I'm the one who
182   # did the OSX port, packaged it up, and built the executables.
183   #
184   my $curator = "Jamie Zawinski";
185   if (! ($authors =~ m/$curator/si)) {
186     if ($authors =~ m@^(.*?),? and (.*)$@s) {
187       $authors = "$1, $2, and $curator";
188     } else {
189       $authors .= " and $curator";
190     }
191   }
192
193   my $desc1 = ("$name, version $vers.\n\n" .            # savername.xml
194                $desc . "\n" .
195                "\n" . 
196                "From the XScreenSaver collection: " .
197                "http://www.jwz.org/xscreensaver/\n" .
198                "Copyright \302\251 $year by $authors.\n");
199
200   my $desc2 = ("$name $vers,\n" .                       # Info.plist
201                "\302\251 $year $authors.\n" .
202                "From the XScreenSaver collection:\n" .
203                "http://www.jwz.org/xscreensaver/\n" .
204                "\n" .
205                $desc .
206                "\n");
207
208   # unwrap lines, but only when it's obviously ok: leave blank lines,
209   # and don't unwrap if that would compress leading whitespace on a line.
210   #
211   $desc2 =~ s/^(From |http:)/\n$1/gm;
212   1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
213   $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
214
215   $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
216
217   # NSXMLParser doesn't seem to work properly on Latin1 XML documents,
218   # so we convert these to UTF8 when embedding them in the .saver bundle.
219   $body =~ s@encoding="ISO-8859-1"@encoding="UTF-8"@gsi;
220
221   if ($obody eq $body && $was_compressed_p) {
222     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
223   } else {
224
225     # Gzip the XML.
226     my $body2 = '';
227     gzip (\$body, \$body2) || error ("$app_dir: xml gzip: $GzipError");
228     $body = $body2;
229
230     my $file_tmp = "$filename.tmp";
231     open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
232     print $out $body || error ("$file_tmp: $!");
233     close $out || error ("$file_tmp: $!");
234
235     if (!rename ("$file_tmp", "$filename")) {
236       unlink "$file_tmp";
237       error ("mv \"$file_tmp\" \"$filename\": $!");
238     }
239     print STDERR "$progname: wrote $filename\n" if ($verbose);
240   }
241
242   return ($desc1, $desc2);
243 }
244
245
246 sub compress_all_xml_files($) {
247   my ($dir) = @_;
248   opendir (my $dirp, $dir) || error ("$dir: $!");
249   my @files = readdir ($dirp);
250   closedir $dirp;
251   foreach my $f (sort @files) {
252     next unless ($f =~ m/\.xml$/si);
253     my $filename = "$dir/$f";
254     open (my $in, '<', $filename) || error ("$filename: $!");
255     print STDERR "$progname: read $filename\n" if ($verbose > 2);
256     local $/ = undef;  # read entire file
257     my $body = <$in>;
258     close $in;
259
260     if ($body =~ m/^<\?xml/s) {
261       my $body2 = '';
262       gzip (\$body, \$body2) || error ("$filename: xml gzip: $GzipError");
263       $body = $body2;
264       my $file_tmp = "$filename.tmp";
265       open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
266       print $out $body || error ("$file_tmp: $!");
267       close $out || error ("$file_tmp: $!");
268
269       if (!rename ("$file_tmp", "$filename")) {
270         unlink "$file_tmp";
271         error ("mv \"$file_tmp\" \"$filename\": $!");
272       }
273       print STDERR "$progname: compressed $filename\n" if ($verbose);
274     } elsif ($verbose > 2) {
275       print STDERR "$filename: already compressed\n";
276     }
277   }
278 }
279
280
281 sub set_plist_key($$$$) {
282   my ($filename, $body, $key, $val) = @_;
283
284   if ($body =~ m@^(.*
285                   \n\t<key>$key</key>
286                   \n\t<string>)([^<>]*)(</string>
287                   .*)$@xs) {
288 #    print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
289     $body = $1 . $val . $3;
290   } else {
291     error ("$filename: unparsable")
292       unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
293     $body = ($1 .
294              "\n\t<key>$key</key>" .
295              "\n\t<string>$val</string>" .
296              $2);
297   }
298
299   return $body;
300 }
301
302
303 sub set_icon($) {
304   my ($app_dir) = @_;
305   $app_dir =~ s@/+$@@s;
306
307   # "seticon" is from osxutils, http://osxutils.sourceforge.net/
308
309   my $icon = ($app_dir =~ m/\.saver$/ ? 'XScreenSaver' : 'SaverRunner');
310   $icon = "$app_dir/../../../$icon.icns";
311   my @cmd = ("seticon", "-d", $icon, $app_dir);
312   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
313     if ($verbose > 1);
314   system (@cmd);
315 }
316
317
318 sub set_thumb($) {
319   my ($app_dir) = @_;
320
321   return unless ($app_dir =~ m@\.saver/?$@s);
322
323   my @cmd = ("$exec_dir/update-thumbnail.pl", $thumbdir, $app_dir);
324   push @cmd, "-" . ("v" x $verbose) if ($verbose);
325   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
326     if ($verbose > 1);
327   system (@cmd);
328   my $exit  = $? >> 8;
329   exit ($exit) if $exit;
330 }
331
332
333 sub enable_gc($) {
334   my ($app_dir) = @_;
335
336   return unless ($app_dir =~ m@\.saver/?$@s);
337   my ($dir, $name) = ($app_dir =~ m@^(.*)/([^/]+)\.saver$@s);
338   error ("unparsable: $app_dir") unless $name;
339   my $exe = "$app_dir/Contents/MacOS/$name";
340   my @cmd = ("$dir/enable_gc", $exe);
341   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
342     if ($verbose > 1);
343   system (@cmd);
344   my $exit  = $? >> 8;
345   exit ($exit) if $exit;
346 }
347
348
349 sub fix_coretext($) {
350   my ($app_dir) = @_;
351
352   # In MacOS 10.8, they moved CoreText.framework from
353   # /System/Library/Frameworks/ApplicationServices.framework/Frameworks/
354   # to /System/Library/Frameworks/ which means that executables compiled
355   # on 10.8 and newer won't run on 10.7 and older because they can't find
356   # the library. Fortunately, 10.8 and later leave a symlink behind, so
357   # the old location still works. So we need our executables to contain
358   # an LC_LOAD_DYLIB pointing at the old directory instead of the new
359   # one.
360   # 
361   return if ($app_dir =~ m@-iphone@s);
362   my ($dir, $name) = ($app_dir =~ m@^(.*)/([^/]+)\.(app|saver)$@s);
363   error ("unparsable: $app_dir") unless $name;
364   my $exe = "$app_dir/Contents/MacOS/$name";
365
366   my $new = ("/System/Library/Frameworks/CoreText.framework/" .
367              "Versions/A/CoreText");
368   my $old = ("/System/Library/Frameworks/ApplicationServices.framework/" .
369              "Frameworks/CoreText.framework/Versions/A/CoreText");
370   my @cmd = ("install_name_tool", "-change", $new, $old, $exe);
371
372   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
373     if ($verbose > 1);
374   system (@cmd);
375   my $exit  = $? >> 8;
376   exit ($exit) if $exit;
377 }
378
379
380 sub update($) {
381   my ($app_dir) = @_;
382
383   error ("$app_dir: no name") 
384     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
385   my $app_name = $1;
386
387   my ($filename, $plist) = read_info_plist ($app_dir);
388   my $oplist = $plist;
389
390   error ("$filename: no version number")
391     unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
392                         <string>([^<>]+)</string>@sx);
393   my $vers = $1;
394   my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
395
396   # No, don't do this -- the iOS version reads the XML file in a few
397   # different places, and most of those places don't understand gzip.
398
399   if ($app_name eq 'XScreenSaver') {
400     compress_all_xml_files ($app_dir);
401   } elsif (! defined($info_str)) {
402     print STDERR "$progname: $filename: no XML file\n" if ($verbose > 1);
403   } else {
404
405     $info_str =~ m@^([^\n]+)\n@s ||
406       error ("$filename: unparsable copyright");
407     my $copyright = "$1";
408     $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
409
410     # Lose the Wikipedia URLs.
411     $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
412
413     $info_str =~ s/(\n\n)\n+/$1/gs;
414     $info_str =~ s/(^\s+|\s+$)//gs;
415     $plist = set_plist_key ($filename, $plist, 
416                             "NSHumanReadableCopyright", $copyright);
417     $plist = set_plist_key ($filename, $plist,
418                             "CFBundleLongVersionString",$copyright);
419     $plist = set_plist_key ($filename, $plist,
420                             "CFBundleGetInfoString",    $info_str);
421
422     if ($oplist eq $plist) {
423       print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
424     } else {
425       $plist = convert_plist ($plist, 1);  # convert to binary plist
426       my $file_tmp = "$filename.tmp";
427       open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
428       print $out $plist || error ("$file_tmp: $!");
429       close $out || error ("$file_tmp: $!");
430
431       if (!rename ("$file_tmp", "$filename")) {
432         unlink "$file_tmp";
433         error ("mv \"$file_tmp\" \"$filename\": $!");
434       }
435       print STDERR "$progname: wrote $filename\n" if ($verbose);
436     }
437   }
438
439   set_icon ($app_dir);
440   set_thumb ($app_dir);
441 # enable_gc ($app_dir);
442   fix_coretext ($app_dir)
443 }
444
445
446 sub error($) {
447   my ($err) = @_;
448   print STDERR "$progname: $err\n";
449   exit 1;
450 }
451
452 sub usage() {
453   print STDERR "usage: $progname [--verbose] program.app ...\n";
454   exit 1;
455 }
456
457 sub main() {
458
459   my @files = ();
460   while ($_ = $ARGV[0]) {
461     shift @ARGV;
462     if    (m/^--?verbose$/s)  { $verbose++; }
463     elsif (m/^-v+$/)          { $verbose += length($_)-1; }
464     elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
465     elsif (m/^-/s)            { usage(); }
466     else                      { push @files, $_; }
467   }
468   usage() unless ($#files >= 0);
469   foreach (@files) {
470     update ($_);
471   }
472 }
473
474 main();
475 exit 0;