Class: Wix

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

Class Method Summary collapse

Class Method Details

.apply_wix_template(output, input, template) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wixgem.rb', line 70

def self.apply_wix_template(output, input, template)
	ext = File.extname(output)
	basename = File.basename(output, ext)
	FileUtils.rm(output) if(File.exists?(output))

	output_path = File.dirname(output)
	wix_file = "#{File.dirname(output)}/#{basename}.wxs"
	create_wix_file(wix_file, input, template)
	
  raise 'WIX path is not set!' if(install_path.nil?)

	stdout = %x[\"#{install_path}\\bin\\candle.exe\" -out \"#{output_path}/#{basename}.wixobj\" \"#{wix_file}\"]
	raise "#{stdout}\nFailed to generate .wixobj file" unless(File.exists?("#{output_path}/#{basename}.wixobj"))

  stdout = %x[\"#{install_path}\\bin\\light.exe\" -nologo -out \"#{output}\" \"#{output_path}/#{basename}.wixobj\"]
	raise "#{stdout}\nFailed to generate #{output} file" unless(File.exists?(output))
  
	Dir.glob("#{output_path}/#{basename}.{wxs,wixobj,wixpdb}") { |file| FileUtils.rm(file) }
end

.component_refs_xml(dir_files_hash) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/wixgem.rb', line 124

def self.component_refs_xml(dir_files_hash)
	xml = ''
	dir_files_hash.each do |directory, value|	 
 if(value.is_a?(Hash)) 
   if(value.has_key?(:files))
        value[:files].each do |file| 
    if(File.extname(file) == ".msm")
      xml = "#{xml}<MergeRef Id=\"#{wix_id(file)}\" />\n" 
	else
      xml = "#{xml}<ComponentRef Id=\"#{wix_id(file)}\" />\n" 
	end
  end 
end
   xml = "#{xml}#{component_refs_xml(value)}" 
 end
	end
	
	return xml  
end

.create_installation_wixfile(output_wixfile, input) ⇒ Object



20
21
22
23
# File 'lib/wixgem.rb', line 20

def self.create_installation_wixfile(output_wixfile, input)
  gem_dir = File.dirname(__FILE__)
  create_wix_file(output_wixfile, input, "#{gem_dir}/templates/mergemodule.wxs")
end

.create_mergemodule_wixfile(output_wixfile, input) ⇒ Object



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

def self.create_mergemodule_wixfile(output_wixfile, input)
  gem_dir = File.dirname(__FILE__)
  create_wix_file(output_wixfile, input, "#{gem_dir}/templates/mergemodule.wxs")
end

.create_wix_file(wix_file, input, template) ⇒ Object



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
60
61
62
63
64
65
66
67
68
# File 'lib/wixgem.rb', line 35

def self.create_wix_file(wix_file, input, template)
	wix_basename = File.basename(wix_file, '.wxs')
	wxs_text = File.read(template)

  files = input
  files = input[:files] if(input.kind_of?(Hash))

	wxs_text = manage_files(wxs_text, files)

	wxs_text = wxs_text.gsub(/PRODUCT_NAME/) { |s| s = wix_basename }
	wxs_text = wxs_text.gsub(/MODULE_NAME/) { |s| s = wix_basename }
  
	product_code = "{#{SecureRandom.uuid}}"
	product_code = input[:product_code] if(input.kind_of?(Hash) && input.has_key?(:product_code))
	wxs_text = wxs_text.gsub(/PRODUCT_CODE/) { |s| s = product_code }

	upgrade_code = "{#{SecureRandom.uuid}}"
	upgrade_code = input[:upgrade_code] if(input.kind_of?(Hash) && input.has_key?(:upgrade_code))
	wxs_text = wxs_text.gsub(/UPGRADE_CODE/) { |s| s = upgrade_code }
	
	product_version = '0.0.0.0'
  product_version = input[:version] if(input.kind_of?(Hash) && input.has_key?(:version))
	wxs_text = wxs_text.gsub(/VERSION/) { |s| s = product_version }

  manufacturer = ''
  manufacturer = input[:manufacturer] if(input.kind_of?(Hash) && input.has_key?(:manufacturer))
  dir_xml = "<Directory Id=\"INSTALLFOLDER\" Name=\"#{wix_basename}\" />"
	dir_xml = "<Directory Id=\"MUSCOFOLDER\" Name=\"#{manufacturer}\">#{dir_xml}</Directory>" if(manufacturer.length > 0)
	wxs_text = wxs_text.gsub(/MANUFACTURER/) { |s| s = manufacturer } unless(manufacturer.empty?)
	wxs_text = wxs_text.gsub(/INSTALL_DIR/) { |s| s = dir_xml }
	
	#puts wxs_text
	File.open(wix_file, 'w') { |f| f.puts(wxs_text) }
end

.files_to_xml(files) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wixgem.rb', line 98

def self.files_to_xml(files)
  xml = ''
	files.each do |file| 
    if(File.extname(file) == ".msm")
   file_xml = "<Merge Id=\"#{wix_id(file)}\" Language=\"1033\" SourceFile=\"#{file}\" DiskId='1' />"
 else
   file_xml = "<Component Id=\"#{wix_id(file)}\" Guid=\"{#{SecureRandom.uuid}}\"><File Id=\"#{wix_id(file)}\" Source=\"#{file}\" /></Component>\n"
 end
 xml = "#{xml}#{file_xml}" 
	end
	return xml
end

.files_xml(dir_files_hash) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wixgem.rb', line 111

def self.files_xml(dir_files_hash)
	xml = ''
	dir_files_hash.each do |directory, value|
 if(value.is_a?(Hash))
   xml = "#{xml}<Directory Id=\"#{wix_id(value[:full_path])}\" Name=\"#{directory}\">\n" unless(directory == '.')
   xml = "#{xml}#{files_to_xml(value[:files])}" if(value.has_key?(:files))
   xml = "#{xml}#{files_xml(value)}"
	    xml = "#{xml}</Directory>\n" unless(directory == '.')
 end
	end
	return xml
end

.install_pathObject



10
11
12
13
# File 'lib/wixgem.rb', line 10

def self.install_path
  @install_path = ENV['WIX'] if(@install_path.nil?)
	return @install_path
end

.install_path=(value) ⇒ Object



5
6
7
8
# File 'lib/wixgem.rb', line 5

def self.install_path=(value)
  raise "WIX path '#{value}' does not exist" unless(Dir.exists?(value))
	@install_path = value
end

.make_installation(output, input) ⇒ Object



30
31
32
33
# File 'lib/wixgem.rb', line 30

def self.make_installation(output, input)
  gem_dir = File.dirname(__FILE__)
  apply_wix_template(output, input, "#{gem_dir}/templates/Install.wxs")
end

.make_mergemodule(output, input) ⇒ Object



25
26
27
28
# File 'lib/wixgem.rb', line 25

def self.make_mergemodule(output, input)
  gem_dir = File.dirname(__FILE__)
  apply_wix_template(output, input, "#{gem_dir}/templates/mergemodule.wxs")
end

.manage_files(wxs_text, files) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/wixgem.rb', line 144

def self.manage_files(wxs_text, files)
	dir_files = {}
	files.each do |file| 
 if(File.file?(file))
   dir_path = File.dirname(file)
   
dir_hash = dir_files
dir_path.split('/').each do |d| 
  if(!dir_hash.has_key?(d))
       dir_hash[d] = Hash.new unless(dir_hash.has_key?(d))
	if(dir_hash.has_key?(:full_path))
	  dir_hash[d][:full_path] = "#{dir_hash[:full_path]}/#{d}"
	else
	  dir_hash[d][:full_path] = d
	end
  end
  dir_hash = dir_hash[d]
   end
 
   dir_hash[:files] = Array.new unless(dir_hash.has_key?(:files))
   dir_hash[:files].insert(dir_hash[:files].length, file)
 end
	end
	
	wxs_text = wxs_text.gsub(/COMPONENT_REFS/) { |s| s = component_refs_xml(dir_files) }
	wxs_text = wxs_text.gsub(/FILES/) { |s| s = files_xml(dir_files) }
	return wxs_text
end

.wix_id(file) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/wixgem.rb', line 90

def self.wix_id(file)
  id = "id_#{file}".gsub(/[^a-zA-Z0-9_\.]/) { |s| s = '_' }
	max_id_length = 72
	id = id.slice(id.length-max_id_length, max_id_length) unless(id.length <= max_id_length)
	id[0] = '_' if(id[0] == '.')
	return id
end