Class: Firebrew::Firefox::Extension::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/firebrew/firefox/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Manager

Returns a new instance of Manager.



13
14
15
# File 'lib/firebrew/firefox/extension.rb', line 13

def initialize(params={})
  @profile = params[:profile]
end

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



11
12
13
# File 'lib/firebrew/firefox/extension.rb', line 11

def profile
  @profile
end

Instance Method Details

#allObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/firebrew/firefox/extension.rb', line 17

def all
  profile_extensions = self.fetch['addons'].find_all do |extension|
    extension['location'] == 'app-profile'
  end
  
  profile_extensions.map do |extension|
    Extension.new({
      name: extension['defaultLocale']['name'],
      guid: extension['id'],
      version: extension['version'],
      uri: extension['descriptor'],
    }, self)
  end
end

#find(name) ⇒ Object



32
33
34
# File 'lib/firebrew/firefox/extension.rb', line 32

def find(name)
  self.all.find{|ext| ext.name == name }
end

#find!(name) ⇒ Object



36
37
38
39
40
# File 'lib/firebrew/firefox/extension.rb', line 36

def find!(name)
  result = self.find(name)
  raise Firebrew::ExtensionNotFoundError, "Extension not found: #{name}" if result.nil?
  result
end

#install(extension) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/firebrew/firefox/extension.rb', line 42

def install(extension)
  dir = File.join(self.profile.path, 'extensions')
  FileUtils.mkdir_p dir
  
  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

#uninstall(extension) ⇒ Object



77
78
79
80
81
# File 'lib/firebrew/firefox/extension.rb', line 77

def uninstall(extension)
  FileUtils.rm_rf extension.uri
  self.remove(extension)
  self.push
end