lib/u3d/downloader.rb in u3d-1.1.5 vs lib/u3d/downloader.rb in u3d-1.2.0
- old
+ new
@@ -28,10 +28,12 @@
module Downloader
# Name of the directory for the package downloading
DOWNLOAD_DIRECTORY = 'Unity_Packages'.freeze
# Path to the directory for the package downloading
DOWNLOAD_PATH = "#{ENV['HOME']}/Downloads".freeze
+ # Regex to get the name of a localization asset
+ UNITY_LANGUAGE_FILE_REGEX = %r{\/\d+/[0-9\.]+\/([\w-]+)$}
# Regex to get the name of a package out of its file name
UNITY_MODULE_FILE_REGEX = %r{\/([\w\-_\.\+]+\.(?:pkg|exe|zip|sh|deb|msi|xz))[^\/]*$}
class << self
def download_directory
@@ -99,32 +101,33 @@
return validator, downloader
end
def get_package(downloader, validator, package, definition, files)
path, url = downloader.destination_and_url_for(package, definition)
+ package_info = definition[package]
if File.file?(path)
- UI.verbose "Installer file for #{package} seems to be present at #{path}"
+ UI.verbose "Installer file for #{package_info.name} seems to be present at #{path}"
if validator.validate(package, path, definition)
- UI.message "#{package.capitalize} is already downloaded"
- files << [package, path, definition[package]]
+ UI.message "#{package_info.name} is already downloaded"
+ files << [package, path, package_info]
return
else
extension = File.extname(path)
new_path = File.join(File.dirname(path), File.basename(path, extension) + '_CORRUPTED' + extension)
UI.important "File present at #{path} is not correct, it has been renamed to #{new_path}"
File.rename(path, new_path)
end
end
- UI.header "Downloading #{package} version #{definition.version}"
+ UI.header "Downloading #{package_info.name} version #{definition.version}"
UI.message 'Downloading from ' + url.to_s.cyan.underline
UI.message 'Download will be found at ' + path
- download_package(path, url, size: definition.size_in_bytes(package))
+ download_package(path, url, size: package_info.download_size)
if validator.validate(package, path, definition)
UI.success "Successfully downloaded #{package}."
- files << [package, path, definition[package]]
+ files << [package, path, package_info]
else
UI.error "Failed to download #{package}"
end
end
@@ -146,18 +149,25 @@
def destination_and_url_for(package, definition)
final_url = url_for(package, definition)
dir = File.join(Downloader.download_directory, definition.version)
Utils.ensure_dir(dir)
- file_name = UNITY_MODULE_FILE_REGEX.match(final_url)[1]
+ file_name = if (language_match = UNITY_LANGUAGE_FILE_REGEX.match(final_url))
+ language_match[1] + '.po' # Unity uses PO (Portable object files) for localization
+ elsif (module_match = UNITY_MODULE_FILE_REGEX.match(final_url))
+ module_match[1]
+ else
+ raise "Unable to download file at #{final_url}. Please report it to the u3d issues on Github: https://github.com/DragonBox/u3d/issues/new"
+ end
+
destination = File.expand_path(file_name, dir)
[destination, final_url]
end
def url_for(package, definition)
- url = definition[package]['url']
+ url = definition[package].url
if url
if url =~ /^http/
Utils.final_url(url)
else
definition.url + url