JAL-3420 JAL-3394 Using install4j8 template with file associations. Attempts unix...
[jalview.git] / utils / install4j / auto_file_associations.pl
index b46e274..7613778 100755 (executable)
@@ -2,11 +2,55 @@
 
 use strict;
 
+my $i4jversion = 7;
+if ($ARGV[0] eq "-v") {
+  shift @ARGV;
+  $i4jversion = shift @ARGV;
+  die("-v i4jversion must be an integer [probably 7 or 8]") unless $i4jversion =~ m/^\d+$/;
+}
+# backwards compatibility
+$i4jversion = "" if $i4jversion < 8;
+
 my $fileformats = $ARGV[0];
-$fileformats = "./src/jalview/io/FileFormats.java" unless $fileformats;
+$fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
+
+# default mimetype will be text/x-$shortname
+# TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
+# TODO: look up standard mime type used for BLASTfmt matrices, etc
+my $mimetypes = {
+  rnaml => "application/rnaml+xml",
+  biojson => "application/x-jalview-biojson+json",
+  jnet => "application/x-jalview-jnet+text",
+  features => "application/x-jalview-features+text",
+  scorematrix => "application/x-jalview-scorematrix+text",
+  pdb => "chemical/x-pdb",
+  mmcif => "chemical/x-cif",
+  mmcif2 => "chemical/x-mcif",
+  jalview => "application/x-jalview+xml+zip",
+  jvl => "application/x-jalview-jvl+text",
+  annotations => "application/x-jalview-annotations+text",
+};
+
+my @dontaddshortname = qw(features json);
+my @dontaddextension = qw(html xml json jar mfa fastq);
+my $add_associations = {
+  biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
+  gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
+  gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
+  features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
+  annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
+  mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
+  jvl => {shortname=>"jvl",name=>"Jalview Version Locator",extensions=>["jvl"],iconfile=>"Jalview-Version-Locator"},
+  jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
+  scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
+};
+my $add_extensions = {
+  blc => ["blc"],
+};
+my @put_first = qw(jalview jvl);
 
 my $mactemplatefile = "file_associations_template-Info_plist.xml";
-my $i4jtemplatefile = "file_associations_template-install4j.xml";
+my $i4jtemplatefile = ($i4jversion >= 8)?"file_associations_template-install4j${i4jversion}.xml";
 my $mactemplate;
 my $i4jtemplate;
 open(MT,"<$mactemplatefile") or dir("Could not open '$mactemplatefile' for reading");
@@ -33,49 +77,93 @@ open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
 
 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
 my $id = 10000;
+my $file_associations = {};
 while(my $line = <IN>) {
   $line =~ s/\s+/ /g;
   $line =~ s/(^ | $)//g;
   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
     my $shortname = lc($1);
+    next if (grep($_ eq $shortname, @dontaddshortname));
     my $name = $2;
     my $extensions = $3;
     $extensions =~ s/\s+//g;
-    my @extensions = split(m/,/,$extensions);
-    #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
-
-    my $macentry = $mactemplate;
-    my $i4jentry = $i4jtemplate;
-
-    $macentry =~ s/\$\$NAME\$\$/$name/g;
-    $macentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
-
-    $i4jentry =~ s/\$\$NAME\$\$/$name/g;
-    $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
-
-    while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
-      my $pre = $1;
-      my $post = $2;
-      my $macextensions;
-      for my $ext (@extensions) {
-        $macextensions .= $pre.$ext.$post;
-      }
-      $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
+    my @possextensions = split(m/,/,$extensions);
+    my @extensions;
+    my $addext = $add_extensions->{$shortname};
+    if (ref($addext) eq "ARRAY") {
+      push(@possextensions, @$addext);
+    }
+    for my $possext (@possextensions) {
+      next if grep($_ eq $possext, @extensions);
+      next if grep($_ eq $possext, @dontaddextension);
+      push(@extensions,$possext);
     }
-    print MA $macentry;
+    next unless scalar(@extensions);
+    $file_associations->{$shortname} = {
+      shortname => $shortname,
+      name => $name,
+      extensions => \@extensions
+    };
+    warn("Adding file association for $shortname\n");
+  }
+}
+close(IN);
 
-    for my $ext (@extensions) {
-      my $i4jextentry = $i4jentry;
-      $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
-      $i4jextentry =~ s/\$\$ID\$\$/$id/g;
-      $id++;
+my %all_associations = (%$file_associations, %$add_associations);
+
+for my $shortname (@put_first, sort keys %all_associations) {
+  my $a = $all_associations{$shortname};
+  if (ref($a) ne "HASH") {
+    next;
+  }
+
+  my $name = $a->{name};
+  my $extensions = $a->{extensions};
+  my $mimetype = $mimetypes->{$shortname};
+  $mimetype = "text/x-$shortname" unless $mimetype;
 
-      print IA $i4jextentry;
+  my $iconfile = $a->{iconfile};
+  $iconfile = "Jalview-File" unless $iconfile;
+
+  my @extensions = @$extensions;
+  #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
+
+  my $macentry = $mactemplate;
+  my $i4jentry = $i4jtemplate;
+
+  $macentry =~ s/\$\$NAME\$\$/$name/g;
+  $macentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
+  $macentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
+  $macentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
+
+  $i4jentry =~ s/\$\$NAME\$\$/$name/g;
+  $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
+  $i4jentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
+  $i4jentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
+
+  while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
+    my $pre = $1;
+    my $post = $2;
+    my $macextensions;
+    for my $ext (@extensions) {
+      $macextensions .= $pre.$ext.$post;
     }
+    $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
+  }
+  print MA $macentry;
+
+  for my $ext (@extensions) {
+    my $i4jextentry = $i4jentry;
+    $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
+    $i4jextentry =~ s/\$\$ID\$\$/$id/g;
+    $id++;
 
+    print IA $i4jextentry;
   }
+
+  delete $all_associations{$shortname};
 }
-close(IN);
+
 close(IA);
 print MA "</array>\n";
 close(MA);