|
| 1 | +DOWNLOAD_ATTEMPTS = 3 |
| 2 | + |
| 3 | +module ArduinoCI |
| 4 | + |
| 5 | + # Manage the OS-specific download & install of Arduino |
| 6 | + class ArduinoDownloader |
| 7 | + |
| 8 | + def initialize(desired_ide_version) |
| 9 | + @desired_ide_version = desired_ide_version |
| 10 | + end |
| 11 | + |
| 12 | + # Provide guidelines to the implementer of this class |
| 13 | + def must_implement(method) |
| 14 | + raise NoMethodError("#{self.class.name} failed to implement ArduinoDownloader.#{method}") |
| 15 | + end |
| 16 | + |
| 17 | + # Make any preparations or run any checks prior to making changes |
| 18 | + # @return [string] Error message, or nil if success |
| 19 | + def prepare |
| 20 | + nil |
| 21 | + end |
| 22 | + |
| 23 | + # The autolocated executable of the installation |
| 24 | + # |
| 25 | + # @return [string] or nil |
| 26 | + def self.autolocation |
| 27 | + # Arbitrarily, I'm going to pick the force installed location first |
| 28 | + # if it exists. I'm not sure why we would have both, but if we did |
| 29 | + # a force install then let's make sure we actually use it. |
| 30 | + locations = [self.force_installed_executable, self.existing_executable] |
| 31 | + locations.each do |loc| |
| 32 | + next if loc.nil? |
| 33 | + next unless File.exist? loc |
| 34 | + return loc |
| 35 | + end |
| 36 | + nil |
| 37 | + end |
| 38 | + |
| 39 | + # The executable Arduino file in an existing installation, or nil |
| 40 | + # @return [string] |
| 41 | + def self.existing_executable |
| 42 | + must_implement(__method__) |
| 43 | + end |
| 44 | + |
| 45 | + # The executable Arduino file in a forced installation, or nil |
| 46 | + # @return [string] |
| 47 | + def self.force_installed_executable |
| 48 | + must_implement(__method__) |
| 49 | + end |
| 50 | + |
| 51 | + # The technology that will be used to complete the download |
| 52 | + # (for logging purposes) |
| 53 | + # @return [string] |
| 54 | + def downloader |
| 55 | + must_implement(__method__) |
| 56 | + end |
| 57 | + |
| 58 | + # The technology that will be used to extract the download |
| 59 | + # (for logging purposes) |
| 60 | + # @return [string] |
| 61 | + def extracter |
| 62 | + must_implement(__method__) |
| 63 | + end |
| 64 | + |
| 65 | + # The URL of the desired IDE package (zip/tar/etc) for this platform |
| 66 | + # @return [string] |
| 67 | + def package_url |
| 68 | + "https://downloads.arduino.cc/#{package_file}" |
| 69 | + end |
| 70 | + |
| 71 | + # The local file (dir) name of the desired IDE package (zip/tar/etc) |
| 72 | + # @return [string] |
| 73 | + def package_file |
| 74 | + must_implement(__method__) |
| 75 | + end |
| 76 | + |
| 77 | + # The local filename of the extracted IDE package (zip/tar/etc) |
| 78 | + # @return [string] |
| 79 | + def extracted_file |
| 80 | + must_implement(__method__) |
| 81 | + end |
| 82 | + |
| 83 | + # @return [String] The location where a forced install will go |
| 84 | + def self.force_install_location |
| 85 | + File.join(ENV['HOME'], 'arduino_ci_ide') |
| 86 | + end |
| 87 | + |
| 88 | + # Download the package_url to package_file, and maybe print a line of dots...... |
| 89 | + # @return [bool] whether successful |
| 90 | + def download |
| 91 | + must_implement(__method__) |
| 92 | + end |
| 93 | + |
| 94 | + # Extract the package_file to extracted_file |
| 95 | + # @return [bool] whether successful |
| 96 | + def extract |
| 97 | + must_implement(__method__) |
| 98 | + end |
| 99 | + |
| 100 | + # Move the extracted package file from extracted_file to the force_install_location |
| 101 | + # @return [bool] whether successful |
| 102 | + def install |
| 103 | + must_implement(__method__) |
| 104 | + end |
| 105 | + |
| 106 | + # Forcibly install Arduino on linux from the web |
| 107 | + # @return [bool] Whether the command succeeded |
| 108 | + def execute |
| 109 | + error_preparing = prepare |
| 110 | + unless error_preparing.nil? |
| 111 | + puts "Arduino force-install failed preparation: #{error_preparing}" |
| 112 | + return false |
| 113 | + end |
| 114 | + |
| 115 | + attempts = 0 |
| 116 | + |
| 117 | + loop do |
| 118 | + if File.exist? package_file |
| 119 | + puts "Arduino package seems to have been downloaded already" if attempts.zero? |
| 120 | + break |
| 121 | + elsif attempts >= DOWNLOAD_ATTEMPTS |
| 122 | + break puts "After #{DOWNLOAD_ATTEMPTS} attempts, failed to download #{package_url}" |
| 123 | + else |
| 124 | + puts "Attempting to download Arduino package with #{downloader}" |
| 125 | + download |
| 126 | + end |
| 127 | + attempts += 1 |
| 128 | + end |
| 129 | + |
| 130 | + if File.exist? extracted_file |
| 131 | + puts "Arduino package seems to have been extracted already" |
| 132 | + elsif File.exist? package_file |
| 133 | + puts "Extracting archive with #{extracter}" |
| 134 | + extract |
| 135 | + end |
| 136 | + |
| 137 | + if File.exist? self.class.force_install_location |
| 138 | + puts "Arduino package seems to have been installed already" |
| 139 | + elsif File.exist? extracted_file |
| 140 | + install |
| 141 | + else |
| 142 | + puts "Arduino force-install failed" |
| 143 | + end |
| 144 | + |
| 145 | + File.exist? self.class.force_install_location |
| 146 | + end |
| 147 | + |
| 148 | + end |
| 149 | +end |
0 commit comments