JAL-3449 JAL-3394 merged on install4j8 template
[jalview.git] / utils / install4j / auto_file_associations.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $i4jversion = 7;
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 # backwards compatibility
12 $i4jversion = "" if $i4jversion < 8;
13
14 my $fileformats = $ARGV[0];
15 $fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
16
17 # default mimetype will be text/x-$shortname
18 # TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
19 # TODO: look up standard mime type used for BLASTfmt matrices, etc
20 my $mimetypes = {
21   rnaml => "application/rnaml+xml",
22   biojson => "application/x-jalview-biojson+json",
23   jnet => "application/x-jalview-jnet+text",
24   features => "application/x-jalview-features+text",
25   scorematrix => "application/x-jalview-scorematrix+text",
26   pdb => "chemical/x-pdb",
27   mmcif => "chemical/x-cif",
28   mmcif2 => "chemical/x-mcif",
29   jalview => "application/x-jalview+xml+zip",
30   jvl => "application/x-jalview-jvl+text",
31   annotations => "application/x-jalview-annotations+text",
32 };
33
34 my @dontaddshortname = qw(features json);
35 my @dontaddextension = qw(html xml json jar mfa fastq);
36 my $add_associations = {
37   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
38   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
39   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
40   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
41   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
42   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
43   jvl => {shortname=>"jvl",name=>"Jalview Version Locator",extensions=>["jvl"],iconfile=>"Jalview-Version-Locator"},
44   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
45   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
46 };
47 my $add_extensions = {
48   blc => ["blc"],
49 };
50 my @put_first = qw(jalview jvl);
51
52 my $mactemplatefile = "file_associations_template-Info_plist.xml";
53 my $i4jtemplatefile = ($i4jversion >= 8)?"file_associations_template-install4j${i4jversion}.xml";
54 my $mactemplate;
55 my $i4jtemplate;
56 open(MT,"<$mactemplatefile") or dir("Could not open '$mactemplatefile' for reading");
57 while(<MT>){
58   $mactemplate .= $_;
59 }
60 close(MT);
61 open(IT,"<$i4jtemplatefile") or dir("Could not open '$i4jtemplatefile' for reading");
62 while(<IT>){
63   $i4jtemplate .= $_;
64 }
65 close(IT);
66 my $macauto;
67 my $i4jauto;
68
69 my $macautofile = $mactemplatefile;
70 $macautofile =~ s/template/auto$1/;
71 my $i4jautofile = $i4jtemplatefile;
72 $i4jautofile =~ s/template/auto$1/;
73
74 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
75 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
76 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
77
78 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
79 my $id = 10000;
80 my $file_associations = {};
81 while(my $line = <IN>) {
82   $line =~ s/\s+/ /g;
83   $line =~ s/(^ | $)//g;
84   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
85     my $shortname = lc($1);
86     next if (grep($_ eq $shortname, @dontaddshortname));
87     my $name = $2;
88     my $extensions = $3;
89     $extensions =~ s/\s+//g;
90     my @possextensions = split(m/,/,$extensions);
91     my @extensions;
92     my $addext = $add_extensions->{$shortname};
93     if (ref($addext) eq "ARRAY") {
94       push(@possextensions, @$addext);
95     }
96     for my $possext (@possextensions) {
97       next if grep($_ eq $possext, @extensions);
98       next if grep($_ eq $possext, @dontaddextension);
99       push(@extensions,$possext);
100     }
101     next unless scalar(@extensions);
102     $file_associations->{$shortname} = {
103       shortname => $shortname,
104       name => $name,
105       extensions => \@extensions
106     };
107     warn("Adding file association for $shortname\n");
108   }
109 }
110 close(IN);
111
112 my %all_associations = (%$file_associations, %$add_associations);
113
114 for my $shortname (@put_first, sort keys %all_associations) {
115   my $a = $all_associations{$shortname};
116   if (ref($a) ne "HASH") {
117     next;
118   }
119
120   my $name = $a->{name};
121   my $extensions = $a->{extensions};
122   my $mimetype = $mimetypes->{$shortname};
123   $mimetype = "text/x-$shortname" unless $mimetype;
124
125   my $iconfile = $a->{iconfile};
126   $iconfile = "Jalview-File" unless $iconfile;
127
128   my @extensions = @$extensions;
129   #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
130
131   my $macentry = $mactemplate;
132   my $i4jentry = $i4jtemplate;
133
134   $macentry =~ s/\$\$NAME\$\$/$name/g;
135   $macentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
136   $macentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
137   $macentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
138
139   $i4jentry =~ s/\$\$NAME\$\$/$name/g;
140   $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
141   $i4jentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
142   $i4jentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
143
144   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
145     my $pre = $1;
146     my $post = $2;
147     my $macextensions;
148     for my $ext (@extensions) {
149       $macextensions .= $pre.$ext.$post;
150     }
151     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
152   }
153   print MA $macentry;
154
155   for my $ext (@extensions) {
156     my $i4jextentry = $i4jentry;
157     $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
158     $i4jextentry =~ s/\$\$ID\$\$/$id/g;
159     $id++;
160
161     print IA $i4jextentry;
162   }
163
164   delete $all_associations{$shortname};
165 }
166
167 close(IA);
168 print MA "</array>\n";
169 close(MA);