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