Skip to content

Commit bccbec2

Browse files
committed
u3d/install: refactor towards supporting a U3D_INSTALL_PATH env variable
1 parent 6eb5732 commit bccbec2

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

lib/u3d/commands_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def run
146146
The default download path is $HOME/Downloads/Unity_Packages/, but you may change that by specifying the environment variable U3D_DOWNLOAD_PATH.
147147
148148
E.g. U3D_DOWNLOAD_PATH=/some/path/you/want u3d install ...
149+
150+
The default install path is platform specific, but you may change that by specifying the environment variable U3D_INSTALL_PATH.
151+
152+
E.g. U3D_INSTALL_PATH=/some/path/you/want u3d install ...
149153
)
150154
c.option '--[no-]download', 'Perform or not downloading before installation. Downloads by default'
151155
c.option '--[no-]install', 'Perform or not installation after downloading. Installs by default'

lib/u3d/installer.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
module U3d
3232
DEFAULT_LINUX_INSTALL = '/opt/'.freeze
33-
DEFAULT_MAC_INSTALL = '/'.freeze
33+
DEFAULT_MAC_INSTALL = '/Applications/'.freeze
3434
DEFAULT_WINDOWS_INSTALL = 'C:/Program Files/'.freeze
3535
UNITY_DIR = "Unity_%<version>s".freeze
3636
UNITY_DIR_LONG = "Unity_%<version>s_%<build_number>s".freeze
@@ -103,6 +103,10 @@ def self.sanitize_install(source_path, new_path, command, dry_run: false)
103103
end
104104

105105
class MacInstaller < BaseInstaller
106+
def install_directory
107+
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_MAC_INSTALL)
108+
end
109+
106110
def sanitize_install(unity, long: false, dry_run: false)
107111
source_path = unity.root_path
108112
parent = File.expand_path('..', source_path)
@@ -124,23 +128,24 @@ def install(file_path, version, installation_path: nil, info: {})
124128
# rubocop:enable UnusedMethodArgument
125129
extension = File.extname(file_path)
126130
raise "Installation of #{extension} files is not supported on Mac" if extension != '.pkg'
127-
path = installation_path || DEFAULT_MAC_INSTALL
131+
path = installation_path || install_directory
128132
install_pkg(
129133
file_path,
130134
version: version,
131-
target_path: path
135+
installation_path: path
132136
)
133137
end
134138

135-
def install_pkg(file_path, version: nil, target_path: nil)
136-
target_path ||= DEFAULT_MAC_INSTALL
139+
# FIXME: we don't support target_path anymore
140+
def install_pkg(file_path, version: nil, installation_path: nil)
141+
target_path = '/'
137142
command = "installer -pkg #{file_path.shellescape} -target #{target_path.shellescape}"
138143
unity = installed.find { |u| u.version == version }
139-
temp_path = File.join(target_path, 'Applications', 'Unity')
144+
temp_path = File.join(installation_path, 'Unity')
140145
if unity.nil?
141146
UI.verbose "No Unity install for version #{version} was found"
142147
U3dCore::CommandExecutor.execute(command: command, admin: true)
143-
destination_path = File.join(target_path, 'Applications', format(UNITY_DIR, version: version))
148+
destination_path = File.join(install_directory, format(UNITY_DIR, version: version))
144149
FileUtils.mv temp_path, destination_path
145150
else
146151
UI.verbose "Unity install for version #{version} found under #{unity.root_path}"
@@ -175,7 +180,7 @@ def uninstall(unity: nil)
175180
private
176181

177182
def list_installed_paths
178-
find = File.join(DEFAULT_MAC_INSTALL, 'Applications', 'Unity*', 'Unity.app')
183+
find = File.join(install_directory, 'Unity*', 'Unity.app')
179184
paths = Dir[find]
180185
paths = paths.map { |u| Pathname.new(u).parent.to_s }
181186
UI.verbose "Found list_installed_paths: #{paths}"
@@ -202,6 +207,10 @@ def spotlight_installed_paths
202207
end
203208

