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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wixgem.rb', line 133

def self.apply_wix_template(output, input, template)
  raise 'WIX path is not set!' if(install_path.nil?)
 
  ext = File.extname(output)
  basename = File.basename(output, ext)
  FileUtils.rm(output) if(File.exists?(output))
 
  output_absolute_path = File.absolute_path(output)

  #dir = 'tmp_dir'

  #FileUtils.rm_rf(dir) if(Dir.exists?(dir))

  Dir.mktmpdir do |dir|
 copy_install_files(dir, input)
 
 wxs_file = "#{basename}.wxs"     
 Dir.chdir(dir) do |current_dir|
create_wxs_file(wxs_file, input, ext)
create_output(wxs_file, output_absolute_path)
 end
  end
  pdb_file = output_absolute_path.gsub(ext,'.wixpdb')
  FileUtils.rm(pdb_file) if(File.exists?(pdb_file))
end

.copy_install_files(directory, input) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wixgem.rb', line 55

def self.copy_install_files(directory, input)
  files = input
  files = input[:files] if(input.kind_of?(Hash))
  
  files.each do |file| 
 if(File.file?(file))
   install_path = "#{directory}/#{file}"
FileUtils.mkpath(File.dirname(install_path)) unless(Dir.exists?(File.dirname(install_path)))
FileUtils.cp(file, install_path)
#   else if(!Dir.exists?(file) && !File.exists?(file))

#     raise "File: '#{file}' does not exist!"

 end
  end
end

.create_output(wxs_file, output) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/wixgem.rb', line 123

def self.create_output(wxs_file, output)
  wixobj_file = "#{File.basename(wxs_file)}.wixobj"
  
  stdout = %x[\"#{install_path}\\bin\\candle.exe\" -out \"#{wixobj_file}\" \"#{wxs_file}\"]
  raise "#{stdout}\nFailed to generate .wixobj file" unless(File.exists?(wixobj_file))

  stdout = %x[\"#{install_path}\\bin\\light.exe\" -nologo -out \"#{output}\" \"#{wixobj_file}\"]
  raise "#{stdout}\nFailed to generate #{output} file" unless(File.exists?(output))
end

.create_wxs_file(wxs_file, input, ext) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wixgem.rb', line 70

def self.create_wxs_file(wxs_file, input, ext)
  template_option = "-template product"
  template_option = "-template module" unless(ext == ".msi")
  
  stdout = %x[\"#{install_path}/bin/heat.exe\" dir . #{template_option} -cg InstallionFiles -gg -nologo -srd -o  \"#{wxs_file}\"]
  raise "#{stdout}\nFailed to generate .wxs file" unless(File.exists?(wxs_file))
  
  product_name = File.basename(wxs_file, '.wxs')
  product_name = input[:product_name] if(input.kind_of?(Hash) && input.has_key?(:product_name))
  
  manufacturer = 'Not Set'
  manufacturer = input[:manufacturer] if(input.kind_of?(Hash) && input.has_key?(:manufacturer))

  product_version = ''
  product_version = input[:version] if(input.kind_of?(Hash) && input.has_key?(:version))

  product_code = ''
  product_code = input[:product_code] if(input.kind_of?(Hash) && input.has_key?(:product_code))
  
  wxs_text = File.read(wxs_file)

  wxs_text = wxs_text.gsub(/SourceDir\\/) { |s| s = '.\\' }
  wxs_text = wxs_text.gsub(/PUT-PRODUCT-NAME-HERE/) { |s| s = product_name }
  wxs_text = wxs_text.gsub(/PUT-MODULE-NAME-HERE/) { |s| s = product_name }
  wxs_text = wxs_text.gsub(/PUT-COMPANY-NAME-HERE/) { |s| s = manufacturer }
  wxs_text = wxs_text.gsub(/PUT-FEATURE-TITLE-HERE/) { |s| s = 'Files to Install' }

  wxs_text = wxs_text.gsub(/Version=\"1.0.0.0\"/) { |s| s = "Version=\"#{product_version}\"" } unless(product_version.empty?)
  wxs_text = wxs_text.gsub(/Product Id=\"[^\"]+\"/) { |s| s = "Product Id=\"#{product_code}\"" } unless(product_code.empty?)

  install_path = '[ProgramFilesFolder][ProductName]'
  install_path = "[ProgramFilesFolder][Manufacturer]\\[ProductName]" unless(manufacturer == 'Not Set')
  
  custom_action = "
      <CustomAction Id='SetTARGETDIR' Property='TARGETDIR' Value='#{install_path}' Execute='firstSequence' />

<InstallUISequence>
  <!-- Set TARGETDIR if it wasn't set on the command line -->
  <Custom Action='SetTARGETDIR' Before='CostFinalize'>TARGETDIR=\"\"</Custom>
</InstallUISequence>

<InstallExecuteSequence>
  <!-- Set TARGETDIR if it wasn't set on the command line -->
  <Custom Action='SetTARGETDIR' Before='CostFinalize'>TARGETDIR=\"\"</Custom>
</InstallExecuteSequence>
  </Product>"  
  wxs_text = wxs_text.gsub(/<\/Product>/) { |s| s = custom_action }

  wxs_text = manage_msm_files(wxs_text)

  File.open(wxs_file, 'w') { |f| f.puts(wxs_text) }  
end

.install_pathObject



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

def self.install_path
  return @install_path
end

.install_path=(value) ⇒ Object



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

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



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

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



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

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

.manage_msm_files(wxs_text) ⇒ Object



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
# File 'lib/wixgem.rb', line 26

def self.manage_msm_files(wxs_text)
  indent_merge = '     '
  indent_directory = '   '
  
  merge_ids = ''
  merge_refs = ''
  remove_components = []
  
  component = 0
  id = 1
  file = 2
  wxs_text.scan(/(?<component><Component Id=\"(?<id>[^\"]+)\".+Source=\"(?<file>.+\.msm)\".+Component>)/m) { |match|
 merge_id = match[id].gsub('cmp','merge')
 merge_ids = "#{merge_ids}#{indent_merge}<Merge Id='#{merge_id}' Language='1033' SourceFile='#{match[file]}' DiskId='1' />\n"
 merge_refs = "#{indent_merge}#{merge_refs}<MergeRef Id='#{merge_id}' />\n#{indent_merge}"

 remove_components.insert(remove_components.length, match[component])
  }
  
  remove_components.each { |cmp| wxs_text = wxs_text.gsub(cmp, '') }
  
  directory_element = "<Directory Id='TARGETDIR' Name='SourceDir'>\n#{merge_ids}#{indent_directory}</Directory>"
  wxs_text = wxs_text.gsub('<Directory Id="TARGETDIR" Name="SourceDir" />', directory_element)
  
  wxs_text = wxs_text.gsub(/\s+<\/Feature>/, "\n#{merge_refs}</Feature>")

  return wxs_text
end