http://www.jwz.org/xscreensaver/xscreensaver-5.07.tar.gz
[xscreensaver] / OSX / update-info-plist.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2006-2008 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
25 my $progname = $0; $progname =~ s@.*/@@g;
26 my $version = q{ $Revision: 1.9 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
27
28 my $verbose = 1;
29
30 sub read_info_plist($) {
31   my ($app_dir) = @_;
32   my $file = "$app_dir/Contents/Info.plist";
33   $file =~ s@/+@/@g;
34   local *IN;
35   my $body = '';
36   error ("$file: $!") unless open (IN, "<$file");
37   while (<IN>) { $body .= $_; }
38   close IN;
39   return ($file, $body);
40 }
41
42
43 sub read_saver_xml($) {
44   my ($app_dir) = @_;
45   error ("$app_dir: no name") 
46     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
47   my $name = lc($1);
48   my $file = "$app_dir/Contents/Resources/$name.xml";
49   $file =~ s@/+@/@g;
50   local *IN;
51   my $body = '';
52   error ("$file: $!") unless open (IN, "<$file");
53   while (<IN>) { $body .= $_; }
54   close IN;
55   return ($file, $body);
56 }
57
58
59 sub update_saver_xml($$) {
60   my ($app_dir, $vers) = @_;
61   my ($filename, $body) = read_saver_xml ($app_dir);
62   my $obody = $body;
63
64   $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
65     error ("$filename: no name label");
66   my $name = $1;
67
68   $body =~ m@<_description>(.*?)</_description>@s ||
69     error ("$filename: no description tag");
70   my $desc = $1;
71   $desc =~ s/^([ \t]*\n)+//s;
72   $desc =~ s/\s*$//s;
73
74   # in case it's done already...
75   $desc =~ s/^.* version \d[^\n]*\n//s;
76   $desc =~ s/^From the XScreenSaver.*\n//m;
77   $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
78   $desc =~ s/^Copyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/Written $3; $1./m;
79   $desc =~ s/^\n+//s;
80
81   error ("$filename: description contains bad characters")
82     if ($desc =~ m/([^\t\n -~]|[<>])/);
83
84   error ("$filename: can't extract authors")
85     unless ($desc =~ m@^(.*)\nWritten by[ \t]+([^\n]+)$@s);
86   $desc = $1;
87   my $authors = $2;
88   $desc =~ s/\s*$//s;
89
90   my $year = undef;
91   if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
92     $authors = $1;
93     $year = $2;
94   }
95
96   my $cyear = 1900 + ((localtime())[5]);
97   $year = "$cyear" unless $year;
98   if ($year && ! ($year =~ m/$cyear/)) {
99     $year = "$year-$cyear";
100   }
101
102   $authors =~ s/[.,;\s]+$//s;
103
104   # List me as a co-author on all of them, since I'm the one who
105   # did the OSX port, packaged it up, and built the executables.
106   #
107   my $curator = "Jamie Zawinski";
108   if (! ($authors =~ m/$curator/si)) {
109     if ($authors =~ m@^(.*),? and (.*)$@s) {
110       $authors = "$1, $2, and $curator";
111     } else {
112       $authors .= " and $curator";
113     }
114   }
115
116   my $desc1 = ("$name, version $vers.\n\n" .            # savername.xml
117                $desc . "\n" .
118                "\n" . 
119                "From the XScreenSaver collection: " .
120                "http://www.jwz.org/xscreensaver/\n" .
121                "Copyright \251 $year by $authors.\n");
122
123   my $desc2 = ("$name $vers,\n" .                       # Info.plist
124                "\302\251 $year $authors.\n" .
125                "From the XScreenSaver collection:\n" .
126                "http://www.jwz.org/xscreensaver/\n" .
127                "\n" .
128                $desc .
129                "\n");
130
131   # unwrap lines, but only when it's obviously ok: leave blank lines,
132   # and don't unwrap if that would compress leading whitespace on a line.
133   #
134   $desc2 =~ s/^(From |http:)/\n$1/gm;
135   1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
136   $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
137
138   $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
139
140   if ($obody eq $body) {
141     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
142   } else {
143     my $file_tmp = "$filename.tmp";
144     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
145     print OUT $body || error ("$file_tmp: $!");
146     close OUT || error ("$file_tmp: $!");
147
148     if (!rename ("$file_tmp", "$filename")) {
149       unlink "$file_tmp";
150       error ("mv \"$file_tmp\" \"$filename\": $!");
151     }
152     print STDERR "$progname: wrote $filename\n" if ($verbose);
153   }
154
155   return ($desc1, $desc2);
156 }
157
158
159
160 sub set_plist_key($$$$) {
161   my ($filename, $body, $key, $val) = @_;
162
163   if ($body =~ m@^(.*
164                   \n\t<key>$key</key>
165                   \n\t<string>)([^<>]*)(</string>
166                   .*)$@xs) {
167 #    print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
168     $body = $1 . $val . $3;
169   } else {
170     error ("$filename: unparsable")
171       unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
172     $body = ($1 .
173              "\n\t<key>$key</key>" .
174              "\n\t<string>$val</string>" .
175              $2);
176   }
177
178   return $body;
179 }
180
181
182 sub set_icon($) {
183   my ($app_dir) = @_;
184   $app_dir =~ s@/+$@@s;
185
186   # "seticon" is from osxutils, http://osxutils.sourceforge.net/
187
188   my $icon = "$app_dir/../../../XScreenSaver.icns";
189   my @cmd = ("seticon", "-d", $icon, $app_dir);
190   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
191     if ($verbose > 1);
192   system (@cmd);
193 }
194
195
196 sub update($) {
197   my ($app_dir) = @_;
198
199   error ("$app_dir: no name") 
200     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
201   my $app_name = $1;
202
203   my ($filename, $plist) = read_info_plist ($app_dir);
204   my $oplist = $plist;
205
206   error ("$filename: no version number")
207     unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
208                         <string>([^<>]+)</string>@sx);
209   my $vers = $1;
210   my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
211
212   $info_str =~ m@^([^\n]+)\n@s ||
213     error ("$filename: unparsable copyright");
214   my $copyright = "$1";
215   $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
216
217   # Lose the Wikipedia URLs.
218   $info_str =~ s@http:.*\bwikipedia\b[^\s]+[ \t]*\n?@@gm;
219
220   $info_str =~ s/(\n\n)\n+/$1/gs;
221   $info_str =~ s/(^\s+|\s+$)//gs;
222   $plist = set_plist_key ($filename, $plist, 
223                           "NSHumanReadableCopyright", $copyright);
224   $plist = set_plist_key ($filename, $plist,
225                           "CFBundleLongVersionString",$copyright);
226   $plist = set_plist_key ($filename, $plist,
227                           "CFBundleGetInfoString",    $info_str);
228
229   if ($oplist eq $plist) {
230     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
231   } else {
232     my $file_tmp = "$filename.tmp";
233     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
234     print OUT $plist || error ("$file_tmp: $!");
235     close OUT || error ("$file_tmp: $!");
236
237     if (!rename ("$file_tmp", "$filename")) {
238       unlink "$file_tmp";
239       error ("mv \"$file_tmp\" \"$filename\": $!");
240     }
241     print STDERR "$progname: wrote $filename\n" if ($verbose);
242   }
243
244   set_icon ($app_dir);
245 }
246
247
248 sub error($) {
249   my ($err) = @_;
250   print STDERR "$progname: $err\n";
251   exit 1;
252 }
253
254 sub usage() {
255   print STDERR "usage: $progname [--verbose] program.app ...\n";
256   exit 1;
257 }
258
259 sub main() {
260   my @files = ();
261   while ($#ARGV >= 0) {
262     $_ = shift @ARGV;
263     if ($_ eq "--verbose") { $verbose++; }
264     elsif (m/^-v+$/) { $verbose += length($_)-1; }
265     elsif (m/^-./) { usage; }
266     else { push @files, $_; }
267   }
268   usage() unless ($#files >= 0);
269   foreach (@files) {
270     update ($_);
271   }
272 }
273
274 main();
275 exit 0;