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