c5233ae7a370afe3e3cee024a6b636f5f15a6c47
[xscreensaver] / OSX / update-info-plist.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2006-2013 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 = q{ $Revision: 1.26 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
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) || error ("pipe: $cmd: $!");
48     print $in $data;
49     close $in;
50     local $/ = undef;  # read entire file
51     $data = <$out>;
52     close $out;
53   }
54   return $data;
55 }
56
57
58 sub read_info_plist($) {
59   my ($app_dir) = @_;
60   my $file  = "$app_dir/Contents/Info.plist";
61   my $file2 = "$app_dir/Info.plist";
62   $file =~ s@/+@/@g;
63   my $in;
64   if (open ($in, '<', $file)) {
65   } elsif (open ($in, '<', $file2)) {
66     $file = $file2;
67   } else {
68     error ("$file: $!");
69   }
70   print STDERR "$progname: read $file\n" if ($verbose > 2);
71   local $/ = undef;  # read entire file
72   my $body = <$in>;
73   close $in;
74
75   $body = convert_plist ($body, 0);  # convert to xml plist
76   return ($file, $body);
77 }
78
79
80 sub read_saver_xml($) {
81   my ($app_dir) = @_;
82   error ("$app_dir: no name") 
83     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
84   my $name  = $1;
85
86   return () if ($name eq 'XScreenSaver');
87   return () if ($name eq 'SaverTester');
88
89   my $file  = "$app_dir/Contents/Resources/" . lc($name) . ".xml";
90   my $file2 = "$app_dir/" . lc($name) . ".xml";
91   my $file3 = "$app_dir/Contents/PlugIns/$name.saver/Contents/Resources/" .
92               lc($name) . ".xml";
93   $file =~ s@/+@/@g;
94   my $in;
95   if (open ($in, '<', $file)) {
96   } elsif (open ($in, '<', $file2)) { $file = $file2;
97   } elsif (open ($in, '<', $file3)) { $file = $file3;
98   } else {
99     error ("$file: $!");
100   }
101   print STDERR "$progname: read $file\n" if ($verbose > 2);
102   local $/ = undef;  # read entire file
103   my $body = <$in>;
104   close $in;
105
106   # Uncompress the XML if it is compressed.
107   my $body2 = '';
108   gunzip (\$body, \$body2) || error ("$app_dir: xml gunzip: $GunzipError");
109   my $was_compressed_p = ($body ne $body2);
110   return ($file, $body2, $was_compressed_p);
111 }
112
113
114 sub update_saver_xml($$) {
115   my ($app_dir, $vers) = @_;
116   my ($filename, $body, $was_compressed_p) = read_saver_xml ($app_dir);
117   my $obody = $body;
118
119   return () unless defined ($filename);
120
121   $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
122     error ("$filename: no name label");
123   my $name = $1;
124
125   $body =~ m@<_description>(.*?)</_description>@s ||
126     error ("$filename: no description tag");
127   my $desc = $1;
128   $desc =~ s/^([ \t]*\n)+//s;
129   $desc =~ s/\s*$//s;
130
131   # in case it's done already...
132   $desc =~ s@<!--.*?-->@@gs;
133   $desc =~ s/^.* version \d[^\n]*\n//s;
134   $desc =~ s/^From the XScreenSaver.*\n//m;
135   $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
136   $desc =~
137        s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
138   $desc =~ s/^\n+//s;
139
140   error ("$filename: description contains bad characters")
141     if ($desc =~ m/([^\t\n -~]|[<>])/);
142
143   error ("$filename: can't extract authors")
144     unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
145   $desc = $1;
146   my $authors = $2;
147   $desc =~ s/\s*$//s;
148
149   my $year = undef;
150   if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
151     $authors = $1;
152     $year = $2;
153   }
154
155   error ("$filename: can't extract year") unless $year;
156   my $cyear = 1900 + ((localtime())[5]);
157   $year = "$cyear" unless $year;
158   if ($year && ! ($year =~ m/$cyear/)) {
159     $year = "$year-$cyear";
160   }
161
162   $authors =~ s/[.,;\s]+$//s;
163
164   # List me as a co-author on all of them, since I'm the one who
165   # did the OSX port, packaged it up, and built the executables.
166   #
167   my $curator = "Jamie Zawinski";
168   if (! ($authors =~ m/$curator/si)) {
169     if ($authors =~ m@^(.*?),? and (.*)$@s) {
170       $authors = "$1, $2, and $curator";
171     } else {
172       $authors .= " and $curator";
173     }
174   }
175
176   my $desc1 = ("$name, version $vers.\n\n" .            # savername.xml
177                $desc . "\n" .
178                "\n" . 
179                "From the XScreenSaver collection: " .
180                "http://www.jwz.org/xscreensaver/\n" .
181                "Copyright \251 $year by $authors.\n");
182
183   my $desc2 = ("$name $vers,\n" .                       # Info.plist
184                "\302\251 $year $authors.\n" .
185                "From the XScreenSaver collection:\n" .
186                "http://www.jwz.org/xscreensaver/\n" .
187                "\n" .
188                $desc .
189                "\n");
190
191   # unwrap lines, but only when it's obviously ok: leave blank lines,
192   # and don't unwrap if that would compress leading whitespace on a line.
193   #
194   $desc2 =~ s/^(From |http:)/\n$1/gm;
195   1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
196   $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
197
198   $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
199
200   if ($obody eq $body && $was_compressed_p) {
201     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
202   } else {
203
204     # Gzip the XML.
205     my $body2 = '';
206     gzip (\$body, \$body2) || error ("$app_dir: xml gzip: $GzipError");
207     $body = $body2;
208
209     my $file_tmp = "$filename.tmp";
210     open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
211     print $out $body || error ("$file_tmp: $!");
212     close $out || error ("$file_tmp: $!");
213
214     if (!rename ("$file_tmp", "$filename")) {
215       unlink "$file_tmp";
216       error ("mv \"$file_tmp\" \"$filename\": $!");
217     }
218     print STDERR "$progname: wrote $filename\n" if ($verbose);
219   }
220
221   return ($desc1, $desc2);
222 }
223
224
225 sub compress_all_xml_files($) {
226   my ($dir) = @_;
227   opendir (my $dirp, $dir) || error ("$dir: $!");
228   my @files = readdir ($dirp);
229   closedir $dirp;
230   foreach my $f (sort @files) {
231     next unless ($f =~ m/\.xml$/si);
232     my $filename = "$dir/$f";
233     open (my $in, '<', $filename) || error ("$filename: $!");
234     print STDERR "$progname: read $filename\n" if ($verbose > 2);
235     local $/ = undef;  # read entire file
236     my $body = <$in>;
237     close $in;
238
239     if ($body =~ m/^<\?xml/s) {
240       my $body2 = '';
241       gzip (\$body, \$body2) || error ("$filename: xml gzip: $GzipError");
242       $body = $body2;
243       my $file_tmp = "$filename.tmp";
244       open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
245       print $out $body || error ("$file_tmp: $!");
246       close $out || error ("$file_tmp: $!");
247
248       if (!rename ("$file_tmp", "$filename")) {
249         unlink "$file_tmp";
250         error ("mv \"$file_tmp\" \"$filename\": $!");
251       }
252       print STDERR "$progname: compressed $filename\n" if ($verbose);
253     } elsif ($verbose > 2) {
254       print STDERR "$filename: already compressed\n";
255     }
256   }
257 }
258
259
260 sub set_plist_key($$$$) {
261   my ($filename, $body, $key, $val) = @_;
262
263   if ($body =~ m@^(.*
264                   \n\t<key>$key</key>
265                   \n\t<string>)([^<>]*)(</string>
266                   .*)$@xs) {
267 #    print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
268     $body = $1 . $val . $3;
269   } else {
270     error ("$filename: unparsable")
271       unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
272     $body = ($1 .
273              "\n\t<key>$key</key>" .
274              "\n\t<string>$val</string>" .
275              $2);
276   }
277
278   return $body;
279 }
280
281
282 sub set_icon($) {
283   my ($app_dir) = @_;
284   $app_dir =~ s@/+$@@s;
285
286   # "seticon" is from osxutils, http://osxutils.sourceforge.net/
287
288   my $icon = ($app_dir =~ m/\.saver$/ ? 'XScreenSaver' : 'SaverRunner');
289   $icon = "$app_dir/../../../$icon.icns";
290   my @cmd = ("seticon", "-d", $icon, $app_dir);
291   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
292     if ($verbose > 1);
293   system (@cmd);
294 }
295
296
297 sub set_thumb($) {
298   my ($app_dir) = @_;
299
300   return unless ($app_dir =~ m@\.saver/?$@s);
301
302   my @cmd = ("$exec_dir/update-thumbnail.pl", $thumbdir, $app_dir);
303   push @cmd, "-" . ("v" x $verbose) if ($verbose);
304   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
305     if ($verbose > 1);
306   system (@cmd);
307   my $exit  = $? >> 8;
308   exit ($exit) if $exit;
309 }
310
311
312 sub update($) {
313   my ($app_dir) = @_;
314
315   error ("$app_dir: no name") 
316     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
317   my $app_name = $1;
318
319   my ($filename, $plist) = read_info_plist ($app_dir);
320   my $oplist = $plist;
321
322   error ("$filename: no version number")
323     unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
324                         <string>([^<>]+)</string>@sx);
325   my $vers = $1;
326   my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
327
328   # No, don't do this -- the iOS version reads the XML file in a few
329   # different places, and most of those places don't understand gzip.
330
331   if ($app_name eq 'XScreenSaver') {
332     compress_all_xml_files ($app_dir);
333   } elsif (! defined($info_str)) {
334     print STDERR "$progname: $filename: no XML file\n" if ($verbose > 1);
335   } else {
336
337     $info_str =~ m@^([^\n]+)\n@s ||
338       error ("$filename: unparsable copyright");
339     my $copyright = "$1";
340     $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
341
342     # Lose the Wikipedia URLs.
343     $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
344
345     $info_str =~ s/(\n\n)\n+/$1/gs;
346     $info_str =~ s/(^\s+|\s+$)//gs;
347     $plist = set_plist_key ($filename, $plist, 
348                             "NSHumanReadableCopyright", $copyright);
349     $plist = set_plist_key ($filename, $plist,
350                             "CFBundleLongVersionString",$copyright);
351     $plist = set_plist_key ($filename, $plist,
352                             "CFBundleGetInfoString",    $info_str);
353
354     if ($oplist eq $plist) {
355       print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
356     } else {
357       $plist = convert_plist ($plist, 1);  # convert to binary plist
358       my $file_tmp = "$filename.tmp";
359       open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
360       print $out $plist || error ("$file_tmp: $!");
361       close $out || error ("$file_tmp: $!");
362
363       if (!rename ("$file_tmp", "$filename")) {
364         unlink "$file_tmp";
365         error ("mv \"$file_tmp\" \"$filename\": $!");
366       }
367       print STDERR "$progname: wrote $filename\n" if ($verbose);
368     }
369   }
370
371   set_icon ($app_dir);
372   set_thumb ($app_dir);
373 }
374
375
376 sub error($) {
377   my ($err) = @_;
378   print STDERR "$progname: $err\n";
379   exit 1;
380 }
381
382 sub usage() {
383   print STDERR "usage: $progname [--verbose] program.app ...\n";
384   exit 1;
385 }
386
387 sub main() {
388
389   my @files = ();
390   while ($_ = $ARGV[0]) {
391     shift @ARGV;
392     if    (m/^--?verbose$/s)  { $verbose++; }
393     elsif (m/^-v+$/)          { $verbose += length($_)-1; }
394     elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
395     elsif (m/^-/s)            { usage(); }
396     else                      { push @files, $_; }
397   }
398   usage() unless ($#files >= 0);
399   foreach (@files) {
400     update ($_);
401   }
402 }
403
404 main();
405 exit 0;