|
| 1 | +require 'base64' |
| 2 | +require 'shellwords' # fingers crossed this works on win32 |
| 3 | +require 'win32' |
| 4 | +require "arduino_ci/arduino_downloader" |
| 5 | + |
| 6 | +module ArduinoCI |
| 7 | + |
| 8 | + # Manage the POSIX download & install of Arduino |
| 9 | + class ArduinoDownloaderWindows < ArduinoDownloader |
| 10 | + |
| 11 | + def powershell(*args) |
| 12 | + encoded_cmd = Base64.strict_encode64(args.shelljoin.encode('utf-16le')) |
| 13 | + system("powershell.exe", "-encodedCommand", encoded_cmd) |
| 14 | + end |
| 15 | + |
| 16 | + def cygwin(*args) |
| 17 | + system("%CYG_ROOT%/bin/bash", "-lc", args.shelljoin) |
| 18 | + end |
| 19 | + |
| 20 | + # Make any preparations or run any checks prior to making changes |
| 21 | + # @return [string] Error message, or nil if success |
| 22 | + def prepare |
| 23 | + nil |
| 24 | + end |
| 25 | + |
| 26 | + # The technology that will be used to complete the download |
| 27 | + # (for logging purposes) |
| 28 | + # @return [string] |
| 29 | + def downloader |
| 30 | + "wget" |
| 31 | + end |
| 32 | + |
| 33 | + # Download the package_url to package_file |
| 34 | + # @return [bool] whether successful |
| 35 | + def download |
| 36 | + powershell("(New-Object Net.WebClient).DownloadFile('#{package_url}', '#{package_file}')") |
| 37 | + end |
| 38 | + |
| 39 | + # Move the extracted package file from extracted_file to the force_install_location |
| 40 | + # @return [bool] whether successful |
| 41 | + def install |
| 42 | + powershell("Move-Item", extracted_file, self.class.force_install_location) |
| 43 | + end |
| 44 | + |
| 45 | + # The local filename of the desired IDE package (zip/tar/etc) |
| 46 | + # @return [string] |
| 47 | + def package_file |
| 48 | + "#{extracted_file}-windows.zip" |
| 49 | + end |
| 50 | + |
| 51 | + # The technology that will be used to extract the download |
| 52 | + # (for logging purposes) |
| 53 | + # @return [string] |
| 54 | + def extracter |
| 55 | + "Expand-Archive" |
| 56 | + end |
| 57 | + |
| 58 | + # Extract the package_file to extracted_file |
| 59 | + # @return [bool] whether successful |
| 60 | + def extract |
| 61 | + powershell("Expand-Archive", package_file, "-dest", extracted_file) |
| 62 | + end |
| 63 | + |
| 64 | + # The local file (dir) name of the extracted IDE package (zip/tar/etc) |
| 65 | + # @return [string] |
| 66 | + def extracted_file |
| 67 | + "arduino-#{@desired_ide_version}" |
| 68 | + end |
| 69 | + |
| 70 | + # The path to the directory of an existing installation, or nil |
| 71 | + # @return [string] |
| 72 | + def self.existing_installation |
| 73 | + exe = self.existing_executable |
| 74 | + return nil if exe.nil? |
| 75 | + File.dirname(exe) |
| 76 | + end |
| 77 | + |
| 78 | + # The executable Arduino file in an existing installation, or nil |
| 79 | + # @return [string] |
| 80 | + def self.existing_executable |
| 81 | + arduino_reg = 'Software\SOFTWARE\Classes\Arduino file\shell\open\command' |
| 82 | + Win32::Registry::HKEY_LOCAL_MACHINE.open(arduino_reg) do |reg| |
| 83 | + reg.each_key do |key| |
| 84 | + k = reg.open(key) |
| 85 | + puts key |
| 86 | + puts k |
| 87 | + return k |
| 88 | + # puts k["DisplayName"] rescue "?" |
| 89 | + # puts k["DisplayVersion"] rescue "?" |
| 90 | + # puts |
| 91 | + end |
| 92 | + end |
| 93 | + nil |
| 94 | + end |
| 95 | + |
| 96 | + # The executable Arduino file in a forced installation, or nil |
| 97 | + # @return [string] |
| 98 | + def self.force_installed_executable |
| 99 | + exe = File.join(self.force_install_location, "arduino.exe") |
| 100 | + return nil if exe.nil? |
| 101 | + exe |
| 102 | + end |
| 103 | + |
| 104 | + end |
| 105 | +end |
0 commit comments