204209
class LinuxInstaller < BaseInstaller
210+
def install_directory
211+
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_LINUX_INSTALL)
212+
end
213+
205214
def sanitize_install(unity, long: false, dry_run: false)
206215
source_path = File.expand_path(unity.root_path)
207216
parent = File.expand_path('..', source_path)
@@ -225,10 +234,10 @@ def install(file_path, version, installation_path: nil, info: {})
225234

226235
raise "Installation of #{extension} files is not supported on Linux" unless ['.sh', '.xz'].include? extension
227236
if extension == '.sh'
228-
path = installation_path || DEFAULT_LINUX_INSTALL
237+
path = installation_path || install_directory
229238
install_sh(file_path, installation_path: path)
230239
elsif extension == '.xz'
231-
new_path = File.join(DEFAULT_LINUX_INSTALL, format(UNITY_DIR_LINUX, version: version))
240+
new_path = File.join(install_directory, format(UNITY_DIR_LINUX, version: version))
232241
path = installation_path || new_path
233242
install_xz(file_path, installation_path: path)
234243
end
@@ -285,7 +294,7 @@ def uninstall(unity: nil)
285294
private
286295

287296
def list_installed_paths
288-
find = File.join(DEFAULT_LINUX_INSTALL, 'unity-editor-*', 'Editor')
297+
find = File.join(install_directory, 'unity-editor-*', 'Editor')
289298
paths = Dir[find]
290299
paths = paths.map { |u| Pathname.new(u).parent.to_s }
291300
UI.verbose "Found list_installed_paths: #{paths}"
@@ -302,6 +311,10 @@ def debian_installed_paths
302311
end
303312

304313
class WindowsInstaller < BaseInstaller
314+
def install_directory
315+
File.expand_path(ENV['U3D_INSTALL_PATH'] || DEFAULT_WINDOWS_INSTALL)
316+
end
317+
305318
def sanitize_install(unity, long: false, dry_run: false)
306319
source_path = File.expand_path(unity.root_path)
307320
parent = File.expand_path('..', source_path)
@@ -314,14 +327,14 @@ def sanitize_install(unity, long: false, dry_run: false)
314327
end
315328

316329
def installed
317-
find = File.join(DEFAULT_WINDOWS_INSTALL, 'Unity*', 'Editor', 'Uninstall.exe')
330+
find = File.join(install_directory, 'Unity*', 'Editor', 'Uninstall.exe')
318331
Dir[find].map { |path| WindowsInstallation.new(root_path: File.expand_path('../..', path)) }
319332
end
320333

321334
def install(file_path, version, installation_path: nil, info: {})
322335
extension = File.extname(file_path)
323336
raise "Installation of #{extension} files is not supported on Windows" unless %w[.exe .msi].include? extension
324-
path = installation_path || File.join(DEFAULT_WINDOWS_INSTALL, format(UNITY_DIR, version: version))
337+
path = installation_path || File.join(install_directory, format(UNITY_DIR, version: version))
325338
install_exe(
326339
file_path,
327340
installation_path: path,
@@ -330,7 +343,7 @@ def install(file_path, version, installation_path: nil, info: {})
330343
end
331344

332345
def install_exe(file_path, installation_path: nil, info: {})
333-
installation_path ||= DEFAULT_WINDOWS_INSTALL
346+
installation_path ||= install_directory
334347
final_path = U3dCore::Helper.windows_path(installation_path)
335348
Utils.ensure_dir(final_path)
336349
begin

spec/u3d/installer_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ class DummyInstaller < U3d::BaseInstaller
160160
describe '.install' do
161161
it 'installs a file in standard installation path' do
162162
filepath = "file.sh"
163-
allow(File).to receive(:directory?).with(U3d::DEFAULT_LINUX_INSTALL) { true }
163+
allow(File).to receive(:directory?).with('/opt') { true }
164164
expect(U3dCore::CommandExecutor).to receive(:execute).with(command: 'chmod a+x file.sh') {}
165-
expect(U3dCore::CommandExecutor).to receive(:execute).with(command: "cd #{U3d::DEFAULT_LINUX_INSTALL}; file.sh", admin: true) {}
165+
expect(U3dCore::CommandExecutor).to receive(:execute).with(command: "cd /opt; file.sh", admin: true) {}
166166

167167
installer = U3d::LinuxInstaller.new
168168
unity = double("UnityProject")

0 commit comments

Comments
 (0)