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