diff --git a/lib/u3d/commands_generator.rb b/lib/u3d/commands_generator.rb index 5cacef08..79b2df7a 100644 --- a/lib/u3d/commands_generator.rb +++ b/lib/u3d/commands_generator.rb @@ -146,6 +146,10 @@ def run The default download path is $HOME/Downloads/Unity_Packages/, but you may change that by specifying the environment variable U3D_DOWNLOAD_PATH. E.g. U3D_DOWNLOAD_PATH=/some/path/you/want u3d install ... + +The default install path is platform specific, but you may change that by specifying the environment variable U3D_INSTALL_PATH. + +E.g. U3D_INSTALL_PATH=/some/path/you/want u3d install ... ) c.option '--[no-]download', 'Perform or not downloading before installation. Downloads by default' c.option '--[no-]install', 'Perform or not installation after downloading. Installs by default' diff --git a/lib/u3d/installer.rb b/lib/u3d/installer.rb index 9baa4df5..5653dfcb 100644 --- a/lib/u3d/installer.rb +++ b/lib/u3d/installer.rb @@ -30,8 +30,9 @@ module U3d DEFAULT_LINUX_INSTALL = '/opt/'.freeze - DEFAULT_MAC_INSTALL = '/'.freeze + DEFAULT_MAC_INSTALL = '/Applications/'.freeze DEFAULT_WINDOWS_INSTALL = 'C:/Program Files/'.freeze + UNITY_DIR = "Unity_%s".freeze UNITY_DIR_LONG = "Unity_%s_%s".freeze UNITY_DIR_LINUX = "unity-editor-%s".freeze @@ -68,6 +69,21 @@ def self.uninstall(unity: nil) end class BaseInstaller + DEFAULT_PLATFORM_INSTALL_PATH = { + linux: DEFAULT_LINUX_INSTALL, + win: DEFAULT_WINDOWS_INSTALL, + mac: DEFAULT_MAC_INSTALL + } + + def initialize(platform: nil) # nil for bacward compatibility. Is really required + @platform = platform + end + + def install_directory(default_only: false) + path = default_only ? DEFAULT_PLATFORM_INSTALL_PATH[@platform] : ENV['U3D_INSTALL_PATH'] || DEFAULT_PLATFORM_INSTALL_PATH[@platform] + File.expand_path(path) + end + def sanitize_installs return unless UI.interactive? || Helper.test? unclean = [] @@ -103,6 +119,10 @@ def self.sanitize_install(source_path, new_path, command, dry_run: false) end class MacInstaller < BaseInstaller + def initialize + super(platform: :mac) + end + def sanitize_install(unity, long: false, dry_run: false) source_path = unity.root_path parent = File.expand_path('..', source_path) @@ -124,23 +144,24 @@ def install(file_path, version, installation_path: nil, info: {}) # rubocop:enable UnusedMethodArgument extension = File.extname(file_path) raise "Installation of #{extension} files is not supported on Mac" if extension != '.pkg' - path = installation_path || DEFAULT_MAC_INSTALL + path = installation_path || install_directory install_pkg( file_path, version: version, - target_path: path + installation_path: path ) end - def install_pkg(file_path, version: nil, target_path: nil) - target_path ||= DEFAULT_MAC_INSTALL + # FIXME: we don't support target_path anymore + def install_pkg(file_path, version: nil, installation_path: nil) + target_path = '/' command = "installer -pkg #{file_path.shellescape} -target #{target_path.shellescape}" unity = installed.find { |u| u.version == version } - temp_path = File.join(target_path, 'Applications', 'Unity') + temp_path = File.join(installation_path, 'Unity') if unity.nil? UI.verbose "No Unity install for version #{version} was found" U3dCore::CommandExecutor.execute(command: command, admin: true) - destination_path = File.join(target_path, 'Applications', format(UNITY_DIR, version: version)) + destination_path = File.join(install_directory, format(UNITY_DIR, version: version)) FileUtils.mv temp_path, destination_path else UI.verbose "Unity install for version #{version} found under #{unity.root_path}" @@ -175,7 +196,7 @@ def uninstall(unity: nil) private def list_installed_paths - find = File.join(DEFAULT_MAC_INSTALL, 'Applications', 'Unity*', 'Unity.app') + find = File.join(install_directory, 'Unity*', 'Unity.app') paths = Dir[find] paths = paths.map { |u| Pathname.new(u).parent.to_s } UI.verbose "Found list_installed_paths: #{paths}" @@ -203,6 +224,10 @@ def spotlight_installed_paths # rubocop:disable ClassLength class LinuxInstaller < BaseInstaller + def initialize + super(platform: :linux) + end + def sanitize_install(unity, long: false, dry_run: false) source_path = File.expand_path(unity.root_path) parent = File.expand_path('..', source_path) @@ -226,14 +251,14 @@ def install(file_path, version, installation_path: nil, info: {}) 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 + path = installation_path || install_directory install_sh(file_path, installation_path: path) elsif extension == '.xz' - new_path = File.join(DEFAULT_LINUX_INSTALL, format(UNITY_DIR_LINUX, version: version)) + new_path = File.join(install_directory, 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)) + new_path = File.join(install_directory, format(UNITY_DIR_LINUX, version: version)) path = installation_path || new_path install_pkg(file_path, installation_path: path) end @@ -338,7 +363,7 @@ def pkg_install_path(unity_root_path, pinfo_path) end def list_installed_paths - find = File.join(DEFAULT_LINUX_INSTALL, 'unity-editor-*', 'Editor') + find = File.join(install_directory, 'unity-editor-*', 'Editor') paths = Dir[find] paths = paths.map { |u| Pathname.new(u).parent.to_s } UI.verbose "Found list_installed_paths: #{paths}" @@ -356,6 +381,10 @@ def debian_installed_paths # rubocop:enable ClassLength class WindowsInstaller < BaseInstaller + def initialize + super(platform: :win) + end + def sanitize_install(unity, long: false, dry_run: false) source_path = File.expand_path(unity.root_path) parent = File.expand_path('..', source_path) @@ -368,14 +397,14 @@ def sanitize_install(unity, long: false, dry_run: false) end def installed - find = File.join(DEFAULT_WINDOWS_INSTALL, 'Unity*', 'Editor', 'Uninstall.exe') + find = File.join(install_directory, 'Unity*', 'Editor', 'Uninstall.exe') Dir[find].map { |path| WindowsInstallation.new(root_path: File.expand_path('../..', path)) } end def install(file_path, version, installation_path: nil, info: {}) extension = File.extname(file_path) raise "Installation of #{extension} files is not supported on Windows" unless %w[.exe .msi].include? extension - path = installation_path || File.join(DEFAULT_WINDOWS_INSTALL, format(UNITY_DIR, version: version)) + path = installation_path || File.join(install_directory, format(UNITY_DIR, version: version)) install_exe( file_path, installation_path: path, @@ -384,7 +413,7 @@ def install(file_path, version, installation_path: nil, info: {}) end def install_exe(file_path, installation_path: nil, info: {}) - installation_path ||= DEFAULT_WINDOWS_INSTALL + installation_path ||= install_directory final_path = U3dCore::Helper.windows_path(installation_path) Utils.ensure_dir(final_path) begin diff --git a/spec/u3d/installer_spec.rb b/spec/u3d/installer_spec.rb index a6946d83..a13ff337 100644 --- a/spec/u3d/installer_spec.rb +++ b/spec/u3d/installer_spec.rb @@ -27,6 +27,9 @@ describe U3d::Installer do describe U3d::BaseInstaller do class DummyInstaller < U3d::BaseInstaller + def initialize + super(platform: :dummy) + end end describe ".sanitize_installs" do context "Clean installs" do @@ -87,6 +90,29 @@ class DummyInstaller < U3d::BaseInstaller expect(installer.installed_sorted_by_versions).to eq(sorted_installed) end end + + context '.install_directory' do + before(:each) do + U3d::BaseInstaller::DEFAULT_PLATFORM_INSTALL_PATH[:dummy] = '/Somewhere'.freeze + end + it 'returns the default installation when INSTALL_PATH is not set' do + with_env_values('U3D_INSTALL_PATH' => nil) do + expect(DummyInstaller.new.install_directory(default_only: true)).to eq '/Somewhere' + end + end + + it 'returns the default installation when INSTALL_PATH is set but default_only is asked' do + with_env_values('U3D_INSTALL_PATH' => '/Applications/Unity') do + expect(DummyInstaller.new.install_directory(default_only: true)).to eq '/Somewhere' + end + end + + it 'returns the overriden installation when INSTALL_PATH is set' do + with_env_values('U3D_INSTALL_PATH' => '/Applications/Unity') do + expect(DummyInstaller.new.install_directory(default_only: false)).to eq '/Applications/Unity' + end + end + end end describe U3d::MacInstaller, unless: WINDOWS do @@ -160,9 +186,9 @@ class DummyInstaller < U3d::BaseInstaller describe '.install' do it 'installs a file in standard installation path' do filepath = "file.sh" - allow(File).to receive(:directory?).with(U3d::DEFAULT_LINUX_INSTALL) { true } + allow(File).to receive(:directory?).with('/opt') { true } expect(U3dCore::CommandExecutor).to receive(:execute).with(command: 'chmod a+x file.sh') {} - expect(U3dCore::CommandExecutor).to receive(:execute).with(command: "cd #{U3d::DEFAULT_LINUX_INSTALL}; file.sh", admin: true) {} + expect(U3dCore::CommandExecutor).to receive(:execute).with(command: "cd /opt; file.sh", admin: true) {} installer = U3d::LinuxInstaller.new unity = double("UnityProject")