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



15
16
17
# File 'lib/firebrew/firefox/extension.rb', line 15

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

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



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

def profile
  @profile
end

Instance Method Details

#allObject



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

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



34
35
36
# File 'lib/firebrew/firefox/extension.rb', line 34

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

#find!(name) ⇒ Object



38
39
40
41
42
# File 'lib/firebrew/firefox/extension.rb', line 38

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

#install(extension, is_displaying_progress = false) ⇒ Object



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
76
77
78
79
80
81
82
83
84
# File 'lib/firebrew/firefox/extension.rb', line 44

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)
  
  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)
    
    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
    
    xpi.close
    FileUtils.rm_rf(xpi_path)
  else
    extension.uri = xpi_path
    xpi.close
  end
  
  self.add(extension)
  self.push
end

#uninstall(extension) ⇒ Object



86
87
88
89
90
# File 'lib/firebrew/firefox/extension.rb', line 86

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