JAL-3394 improvements to the file association progress bar
[jalview.git] / utils / install4j / auto_file_associations-i4j8.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $i4jversion = 8;
6 if ($ARGV[0] eq "-v") {
7   shift @ARGV;
8   $i4jversion = shift @ARGV;
9   die("-v i4jversion must be an integer [probably 7 or 8]") unless $i4jversion =~ m/^\d+$/;
10 }
11
12 my $fileformats = $ARGV[0];
13 $fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
14
15 # default mimetype will be text/x-$shortname
16 # TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
17 # TODO: look up standard mime type used for BLASTfmt matrices, etc
18 my $mimetypes = {
19   rnaml => "application/rnaml+xml",
20   biojson => "application/x-jalview-biojson+json",
21   jnet => "application/x-jalview-jnet+text",
22   features => "application/x-jalview-features+text",
23   scorematrix => "application/x-jalview-scorematrix+text",
24   pdb => "chemical/x-pdb",
25   mmcif => "chemical/x-cif",
26   mmcif2 => "chemical/x-mcif",
27   jalview => "application/x-jalview+xml+zip",
28   jvl => "application/x-jalview-jvl+text",
29   annotations => "application/x-jalview-annotations+text",
30 };
31
32 my @dontaddshortname = qw(features json);
33 my @dontaddextension = qw(html xml json jar mfa fastq);
34 my $add_associations = {
35   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
36   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
37   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
38   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
39   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
40   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
41   jvl => {shortname=>"jvl",name=>"Jalview Launch",extensions=>["jvl"],iconfile=>"Jalview-Launch"},
42   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
43   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
44 };
45 my $add_extensions = {
46   blc => ["blc"],
47 };
48 my @put_first = qw(jalview jvl);
49
50 my @non_primary = qw(mmcif mmcif2 pdb);
51
52 my $v = ($i4jversion >= 8)?$i4jversion:"";
53 my $i4jtemplatefile = "file_associations_template-install4j${v}.xml";
54 my $i4jtemplate;
55 my $mactemplatefile = "file_associations_template-Info_plist.xml";
56 my $mactemplate;
57
58 open(MT,"<$mactemplatefile") or die("Could not open '$mactemplatefile' for reading");
59 while(<MT>){
60   $mactemplate .= $_;
61 }
62 close(MT);
63 open(IT,"<$i4jtemplatefile") or die("Could not open '$i4jtemplatefile' for reading");
64 while(<IT>){
65   $i4jtemplate .= $_;
66 }
67 close(IT);
68 my $macauto;
69 my $i4jauto;
70
71 my $macautofile = $mactemplatefile;
72 $macautofile =~ s/template/auto$1/;
73
74 my $i4jautofile = $i4jtemplatefile;
75 $i4jautofile =~ s/template/auto$1/;
76
77 for my $key (sort keys %$add_associations) {
78   my $a = $add_associations->{$key};
79   warn("Known file association for $a->{shortname} (".join(",",@{$a->{extensions}}).")\n");
80 }
81
82 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
83 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
84
85 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
86
87 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
88 my $id = 10000;
89 my $file_associations = {};
90 while(my $line = <IN>) {
91   $line =~ s/\s+/ /g;
92   $line =~ s/(^ | $)//g;
93   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
94     my $shortname = lc($1);
95     next if (grep($_ eq $shortname, @dontaddshortname));
96     my $name = $2;
97     my $extensions = $3;
98     $extensions =~ s/\s+//g;
99     my @possextensions = map(lc($_),split(m/,/,$extensions));
100     my @extensions;
101     my $addext = $add_extensions->{$shortname};
102     if (ref($addext) eq "ARRAY") {
103       push(@possextensions, @$addext);
104     }
105     for my $possext (@possextensions) {
106       next if grep($_ eq $possext, @extensions);
107       next if grep($_ eq $possext, @dontaddextension);
108       push(@extensions,$possext);
109     }
110     next unless scalar(@extensions);
111     $file_associations->{$shortname} = {
112       shortname => $shortname,
113       name => $name,
114       extensions => \@extensions
115     };
116     warn("Reading file association for $shortname (".join(",",@extensions).")\n");
117   }
118 }
119 close(IN);
120
121 my %all_associations = (%$file_associations, %$add_associations);
122
123 my @ordered = (@put_first, @non_primary);
124 for my $key (sort keys %all_associations) {
125   next if grep($_ eq $key, @ordered);
126   push(@ordered, $key);
127 }
128 my $num = $#ordered + 1;
129
130 warn("--\n");
131
132 my $i4jcount = 0;
133 for my $shortname (@ordered) {
134   my $a = $all_associations{$shortname};
135   next if (ref($a) ne "HASH");
136
137   my $name = $a->{name};
138   my $extensions = $a->{extensions};
139   my $mimetype = $mimetypes->{$shortname};
140   $mimetype = "application/x-$shortname+txt" unless $mimetype;
141
142   my $iconfile = $a->{iconfile};
143   $iconfile = "Jalview-File" unless $iconfile;
144
145   my $primary = (! grep($_ eq $shortname, @non_primary));
146   my $primarystring = $primary?"true":"false";
147   my $role = $primary?"Editor":"Viewer";
148
149   my @extensions = @$extensions;
150
151   my $xname = xml_escape($name);
152   my $xmimetype = xml_escape($mimetype);
153   my $xshortname = xml_escape($shortname);
154   my $xiconfile = xml_escape($iconfile);
155   my $xrole = xml_escape($role);
156   my $xprimarystring = xml_escape($primarystring);
157
158   my $macentry = $mactemplate;
159   $macentry =~ s/\$\$NAME\$\$/$xname/g;
160   $macentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
161   $macentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
162   $macentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
163   $macentry =~ s/\$\$ROLE\$\$/$xrole/g;
164   $macentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
165   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
166     my $pre = $1;
167     my $post = $2;
168     my $macextensions;
169     for my $ext (@extensions) {
170       my $xext = xml_escape($ext);
171       $macextensions .= $pre.$xext.$post;
172     }
173     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
174   }
175   print MA $macentry;
176
177   my $i4jentry = $i4jtemplate;
178   $i4jentry =~ s/\$\$NAME\$\$/$xname/g;
179   $i4jentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
180   $i4jentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
181   $i4jentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
182   $i4jentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
183
184   my $ext = join(",",sort(@extensions));
185   my $xdisplayext = xml_escape(join(", ", map(".$_",sort(@extensions))));
186   my $progresspercent = int(($i4jcount/$num)*100);
187   $progresspercent = 100 if $progresspercent > 100;
188   $i4jcount++;
189   my $xext = xml_escape($ext);
190   my $addunixextension = "true";
191
192   $i4jentry =~ s/\$\$ADDUNIXEXTENSION\$\$/$addunixextension/g;
193   $i4jentry =~ s/\$\$EXTENSION\$\$/$xext/g;
194   $i4jentry =~ s/\$\$DISPLAYEXTENSION\$\$/$xdisplayext/g;
195   $i4jentry =~ s/\$\$PROGRESSPERCENT\$\$/$progresspercent/g;
196   $i4jentry =~ s/\$\$ID\$\$/$id/g;
197   $id++;
198   $i4jentry =~ s/\$\$ID1\$\$/$id/g;
199   $id++;
200   $i4jentry =~ s/\$\$ID2\$\$/$id/g;
201   $id++;
202
203   print IA $i4jentry;
204
205   delete $all_associations{$shortname};
206   warn("Writing entry for $name (".join(",",@$extensions).": $mimetype)\n");
207 }
208
209 close(IA);
210 print MA "</array>\n";
211 close(MA);
212
213 sub xml_escape {
214   my $x = shift;
215   # stolen from Pod::Simple::XMLOutStream in base distro
216   $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(ord($1)).';'/eg;
217   return $x;
218 }