Skip to content

Commit

Permalink
u3d/install: refactor towards supporting a U3D_INSTALL_PATH env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Apr 23, 2018
1 parent c11ab8d commit b37e08f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/u3d/commands_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
41 changes: 27 additions & 14 deletions lib/u3d/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

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_%<version>s".freeze
UNITY_DIR_LONG = "Unity_%<version>s_%<build_number>s".freeze
Expand Down Expand Up @@ -103,6 +103,10 @@ def self.sanitize_install(source_path, new_path, command, dry_run: false)
end

class MacInstaller < BaseInstaller
def install_directory
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_MAC_INSTALL)
end

def sanitize_install(unity, long: false, dry_run: false)
source_path = unity.root_path
parent = File.expand_path('..', source_path)
Expand All @@ -124,23 +128,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}"
Expand Down Expand Up @@ -175,7 +180,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}"
Expand All @@ -202,6 +207,10 @@ def spotlight_installed_paths
end

class LinuxInstaller < BaseInstaller
def install_directory
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_LINUX_INSTALL)
end

def sanitize_install(unity, long: false, dry_run: false)
source_path = File.expand_path(unity.root_path)
parent = File.expand_path('..', source_path)
Expand All @@ -225,10 +234,10 @@ def install(file_path, version, installation_path: nil, info: {})

raise "Installation of #{extension} files is not supported on Linux" unless ['.sh', '.xz'].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)
end
Expand Down Expand Up @@ -285,7 +294,7 @@ def uninstall(unity: nil)
private

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}"
Expand All @@ -302,6 +311,10 @@ def debian_installed_paths
end

class WindowsInstaller < BaseInstaller
def install_directory
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_WINDOWS_INSTALL)
end

def sanitize_install(unity, long: false, dry_run: false)
source_path = File.expand_path(unity.root_path)
parent = File.expand_path('..', source_path)
Expand All @@ -314,14 +327,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,
Expand All @@ -330,7 +343,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
Expand Down
4 changes: 2 additions & 2 deletions spec/u3d/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,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")
Expand Down

0 comments on commit b37e08f

Please sign in to comment.