2 # Copyright © 2006-2014 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) = ('$Revision: 1.38 $' =~ m/\s(\d[.\d]+)\s/s);
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) ||
48 error ("pipe: $cmd[0]: $!");
49 error ("$cmd[0]: $!") unless $pid;
52 local $/ = undef; # read entire file
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);
69 sub read_info_plist($) {
71 my $file = "$app_dir/Contents/Info.plist";
72 my $file2 = "$app_dir/Info.plist";
75 if (open ($in, '<', $file)) {
76 } elsif (open ($in, '<', $file2)) {
81 print STDERR "$progname: read $file\n" if ($verbose > 2);
82 local $/ = undef; # read entire file
86 $body = convert_plist ($body, 0); # convert to xml plist
87 return ($file, $body);
91 sub read_saver_xml($) {
93 error ("$app_dir: no name")
94 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
97 return () if ($name eq 'XScreenSaver');
98 return () if ($name eq 'SaverTester');
99 return () if ($name eq 'XScreenSaverUpdater');
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/" .
107 if (open ($in, '<', $file)) {
108 } elsif (open ($in, '<', $file2)) { $file = $file2;
109 } elsif (open ($in, '<', $file3)) { $file = $file3;
113 print STDERR "$progname: read $file\n" if ($verbose > 2);
114 local $/ = undef; # read entire file
118 # Uncompress the XML if it is compressed.
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);
126 sub update_saver_xml($$) {
127 my ($app_dir, $vers) = @_;
128 my ($filename, $body, $was_compressed_p) = read_saver_xml ($app_dir);
131 return () unless defined ($filename);
133 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
134 error ("$filename: no name label");
137 $body =~ m@<_description>(.*?)</_description>@s ||
138 error ("$filename: no description tag");
140 $desc =~ s/^([ \t]*\n)+//s;
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;
149 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
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);
160 error ("$filename: can't extract authors")
161 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
167 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
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";
179 $authors =~ s/[.,;\s]+$//s;
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.
184 my $curator = "Jamie Zawinski";
185 if (! ($authors =~ m/$curator/si)) {
186 if ($authors =~ m@^(.*?),? and (.*)$@s) {
187 $authors = "$1, $2, and $curator";
189 $authors .= " and $curator";
193 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
196 "From the XScreenSaver collection: " .
197 "http://www.jwz.org/xscreensaver/\n" .
198 "Copyright \302\251 $year by $authors.\n");
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" .
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.
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;
215 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
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;
221 if ($obody eq $body && $was_compressed_p) {
222 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
227 gzip (\$body, \$body2) || error ("$app_dir: xml gzip: $GzipError");
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: $!");
235 if (!rename ("$file_tmp", "$filename")) {
237 error ("mv \"$file_tmp\" \"$filename\": $!");
239 print STDERR "$progname: wrote $filename\n" if ($verbose);
242 return ($desc1, $desc2);
246 sub compress_all_xml_files($) {
248 opendir (my $dirp, $dir) || error ("$dir: $!");
249 my @files = readdir ($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
260 if ($body =~ m/^<\?xml/s) {
262 gzip (\$body, \$body2) || error ("$filename: xml gzip: $GzipError");
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: $!");
269 if (!rename ("$file_tmp", "$filename")) {
271 error ("mv \"$file_tmp\" \"$filename\": $!");
273 print STDERR "$progname: compressed $filename\n" if ($verbose);
274 } elsif ($verbose > 2) {
275 print STDERR "$filename: already compressed\n";
281 sub set_plist_key($$$$) {
282 my ($filename, $body, $key, $val) = @_;
286 \n\t<string>)([^<>]*)(</string>
288 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
289 $body = $1 . $val . $3;
291 error ("$filename: unparsable")
292 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
294 "\n\t<key>$key</key>" .
295 "\n\t<string>$val</string>" .
305 $app_dir =~ s@/+$@@s;
307 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
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"
321 return unless ($app_dir =~ m@\.saver/?$@s);
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"
329 exit ($exit) if $exit;
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"
345 exit ($exit) if $exit;
349 sub fix_coretext($) {
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
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";
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);
372 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
376 exit ($exit) if $exit;
383 error ("$app_dir: no name")
384 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
387 my ($filename, $plist) = read_info_plist ($app_dir);
390 error ("$filename: no version number")
391 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
392 <string>([^<>]+)</string>@sx);
394 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
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.
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);
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/;
410 # Lose the Wikipedia URLs.
411 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
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);
422 if ($oplist eq $plist) {
423 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
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: $!");
431 if (!rename ("$file_tmp", "$filename")) {
433 error ("mv \"$file_tmp\" \"$filename\": $!");
435 print STDERR "$progname: wrote $filename\n" if ($verbose);
440 set_thumb ($app_dir);
441 # enable_gc ($app_dir);
442 fix_coretext ($app_dir)
448 print STDERR "$progname: $err\n";
453 print STDERR "usage: $progname [--verbose] program.app ...\n";
460 while ($_ = $ARGV[0]) {
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, $_; }
468 usage() unless ($#files >= 0);