Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Apr 25, 2018
1 parent dcfc2a5 commit 54c4f13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/u3d/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module U3d
DEFAULT_LINUX_INSTALL = '/opt/'.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,8 +119,8 @@ 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)
def initialize
super(platform: :mac)
end

def sanitize_install(unity, long: false, dry_run: false)
Expand Down Expand Up @@ -208,8 +224,8 @@ def spotlight_installed_paths

# rubocop:disable ClassLength
class LinuxInstaller < BaseInstaller
def install_directory
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_LINUX_INSTALL)
def initialize
super(platform: :linux)
end

def sanitize_install(unity, long: false, dry_run: false)
Expand Down Expand Up @@ -242,7 +258,7 @@ def install(file_path, version, installation_path: nil, info: {})
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 @@ -365,8 +381,8 @@ def debian_installed_paths
# rubocop:enable ClassLength

class WindowsInstaller < BaseInstaller
def install_directory
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_WINDOWS_INSTALL)
def initialize
super(platform: :win)
end

def sanitize_install(unity, long: false, dry_run: false)
Expand Down
26 changes: 26 additions & 0 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

0 comments on commit 54c4f13

Please sign in to comment.