lib/firebrew/firefox/extension.rb in firebrew-0.2.0 vs lib/firebrew/firefox/extension.rb in firebrew-0.3.0

- old
+ new

@@ -1,11 +1,13 @@ require 'fileutils' require 'open-uri' require 'json' require 'rexml/document' +require 'rake/pathmap' require 'zip' require 'firebrew/firefox/basic_extension' +require 'firebrew/downloader' module Firebrew::Firefox class Extension < BasicExtension class Manager attr_reader :profile @@ -37,46 +39,53 @@ result = self.find(name) raise Firebrew::ExtensionNotFoundError, "Extension not found: #{name}" if result.nil? result end - def install(extension) + def install(extension, is_displaying_progress = false) dir = File.join(self.profile.path, 'extensions') FileUtils.mkdir_p dir + xpi_path = '%s.xpi' % File.join(dir, extension.guid) - open([extension.uri].flatten.first, 'rb') do |r| - xpi = Zip::File.open(r) - install_manifests = xpi.find_entry('install.rdf') - install_manifests = install_manifests.get_input_stream.read - install_manifests = REXML::Document.new(install_manifests) - is_unpacking = REXML::XPath.match(install_manifests, '/RDF/Description/em:unpack/text()').first - is_unpacking = is_unpacking.nil? ? false : is_unpacking.value.strip == 'true' + File.open(xpi_path,'wb') do |out| + uri = [extension.uri].flatten.first + pout = is_displaying_progress ? $stdout : nil + Firebrew::Downloader.new(uri, out, output: pout).exec + end + + xpi = Zip::File.open(File.open(xpi_path,'rb')) + install_manifests = xpi.find_entry('install.rdf') + install_manifests = install_manifests.get_input_stream.read + install_manifests = REXML::Document.new(install_manifests) + is_unpacking = REXML::XPath.match(install_manifests, '/RDF/Description/em:unpack/text()').first + is_unpacking = is_unpacking.nil? ? false : is_unpacking.value.strip == 'true' + + if is_unpacking then + extension.uri = xpi_path.pathmap('%X') + FileUtils.mkdir_p(extension.uri) - if is_unpacking then - extension.uri = File.join(dir, extension.guid) - FileUtils.mkdir_p(extension.uri) - xpi.each do |entry| - next if entry.ftype == :directory - content = entry.get_input_stream.read - Dir.chdir(extension.uri) do - FileUtils.mkdir_p File.dirname(entry.name) - File.write(entry.name, content) - end + xpi.each do |entry| + next if entry.ftype == :directory + content = entry.get_input_stream.read + Dir.chdir(extension.uri) do + FileUtils.mkdir_p File.dirname(entry.name) + File.write(entry.name, content) end - else - extension.uri = '%s.xpi' % File.join(dir, extension.guid) - open(extension.uri, 'wb') do |w| - w.write r.read - end end + + xpi.close + FileUtils.rm_rf(xpi_path) + else + extension.uri = xpi_path + xpi.close end self.add(extension) self.push end def uninstall(extension) - FileUtils.rm_rf extension.uri + FileUtils.rm_r extension.uri self.remove(extension) self.push end protected