فهرست منبع

Fix & simplify Firefox extension disk layout

This was nice! We get to replace a non-core module (Archive::Extract)
with a core module (IO::Uncompress::Unzip) and instead of parsing XML
with regex, we're trying out XML::Simple. It's unfortunately not core,
but it doesn't have too many dependencies and hopefully our users
already have either or its deps already installed!

We weren't able to find any core XML modules :(
Daniel Gempesaw 10 سال پیش
والد
کامیت
86e20a4c1e
1فایلهای تغییر یافته به همراه11 افزوده شده و 21 حذف شده
  1. 11 21
      lib/Selenium/Firefox/Profile.pm

+ 11 - 21
lib/Selenium/Firefox/Profile.pm

@@ -6,15 +6,16 @@ use strict;
 use warnings;
 use warnings;
 
 
 use Archive::Zip qw( :ERROR_CODES );
 use Archive::Zip qw( :ERROR_CODES );
-use Archive::Extract;
 use Carp qw(croak);
 use Carp qw(croak);
 use Cwd qw(abs_path);
 use Cwd qw(abs_path);
 use File::Copy qw(copy);
 use File::Copy qw(copy);
 use File::Temp;
 use File::Temp;
 use File::Basename qw(dirname);
 use File::Basename qw(dirname);
+use IO::Uncompress::Unzip qw(unzip $UnzipError);
 use JSON qw/decode_json/;
 use JSON qw/decode_json/;
 use MIME::Base64;
 use MIME::Base64;
 use Scalar::Util qw(blessed looks_like_number);
 use Scalar::Util qw(blessed looks_like_number);
+use XML::Simple;
 
 
 =head1 DESCRIPTION
 =head1 DESCRIPTION
 
 
@@ -255,32 +256,21 @@ sub _install_extensions {
     mkdir $extension_dir unless -d $extension_dir;
     mkdir $extension_dir unless -d $extension_dir;
 
 
     # TODO: handle extensions that need to be unpacked
     # TODO: handle extensions that need to be unpacked
-    foreach (@{$self->{extensions}}) {
+    foreach my $xpi (@{$self->{extensions}}) {
         # For Firefox to recognize the extension, we have to put the
         # For Firefox to recognize the extension, we have to put the
         # .xpi in the /extensions/ folder and change the filename to
         # .xpi in the /extensions/ folder and change the filename to
         # its id, which is found in the install.rdf in the root of the
         # its id, which is found in the install.rdf in the root of the
         # zip.
         # zip.
-        my $ae = Archive::Extract->new(
-            archive => $_,
-            type => "zip"
-        );
-
-        $Archive::Extract::PREFER_BIN = 1;
-        my $tempDir = File::Temp->newdir();
-        $ae->extract( to => $tempDir );
-        my $install = $ae->extract_path();
-        $install .= '/install.rdf';
-
-        open (my $fh, "<", $install)
-            or croak "No install.rdf inside $_: $!";
-        my (@file) = <$fh>;
-        close ($fh);
 
 
-        my @name = grep { chomp; $_ =~ /<em:id>[^{]/ } @file;
-        $name[0] =~ s/.*<em:id>(.*)<\/em:id>.*/$1/;
+        my $fh;
+        unzip $xpi => \$fh, Name => "install.rdf"
+          or die "unzip failed: $UnzipError\n";
+
+        my $rdf = XMLin($fh);
+        my $name = $rdf->{Description}->{'em:id'};
 
 
-        my $xpi_dest = $extension_dir . $name[0] . ".xpi";
-        copy($_, $xpi_dest)
+        my $xpi_dest = $extension_dir . $name . ".xpi";
+        copy($xpi, $xpi_dest)
             or croak "Error copying $_ to $xpi_dest : $!";
             or croak "Error copying $_ to $xpi_dest : $!";
     }
     }
 }
 }