Class: Firefox::Installer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(os, language = nil, version = nil, opts = {}) ⇒ Installer

Returns a new instance of Installer.



9
10
11
12
13
14
15
16
17
18
# File 'lib/firefox/installer.rb', line 9

def initialize os, language = nil, version = nil, opts = {}
  # set dmg if specified
  if opts[:dmg]
    @dmg_file = opts.delete(:dmg) 
    raise "#{@dmg_file} not found" unless File.exist?(@dmg_file)
  end
  @os = os
  @lang = language || 'en-US'
  @version = version || get_current_version
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



7
8
9
# File 'lib/firefox/installer.rb', line 7

def lang
  @lang
end

#osObject (readonly)

Returns the value of attribute os.



7
8
9
# File 'lib/firefox/installer.rb', line 7

def os
  @os
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/firefox/installer.rb', line 7

def version
  @version
end

Instance Method Details

#get_current_versionObject



61
62
63
64
# File 'lib/firefox/installer.rb', line 61

def get_current_version
  page = open("http://www.mozilla.org/en-US/").read
  page.scan(/download\.html\?product\=firefox\-([^&]*)/).first.first
end

#install!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/firefox/installer.rb', line 20

def install!
  # if no dmg was specified, download an installer
  unless @dmg_file
    download_url = "http://download.mozilla.org/?lang=#{lang}&product=firefox-#{version}&os=#{os}"
    pbar = nil
    installer = open(download_url,
      :content_length_proc => lambda {|t|
        if t && 0 < t
          pbar = ProgressBar.new("Downloading", t)
          pbar.bar_mark = '='
          pbar.file_transfer_mode
        end
      },
      :progress_proc => lambda {|s|
        pbar.set s if pbar
      }
    )
    pbar.finish
  end
  
  case os
  when :osx
    tmp_path = @dmg_file ? @dmg_file : File.join(Dir.tmpdir,'firefox.dmg')
    unless @dmg_file
      File.open(tmp_path,'w') { |f| f.write(installer.read) }
      puts "Downloaded installer in #{tmp_path}"
    end
    
    puts "hdiutil mount #{tmp_path} 2>&1"
    mount_response = `hdiutil mount #{tmp_path} 2>&1`
    raise mount_response if mount_response =~ /mount failed/
    volume = mount_response.split.last
    puts "Mounted on #{volume}"
    FileUtils.cp_r(File.join(volume,'Firefox.app'), '/Applications/Firefox.app')
    puts 'Copied App in /Applications'
    unmount_response = `hdiutil unmount #{volume} 2>&1`
    raise unmount_response if unmount_response =~ /unmount failed/
    puts "Unmounted #{volume}"
  end
end