Class: WindowsInstaller

Inherits:
Hash
  • Object
show all
Defined in:
lib/WindowsInstaller.rb

Constant Summary collapse

@@default_options =
{mode: '/passive'}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WindowsInstaller

Returns a new instance of WindowsInstaller.



8
9
10
11
12
13
# File 'lib/WindowsInstaller.rb', line 8

def initialize(options = {})
  @@default_options.each { |key, value| self[key] = value }
  options.each { |key, value| self[key] = value}

 @installer = WIN32OLE.new('WindowsInstaller.Installer')
end

Class Method Details

.default_options(hash) ⇒ Object



15
16
17
# File 'lib/WindowsInstaller.rb', line 15

def self.default_options(hash) 
 hash.each { |key,value| @@default_options[key] = value }
end

Instance Method Details

#install_msi(msi_file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/WindowsInstaller.rb', line 36

def install_msi(msi_file)
  raise "#{msi_file} does not exist!" unless(File.exist?(msi_file))

  msi_file = File.absolute_path(msi_file).gsub(/\//, '\\')

 cmd = "msiexec.exe"
 cmd = "#{cmd} #{self[:mode]}" if(has_key?(:mode))
 cmd = "#{cmd} /i #{msi_file}"

 msiexec(cmd)
 raise "Failed to install msi_file: #{msi_file}" unless(msi_installed?(msi_file))
end

#installation_properties(product_name_or_product_code) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/WindowsInstaller.rb', line 98

def installation_properties(product_name_or_product_code)	
  product_code = product_name_or_product_code
  properties = installed_properties(product_code)
  if(properties.nil?)
   product_name = product_name_or_product_code
   product_code = installed_get_product_code(product_name)
   return nil if(product_code == '')
 
   properties = installed_properties(product_code)
 end
	
 return nil if(properties.nil?)
	
 upgrade_code = get_upgrade_code(product_code)
 properties['UpgradeCode'] = upgrade_code unless(upgrade_code.nil?)
	
  return properties
end

#installed_productsObject



117
118
119
120
121
# File 'lib/WindowsInstaller.rb', line 117

def installed_products
  products = []
 @installer.Products.each { |installed_product_code| products << installed_product_code }
 return products
end

#msi_installed?(msi_file) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/WindowsInstaller.rb', line 19

def msi_installed?(msi_file)
  info = msi_properties(msi_file)
  return product_code_installed?(info['ProductCode'])
end

#msi_properties(msi_file) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/WindowsInstaller.rb', line 74

def msi_properties(msi_file)
  raise "#{msi_file} does not exist!" unless(File.exist?(msi_file))

  properties = {}
 
  sql_query = "SELECT * FROM `Property`"

 db = @installer.OpenDatabase(msi_file, 0)
 view = db.OpenView(sql_query)
 view.Execute(nil)
	
 record = view.Fetch()
 return nil if(record == nil)

 while(!record.nil?)
   properties[record.StringData(1)] = record.StringData(2) 
   record = view.Fetch()
 end
 db.ole_free
 db = nil

 return properties
end

#product_code_installed?(product_code) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/WindowsInstaller.rb', line 31

def product_code_installed?(product_code)
 @installer.Products.each { |installed_product_code| return true if (product_code == installed_product_code) }
 return false
end

#product_codes(upgrade_code) ⇒ Object



123
124
125
126
# File 'lib/WindowsInstaller.rb', line 123

def product_codes(upgrade_code)
 upgrade_codes = get_upgrade_codes
 return (upgrade_codes.key?(upgrade_code)) ? upgrade_codes[upgrade_code] : []
end

#product_installed?(product_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/WindowsInstaller.rb', line 24

def product_installed?(product_name)
 product_code = installed_get_product_code(product_name)
  return false if(product_code.empty?)
	
 return product_code_installed?(product_code)
end

#uninstall_msi(msi_file) ⇒ Object



49
50
51
52
53
# File 'lib/WindowsInstaller.rb', line 49

def uninstall_msi(msi_file)
  raise "#{msi_file} does not exist!" unless(File.exist?(msi_file))
  info = msi_properties(msi_file)
  uninstall_product_code(info['ProductCode'])
end

#uninstall_product(product_name) ⇒ Object

Raises:

  • (StandardError)


55
56
57
58
59
60
# File 'lib/WindowsInstaller.rb', line 55

def uninstall_product(product_name)
  raise StandardError.new("Product '#{product_name}' is not installed") unless(product_installed?(product_name))
	
 product_code = installed_get_product_code(product_name)
 uninstall_product_code(product_code)		
end

#uninstall_product_code(product_code) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/WindowsInstaller.rb', line 61

def uninstall_product_code(product_code)
  raise "#{product_code} is not installed" unless(product_code_installed?(product_code))
 
 cmd = "msiexec.exe"
 cmd = "#{cmd} #{self[:mode]}" if(has_key?(:mode))
 cmd = "#{cmd} /x #{product_code}"
 msiexec(cmd)
 if(product_code_installed?(product_code))
   properties = installed_properties(product_code)
    raise "Failed to uninstall #{properties['InstalledProductName']} #{properties['VersionString']}" 
 end
end