Class: U3d::LinuxInstaller

Inherits:
BaseInstaller show all
Defined in:
lib/u3d/installer.rb

Overview

rubocop:disable ClassLength

Instance Method Summary collapse

Methods inherited from BaseInstaller

#installed_sorted_by_versions, #sanitize_installs

Instance Method Details

#install(file_path, version, installation_path: nil, info: {}) ⇒ Object

rubocop:disable UnusedMethodArgument, PerceivedComplexity



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/u3d/installer.rb', line 217

def install(file_path, version, installation_path: nil, info: {})
  # rubocop:enable UnusedMethodArgument, PerceivedComplexity
  extension = File.extname(file_path)

  raise "Installation of #{extension} files is not supported on Linux" unless ['.sh', '.xz', '.pkg'].include? extension
  if extension == '.sh'
    path = installation_path || DEFAULT_LINUX_INSTALL
    install_sh(file_path, installation_path: path)
  elsif extension == '.xz'
    new_path = File.join(DEFAULT_LINUX_INSTALL, format(UNITY_DIR_LINUX, version: version))
    path = installation_path || new_path
    install_xz(file_path, installation_path: path)
  elsif extension == '.pkg'
    new_path = File.join(DEFAULT_LINUX_INSTALL, format(UNITY_DIR_LINUX, version: version))
    path = installation_path || new_path
    install_pkg(file_path, installation_path: path)
  end

  # Forces sanitation for installation of 'weird' versions eg 5.6.1xf1Linux
  unity = installed.select { |u| u.version == version }.first
  if unity
    sanitize_install(unity)
  else
    UI.error "Unity was not installed properly"
  end
end

#install_pkg(file, installation_path: nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/u3d/installer.rb', line 274

def install_pkg(file, installation_path: nil)
  raise 'Missing installation_path' unless installation_path
  raise 'Only able to install pkg on top of existing Unity installs' unless File.exist? installation_path
  raise 'Missing 7z' if `which 7z`.empty?

  Dir.mktmpdir do |tmp_dir|
    UI.verbose "Working in tmp dir #{tmp_dir}"

    command = "7z -aos -t* -o#{tmp_dir.shellescape} e #{file.shellescape}"
    U3dCore::CommandExecutor.execute(command: command)

    target_location = pkg_install_path(installation_path, "#{tmp_dir}/PackageInfo")

    # raise "Path for #{target_location} already exists" if path File.exist? target_location

    command = "cd #{target_location.shellescape}; gzip -dc #{tmp_dir}/Payload | cpio -i '*' -"
    command = "mkdir -p #{target_location.shellescape}; #{command}" # unless File.directory? installation_path

    U3dCore::CommandExecutor.execute(command: command, admin: true)
  end
rescue StandardError => e
  UI.verbose(e.backtrace.join("\n"))
  UI.error "Failed to install pkg file #{file} at #{installation_path}: #{e}"
else
  UI.success 'Installation successful'
end

#install_sh(file, installation_path: nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/u3d/installer.rb', line 244

def install_sh(file, installation_path: nil)
  cmd = file.shellescape

  U3dCore::CommandExecutor.execute(command: "chmod a+x #{cmd}")

  if installation_path
    command = "cd #{installation_path.shellescape}; #{cmd}"
    command = "mkdir -p #{installation_path.shellescape}; #{command}" unless File.directory? installation_path
    U3dCore::CommandExecutor.execute(command: command, admin: true)
  else
    U3dCore::CommandExecutor.execute(command: cmd, admin: true)
  end
rescue StandardError => e
  UI.error "Failed to install sh file #{file} at #{installation_path}: #{e}"
else
  UI.success 'Installation successful'
end

#install_xz(file, installation_path: nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/u3d/installer.rb', line 262

def install_xz(file, installation_path: nil)
  raise 'Missing installation_path' unless installation_path

  command = "cd #{installation_path.shellescape}; tar xf #{file.shellescape}"
  command = "mkdir -p #{installation_path.shellescape}; #{command}" unless File.directory? installation_path
  U3dCore::CommandExecutor.execute(command: command, admin: true)
rescue StandardError => e
  UI.error "Failed to install xz file #{file} at #{installation_path}: #{e}"
else
  UI.success 'Installation successful'
end

#installedObject



211
212
213
214
# File 'lib/u3d/installer.rb', line 211

def installed
  paths = (list_installed_paths + debian_installed_paths).uniq
  paths.map { |path| LinuxInstallation.new(root_path: path) }
end

#sanitize_install(unity, long: false, dry_run: false) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/u3d/installer.rb', line 200

def sanitize_install(unity, long: false, dry_run: false)
  source_path = File.expand_path(unity.root_path)
  parent = File.expand_path('..', source_path)
  dir_name = format(long ? UNITY_DIR_LINUX_LONG : UNITY_DIR_LINUX,
                    version: unity.version, build_number: unity.build_number)
  new_path = File.join(parent, dir_name)

  moved = U3dCore::AdminTools.move_os_file(:linux, source_path, new_path, dry_run: dry_run)
  unity.root_path = new_path if moved && !dry_run
end

#uninstall(unity: nil) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/u3d/installer.rb', line 301

def uninstall(unity: nil)
  UI.verbose("Uninstalling Unity at '#{unity.root_path}'...")
  command = "rm -r #{unity.root_path}"
  U3dCore::CommandExecutor.execute(command: command, admin: true)
rescue StandardError => e
  UI.error "Failed to uninstall unity at #{unity.path}: #{e}"
else
  UI.success "Successfully uninstalled '#{unity.root_path}'"
end