JAL-4026 update the viewport’s wrapped width after calculating it
[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-mmcif",
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   mmcif => {shortname=>"mmcif",name=>"CIF",extensions=>["cif"]},
41   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
42   jvl => {shortname=>"jvl",name=>"Jalview Launch",extensions=>["jvl"],iconfile=>"jvl_file"},
43   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
44   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
45 };
46 my $add_extensions = {
47   blc => ["blc"],
48 };
49 my @put_first = qw(jalview jvl);
50
51 my @non_primary = qw(mmcif mmcif2 pdb);
52
53 my $v = ($i4jversion >= 8)?$i4jversion:"";
54 my $i4jtemplatefile = "file_associations_template-install4j${v}.xml";
55 my $i4jtemplate;
56 my $mactemplatefile = "file_associations_template-Info_plist.xml";
57 my $mactemplate;
58
59 open(MT,"<$mactemplatefile") or die("Could not open '$mactemplatefile' for reading");
60 while(<MT>){
61   $mactemplate .= $_;
62 }
63 close(MT);
64 open(IT,"<$i4jtemplatefile") or die("Could not open '$i4jtemplatefile' for reading");
65 while(<IT>){
66   $i4jtemplate .= $_;
67 }
68 close(IT);
69 my $macauto;
70 my $i4jauto;
71
72 my $macautofile = $mactemplatefile;
73 $macautofile =~ s/template/auto$1/;
74
75 my $i4jautofile = $i4jtemplatefile;
76 $i4jautofile =~ s/template/auto$1/;
77
78 for my $key (sort keys %$add_associations) {
79   my $a = $add_associations->{$key};
80   warn("Known file association for $a->{shortname} (".join(",",@{$a->{extensions}}).")\n");
81 }
82
83 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
84 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
85
86 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
87
88 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
89 my $id = 10000;
90 my $file_associations = {};
91 while(my $line = <IN>) {
92   $line =~ s/\s+/ /g;
93   $line =~ s/(^ | $)//g;
94   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
95     my $shortname = lc($1);
96     next if (grep($_ eq $shortname, @dontaddshortname));
97     my $name = $2;
98     my $extensions = $3;
99     $extensions =~ s/\s+//g;
100     my @possextensions = map(lc($_),split(m/,/,$extensions));
101     my @extensions;
102     my $addext = $add_extensions->{$shortname};
103     if (ref($addext) eq "ARRAY") {
104       push(@possextensions, @$addext);
105     }
106     for my $possext (@possextensions) {
107       next if grep($_ eq $possext, @extensions);
108       next if grep($_ eq $possext, @dontaddextension);
109       push(@extensions,$possext);
110     }
111     next unless scalar(@extensions);
112     $file_associations->{$shortname} = {
113       shortname => $shortname,
114       name => $name,
115       extensions => \@extensions
116     };
117     warn("Reading file association for $shortname (".join(",",@extensions).")\n");
118   }
119 }
120 close(IN);
121
122 my %all_associations = (%$file_associations, %$add_associations);
123
124 my @ordered = (@put_first, @non_primary);
125 for my $key (sort keys %all_associations) {
126   next if grep($_ eq $key, @ordered);
127   push(@ordered, $key);
128 }
129 my $num = $#ordered + 1;
130
131 warn("--\n");
132
133 my $i4jcount = 0;
134 for my $shortname (@ordered) {
135   my $a = $all_associations{$shortname};
136   next if (ref($a) ne "HASH");
137
138   my $name = $a->{name};
139   my $extensions = $a->{extensions};
140   my $mimetype = $mimetypes->{$shortname};
141   $mimetype = "application/x-$shortname+txt" unless $mimetype;
142
143   my $iconfile = $a->{iconfile};
144   $iconfile = "Jalview-File" unless $iconfile;
145
146   my $primary = (! grep($_ eq $shortname, @non_primary));
147   my $primarystring = $primary?"true":"false";
148   my $role = $primary?"Editor":"Viewer";
149
150   my @extensions = @$extensions;
151
152   my $xname = xml_escape($name);
153   my $xmimetype = xml_escape($mimetype);
154   my $xshortname = xml_escape($shortname);
155   my $xiconfile = xml_escape($iconfile);
156   my $xrole = xml_escape($role);
157   my $xROLE = xml_escape(uc($role));
158   my $xprimarystring = xml_escape($primarystring);
159
160   my $macentry = $mactemplate;
161   $macentry =~ s/\$\$NAME\$\$/$xname/g;
162   $macentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
163   $macentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
164   $macentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
165   $macentry =~ s/\$\$ROLE\$\$/$xrole/g;
166   $macentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
167   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
168     my $pre = $1;
169     my $post = $2;
170     my $macextensions;
171     for my $ext (@extensions) {
172       my $xext = xml_escape($ext);
173       $macextensions .= $pre.$xext.$post;
174     }
175     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
176   }
177   print MA $macentry;
178
179   my $i4jentry = $i4jtemplate;
180   $i4jentry =~ s/\$\$NAME\$\$/$xname/g;
181   $i4jentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
182   $i4jentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
183   $i4jentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
184   $i4jentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
185   $i4jentry =~ s/\$\$MACASSOCIATIONROLE\$\$/$xROLE/g;
186
187   my $ext = join(",",sort(@extensions));
188   my $xdisplayext = xml_escape(join(", ", map(".$_",sort(@extensions))));
189   my $progresspercent = int(($i4jcount/$num)*100);
190   $progresspercent = 100 if $progresspercent > 100;
191   $i4jcount++;
192   my $xext = xml_escape($ext);
193   my $addunixextension = "true";
194
195   $i4jentry =~ s/\$\$ADDUNIXEXTENSION\$\$/$addunixextension/g;
196   $i4jentry =~ s/\$\$EXTENSION\$\$/$xext/g;
197   $i4jentry =~ s/\$\$DISPLAYEXTENSION\$\$/$xdisplayext/g;
198   $i4jentry =~ s/\$\$PROGRESSPERCENT\$\$/$progresspercent/g;
199   $i4jentry =~ s/\$\$ID\$\$/$id/g;
200   $id++;
201   $i4jentry =~ s/\$\$ID1\$\$/$id/g;
202   $id++;
203   $i4jentry =~ s/\$\$ID2\$\$/$id/g;
204   $id++;
205
206   print IA $i4jentry;
207
208   delete $all_associations{$shortname};
209   warn("Writing entry for $name (".join(",",@$extensions).": $mimetype)\n");
210 }
211
212 close(IA);
213 print MA "</array>\n";
214 close(MA);
215
216 sub xml_escape {
217   my $x = shift;
218   # stolen from Pod::Simple::XMLOutStream in base distro
219   $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}a-zA-Z0-9])/'&#'.(ord($1)).';'/eg;
220   return $x;
221 }