Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] u3d/install: refactor towards supporting a U3D_INSTALL_PATH env variable #287

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
59 changes: 44 additions & 15 deletions lib/u3d/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_%<version>s".freeze
UNITY_DIR_LONG = "Unity_%<version>s_%<build_number>s".freeze
UNITY_DIR_LINUX = "unity-editor-%<version>s".freeze
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand All @@ -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}"
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand Down
30 changes: 28 additions & 2 deletions spec/u3d/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down