lib/xcode/install.rb in xcode-install-2.6.6 vs lib/xcode/install.rb in xcode-install-2.6.7

- old
+ new

@@ -23,17 +23,19 @@ # @param cookies: Any cookies we should use for the download (used for auth with Apple) # @param output: A PathName for where we want to store the file # @param progress: parse and show the progress? # @param progress_block: A block that's called whenever we have an updated progress % # the parameter is a single number that's literally percent (e.g. 1, 50, 80 or 100) + # @param retry_download_count: A count to retry the downloading Xcode dmg/xip # rubocop:disable Metrics/AbcSize def fetch(url: nil, directory: nil, cookies: nil, output: nil, progress: nil, - progress_block: nil) + progress_block: nil, + retry_download_count: 3) options = cookies.nil? ? [] : ['--cookie', cookies, '--cookie-jar', COOKIES_PATH] uri = URI.parse(url) output ||= File.basename(uri.path) output = (Pathname.new(directory) + Pathname.new(output)) if directory @@ -76,11 +78,11 @@ # Run the curl command in a loop, retry when curl exit status is 18 # "Partial file. Only a part of the file was transferred." # https://curl.haxx.se/mail/archive-2008-07/0098.html # https://github.com/KrauseFx/xcode-install/issues/210 - 3.times do + retry_download_count.times do # Non-blocking call of Open3 # We're not using the block based syntax, as the bacon testing # library doesn't seem to support writing tests for it stdin, stdout, stderr, wait_thr = Open3.popen3(command_string) @@ -133,11 +135,11 @@ def current_symlink File.symlink?(SYMLINK_PATH) ? SYMLINK_PATH : nil end - def download(version, progress, url = nil, progress_block = nil) + def download(version, progress, url = nil, progress_block = nil, retry_download_count = 3) xcode = find_xcode_version(version) if url.nil? return if url.nil? && xcode.nil? dmg_file = Pathname.new(File.basename(url || xcode.path)) @@ -145,11 +147,12 @@ url: url || xcode.url, directory: CACHE_DIR, cookies: url ? nil : spaceship.cookie, output: dmg_file, progress: progress, - progress_block: progress_block + progress_block: progress_block, + retry_download_count: retry_download_count ) result ? CACHE_DIR + dmg_file : nil end def find_xcode_version(version) @@ -278,12 +281,12 @@ FileUtils.rm_f(dmg_path) if clean end # rubocop:disable Metrics/ParameterLists - def install_version(version, switch = true, clean = true, install = true, progress = true, url = nil, show_release_notes = true, progress_block = nil) - dmg_path = get_dmg(version, progress, url, progress_block) + def install_version(version, switch = true, clean = true, install = true, progress = true, url = nil, show_release_notes = true, progress_block = nil, retry_download_count = 3) + dmg_path = get_dmg(version, progress, url, progress_block, retry_download_count) fail Informative, "Failed to download Xcode #{version}." if dmg_path.nil? if install install_dmg(dmg_path, "-#{version.to_s.split(' ').join('.')}", switch, clean) else @@ -312,11 +315,11 @@ installed.include?(version) ? "#{x} (installed)" : x end.join("\n") end def list - list_annotated(list_versions.sort_by(&:to_f)) + list_annotated(list_versions.sort { |first, second| compare_versions(first, second) }) end def rm_list_cache FileUtils.rm_f(LIST_FILE) end @@ -368,22 +371,22 @@ def enable_developer_mode `sudo /usr/sbin/DevToolsSecurity -enable` `sudo /usr/sbin/dseditgroup -o edit -t group -a staff _developer` end - def get_dmg(version, progress = true, url = nil, progress_block = nil) + def get_dmg(version, progress = true, url = nil, progress_block = nil, retry_download_count = 3) if url path = Pathname.new(url) return path if path.exist? end if ENV.key?('XCODE_INSTALL_CACHE_DIR') Pathname.glob(ENV['XCODE_INSTALL_CACHE_DIR'] + '/*').each do |fpath| return fpath if /^xcode_#{version}\.dmg|xip$/ =~ fpath.basename.to_s end end - download(version, progress, url, progress_block) + download(version, progress, url, progress_block, retry_download_count) end def fetch_seedlist @xcodes = parse_seedlist(spaceship.send(:request, :post, '/services-account/QH65B2/downloadws/listDownloads.action').body) @@ -459,10 +462,31 @@ end links end + def compare_versions(first, second) + # Sort by version number + numeric_comparation = first.to_f <=> second.to_f + return numeric_comparation if numeric_comparation != 0 + + # Return beta versions before others + is_first_beta = first.include?('beta') + is_second_beta = second.include?('beta') + return -1 if is_first_beta && !is_second_beta + return 1 if !is_first_beta && is_second_beta + + # Return GM versions before others + is_first_gm = first.include?('GM') + is_second_gm = second.include?('GM') + return -1 if is_first_gm && !is_second_gm + return 1 if !is_first_gm && is_second_gm + + # Sort alphabetically + first <=> second + end + def hdiutil(*args) io = IO.popen(['hdiutil', *args]) result = io.read io.close unless $?.exitstatus.zero? @@ -510,15 +534,16 @@ s.version == version end end end - def download(progress, progress_block = nil) + def download(progress, progress_block = nil, retry_download_count = 3) result = Curl.new.fetch( url: source, directory: CACHE_DIR, progress: progress, - progress_block: progress_block + progress_block: progress_block, + retry_download_count: retry_download_count ) result ? dmg_path : nil end def install(progress, should_install)