f3ae48283dbf024f40c493e4032ba3b002296e84
[xscreensaver] / OSX / update-info-plist.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2006 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;
23 use strict;
24
25 my $progname = $0; $progname =~ s@.*/@@g;
26 my $version = q{ $Revision: 1.6 $ }; $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 $cc = "\302\251";  # unicode "&copy;"
117
118   my $desc1 = ("$name, version $vers.\n\n" .
119                $desc . "\n" .
120                "\n" . 
121                "From the XScreenSaver collection:\n" .
122                "http://www.jwz.org/xscreensaver/\n" .
123                "Copyright \251 $year by $authors.\n");
124
125   my $desc2 = ("$name $vers,\n" .
126                "$cc $year $authors.\n" .
127                "From the XScreenSaver collection:\n" .
128                "http://www.jwz.org/xscreensaver/\n" .
129                "\n" .
130                $desc .
131                "\n");
132
133   # unwrap lines, but only when it's obviously ok: leave blank lines,
134   # and don't unwrap if that would compress leading whitespace on a line.
135   #
136   $desc2 =~ s/^(From |http:)/\n$1/gm;
137   1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
138   $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
139
140   $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
141
142   if ($obody eq $body) {
143     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
144   } else {
145     my $file_tmp = "$filename.tmp";
146     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
147     print OUT $body || error ("$file_tmp: $!");
148     close OUT || error ("$file_tmp: $!");
149
150     if (!rename ("$file_tmp", "$filename")) {
151       unlink "$file_tmp";
152       error ("mv \"$file_tmp\" \"$filename\": $!");
153     }
154     print STDERR "$progname: wrote $filename\n" if ($verbose);
155   }
156
157   return ($desc1, $desc2);
158 }
159
160
161
162 sub set_plist_key($$$$) {
163   my ($filename, $body, $key, $val) = @_;
164
165   if ($body =~ m@^(.*
166                   \n\t<key>$key</key>
167                   \n\t<string>)([^<>]*)(</string>
168                   .*)$@xs) {
169 #    print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
170     $body = $1 . $val . $3;
171   } else {
172     error ("$filename: unparsable")
173       unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
174     $body = ($1 .
175              "\n\t<key>$key</key>" .
176              "\n\t<string>$val</string>" .
177              $2);
178   }
179
180   return $body;
181 }
182
183
184 sub set_icon($) {
185   my ($app_dir) = @_;
186   $app_dir =~ s@/+$@@s;
187
188   # "seticon" is from osxutils, http://osxutils.sourceforge.net/
189
190   my $icon = "$app_dir/../../../XScreenSaver.icns";
191   my @cmd = ("seticon", "-d", $icon, $app_dir);
192   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
193     if ($verbose > 1);
194   system (@cmd);
195 }
196
197
198 sub update($) {
199   my ($app_dir) = @_;
200
201   error ("$app_dir: no name") 
202     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
203   my $app_name = $1;
204
205   my ($filename, $plist) = read_info_plist ($app_dir);
206   my $oplist = $plist;
207
208   error ("$filename: no version number")
209     unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
210                         <string>([^<>]+)</string>@sx);
211   my $vers = $1;
212   my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
213
214   $info_str =~ m@^([^\n]+)\n@s ||
215     error ("$filename: unparsable copyright");
216   my $copyright = "$1";
217   $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
218
219   $plist = set_plist_key ($filename, $plist, 
220                           "NSHumanReadableCopyright", $copyright);
221   $plist = set_plist_key ($filename, $plist,
222                           "CFBundleLongVersionString",$copyright);
223   $plist = set_plist_key ($filename, $plist,
224                           "CFBundleGetInfoString",    $info_str);
225
226   if ($oplist eq $plist) {
227     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
228   } else {
229     my $file_tmp = "$filename.tmp";
230     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
231     print OUT $plist || error ("$file_tmp: $!");
232     close OUT || error ("$file_tmp: $!");
233
234     if (!rename ("$file_tmp", "$filename")) {
235       unlink "$file_tmp";
236       error ("mv \"$file_tmp\" \"$filename\": $!");
237     }
238     print STDERR "$progname: wrote $filename\n" if ($verbose);
239   }
240
241   set_icon ($app_dir);
242 }
243
244
245 sub error($) {
246   my ($err) = @_;
247   print STDERR "$progname: $err\n";
248   exit 1;
249 }
250
251 sub usage() {
252   print STDERR "usage: $progname [--verbose] program.app ...\n";
253   exit 1;
254 }
255
256 sub main() {
257   my @files = ();
258   while ($#ARGV >= 0) {
259     $_ = shift @ARGV;
260     if ($_ eq "--verbose") { $verbose++; }
261     elsif (m/^-v+$/) { $verbose += length($_)-1; }
262     elsif (m/^-./) { usage; }
263     else { push @files, $_; }
264   }
265   usage() unless ($#files >= 0);
266   foreach (@files) {
267     update ($_);
268   }
269 }
270
271 main();
272 exit 0;