Skip to content

Commit

Permalink
u3d/install: add support for installing .msi packages on Windows (#230)
Browse files Browse the repository at this point in the history
* Support .msi installers

* Rubocop compliance

* Move Windows path sanization to Utils

* Make proper windows_path
  • Loading branch information
niezbop authored Jan 15, 2018
1 parent 1858029 commit 442610f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/u3d/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Downloader
# Path to the directory for the package downloading
DOWNLOAD_PATH = "#{ENV['HOME']}/Downloads".freeze
# Regex to get the name of a package out of its file name
UNITY_MODULE_FILE_REGEX = %r{\/([\w\-_\.\+]+\.(?:pkg|exe|zip|sh|deb))}
UNITY_MODULE_FILE_REGEX = %r{\/([\w\-_\.\+]+\.(?:pkg|exe|zip|sh|deb|msi))[^\/]*$}

class << self
def download_directory
Expand Down
12 changes: 8 additions & 4 deletions lib/u3d/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def installed

def install(file_path, version, installation_path: nil, info: {})
extension = File.extname(file_path)
raise "Installation of #{extension} files is not supported on Windows" if extension != '.exe'
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))
install_exe(
file_path,
Expand All @@ -306,13 +306,17 @@ def install(file_path, version, installation_path: nil, info: {})

def install_exe(file_path, installation_path: nil, info: {})
installation_path ||= DEFAULT_WINDOWS_INSTALL
final_path = installation_path.tr('/', '\\')
final_path = Utils.windows_path(installation_path)
Utils.ensure_dir(final_path)
begin
command = nil
if info['cmd']
command = info['cmd']
command.sub!(/{FILENAME}/, file_path)
if /msiexec/ =~ command
command.sub!(/{FILENAME}/, '"' + Utils.windows_path(file_path) + '"')
else
command.sub!(/{FILENAME}/, file_path)
end
command.sub!(/{INSTDIR}/, final_path)
command.sub!(/{DOCDIR}/, final_path)
command.sub!(/{MODULEDIR}/, final_path)
Expand All @@ -321,7 +325,7 @@ def install_exe(file_path, installation_path: nil, info: {})
command ||= file_path.to_s
U3dCore::CommandExecutor.execute(command: command, admin: true)
rescue StandardError => e
UI.error "Failed to install exe at #{file_path}: #{e}"
UI.error "Failed to install package at #{file_path}: #{e}"
else
UI.success "Successfully installed #{info['title']}"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/u3d/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def windows_local_appdata
def pretty_filesize(filesize)
Filesize.from(filesize.round.to_s + ' B').pretty
end

def windows_path(path)
path.gsub(%r{\/(\d)}, '/\\\\\1').tr('/', '\\')
end
end
end
# rubocop:enable ModuleLength
Expand Down

0 comments on commit 442610f

Please sign in to comment.