lib/firebrew/firefox/extension.rb in firebrew-0.1.2 vs lib/firebrew/firefox/extension.rb in firebrew-0.1.3
- old
+ new
@@ -1,8 +1,10 @@
require 'fileutils'
require 'open-uri'
require 'json'
+require 'zip'
+require 'rexml/document'
require 'firebrew/firefox/basic_extension'
module Firebrew::Firefox
class Extension < BasicExtension
class Manager
@@ -20,11 +22,11 @@
profile_extensions.map do |extension|
Extension.new({
name: extension['defaultLocale']['name'],
guid: extension['id'],
version: extension['version'],
- uri: '%s.xpi' % File.join(self.profile.path, 'extensions', extension['id'])
+ uri: extension['descriptor'],
}, self)
end
end
def find(name)
@@ -38,24 +40,44 @@
end
def install(extension)
dir = File.join(self.profile.path, 'extensions')
FileUtils.mkdir_p dir
- install_path = '%s.xpi' % File.join(dir, extension.guid)
- open([extension.uri].flatten.first, 'rb') do |i|
- open(install_path, 'wb') do |o|
- o.write i.read
+ 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'
+
+ 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
+ end
+ else
+ extension.uri = '%s.xpi' % File.join(dir, extension.guid)
+ open(extension.uri, 'wb') do |w|
+ w.write r.read
+ end
end
end
self.add(extension)
self.push
end
def uninstall(extension)
- FileUtils.rm_f extension.uri
+ FileUtils.rm_rf extension.uri
self.remove(extension)
self.push
end
protected
@@ -81,9 +103,10 @@
def add(extension)
self.fetch['addons'].push(
'id'=> extension.guid,
'location'=> 'app-profile',
'version'=> extension.version,
+ 'descriptor'=> extension.uri,
'defaultLocale'=> {
'name'=> extension.name
}
)
end