lib/u3d/installer.rb in u3d-1.0.8 vs lib/u3d/installer.rb in u3d-1.0.9

- old
+ new

@@ -23,10 +23,11 @@ require 'u3d/utils' require 'u3d_core/core_ext/string' require 'u3d/installation' require 'fileutils' require 'file-tail' +require 'pathname' module U3d DEFAULT_LINUX_INSTALL = '/opt/'.freeze DEFAULT_MAC_INSTALL = '/'.freeze DEFAULT_WINDOWS_INSTALL = 'C:/Program Files/'.freeze @@ -64,10 +65,15 @@ files.each do |name, file, info| UI.verbose "Installing #{name}#{info['mandatory'] ? ' (mandatory package)' : ''}, with file #{file}" installer.install(file, version, installation_path: installation_path, info: info) end end + + def self.uninstall(unity: nil) + installer = Installer.create + installer.uninstall(unity: unity) + end end class CommonInstaller def self.sanitize_install(source_path, new_path, command, dry_run: false) if source_path == new_path @@ -87,24 +93,22 @@ end end class MacInstaller def sanitize_install(unity, dry_run: false) - source_path = File.expand_path('..', unity.path) + source_path = unity.root_path parent = File.expand_path('..', source_path) new_path = File.join(parent, UNITY_DIR % unity.version) - source_path = source_path.shellescape - new_path = new_path.shellescape - command = "mv #{source_path} #{new_path}" + command = "mv #{source_path.shellescape} #{new_path.shellescape}" CommonInstaller.sanitize_install(source_path, new_path, command, dry_run: dry_run) end def installed paths = (list_installed_paths + spotlight_installed_paths).uniq - versions = paths.map { |path| MacInstallation.new(path: path) } + versions = paths.map { |path| MacInstallation.new(root_path: path) } # sorting should take into account stable/patch etc versions.sort! { |x, y| x.version <=> y.version } end @@ -130,13 +134,13 @@ UI.verbose "No Unity install for version #{version} was found" U3dCore::CommandExecutor.execute(command: command, admin: true) destination_path = File.join(target_path, 'Applications', UNITY_DIR % version) FileUtils.mv temp_path, destination_path else - UI.verbose "Unity install for version #{version} found under #{unity.path}" + UI.verbose "Unity install for version #{version} found under #{unity.root_path}" begin - path = File.expand_path('..', unity.path) + path = unity.root_path move_to_temp = (temp_path != path) if move_to_temp UI.verbose "Temporary switching location of #{path} to #{temp_path} for installation purpose" FileUtils.mv path, temp_path end @@ -149,15 +153,26 @@ UI.error "Failed to install pkg at #{file_path}: #{e}" else UI.success "Successfully installed package from #{file_path}" end + def uninstall(unity: nil) + UI.verbose("Uninstalling Unity at '#{unity.root_path}'...") + command = "rm -r #{unity.root_path.argescape}" + U3dCore::CommandExecutor.execute(command: command, admin: true) + rescue => e + UI.error "Failed to uninstall unity at #{unity.path}: #{e}" + else + UI.success "Successfully uninstalled '#{unity.root_path}'" + end + private def list_installed_paths find = File.join(DEFAULT_MAC_INSTALL, 'Applications', 'Unity*', 'Unity.app') paths = Dir[find] + paths = paths.map { |u| Pathname.new(u).parent.to_s } UI.verbose "Found list_installed_paths: #{paths}" paths end def spotlight_installed_paths @@ -171,31 +186,30 @@ mdfind_args = bundle_identifiers.map { |bi| "kMDItemCFBundleIdentifier == '#{bi}'" }.join(' || ') cmd = "mdfind \"#{mdfind_args}\" 2>/dev/null" UI.verbose cmd paths = `#{cmd}`.split("\n") + paths = paths.map { |u| Pathname.new(u).parent.to_s } UI.verbose "Found spotlight_installed_paths: #{paths}" paths end end class LinuxInstaller def sanitize_install(unity, dry_run: false) - source_path = File.expand_path(unity.path) + source_path = File.expand_path(unity.root_path) parent = File.expand_path('..', source_path) new_path = File.join(parent, UNITY_DIR_LINUX % unity.version) - source_path = source_path.shellescape - new_path = new_path.shellescape - command = "mv #{source_path} #{new_path}" + command = "mv #{source_path.shellescape} #{new_path.shellescape}" CommonInstaller.sanitize_install(source_path, new_path, command, dry_run: dry_run) end def installed find = File.join(DEFAULT_LINUX_INSTALL, 'unity-editor-*') - versions = Dir[find].map { |path| LinuxInstallation.new(path: path) } + versions = Dir[find].map { |path| LinuxInstallation.new(root_path: path) } # sorting should take into account stable/patch etc versions.sort! { |x, y| x.version <=> y.version } end @@ -235,31 +249,39 @@ rescue => e UI.error "Failed to install bash file at #{file}: #{e}" else UI.success 'Installation successful' end + + 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 => e + UI.error "Failed to uninstall unity at #{unity.path}: #{e}" + else + UI.success "Successfully uninstalled '#{unity.root_path}'" + end end class WindowsInstaller def sanitize_install(unity, dry_run: false) - source_path = File.expand_path(unity.path) + source_path = File.expand_path(unity.root_path) parent = File.expand_path('..', source_path) new_path = File.join(parent, UNITY_DIR % unity.version) source_path.tr!('/', '\\') new_path.tr!('/', '\\') - source_path = source_path.argescape - new_path = new_path.argescape - command = "move #{source_path} #{new_path}" + command = "move #{source_path.argescape} #{new_path.argescape}" CommonInstaller.sanitize_install(source_path, new_path, command, dry_run: dry_run) end def installed find = File.join(DEFAULT_WINDOWS_INSTALL, 'Unity*', 'Editor', 'Uninstall.exe') - versions = Dir[find].map { |path| WindowsInstallation.new(path: File.expand_path('../..', path)) } + versions = Dir[find].map { |path| WindowsInstallation.new(root_path: File.expand_path('../..', path)) } # sorting should take into account stable/patch etc versions.sort! { |x, y| x.version <=> y.version } end @@ -293,9 +315,21 @@ rescue => e UI.error "Failed to install exe at #{file_path}: #{e}" else UI.success "Successfully installed #{info['title']}" end + end + + def uninstall(unity: nil) + UI.verbose("Uninstalling Unity at '#{unity.root_path}'...") + uninstall_exe = File.join(unity.root_path, 'Editor', 'Uninstall.exe') + command = "#{uninstall_exe.argescape} /S" + UI.message("Although the uninstall process completed, it takes a few seconds before the files are actually removed") + U3dCore::CommandExecutor.execute(command: command, admin: true) + rescue => e + UI.error "Failed to uninstall unity at #{unity.path}: #{e}" + else + UI.success "Successfully uninstalled '#{unity.root_path}'" end end class LinuxDependencies # see https://forum.unity3d.com/threads/unity-on-linux-release-notes-and-known-issues.350256/