Class: ChromedriverUpdate
- Inherits:
-
Object
- Object
- ChromedriverUpdate
- Defined in:
- lib/chromedriver_update.rb,
lib/chromedriver_update/version.rb
Constant Summary collapse
- CHROME_DOWNLOADS_LIST_URL =
'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
- VERSION =
'0.3.1'.freeze
Class Method Summary collapse
-
.auto_update_chromedriver(force: false) ⇒ Object
Update the installed version of chromedriver automatically fitting to the currently installed version of chrome.
-
.chromedriver_closest_link_for_version(version) ⇒ String
Get the download URL of the closest chromedriver version fitting the given chrome version.
- .chromedriver_closest_version_for_version(version) ⇒ Object
- .chromedriver_link_for_version(version) ⇒ Object
-
.chromedriver_path ⇒ String
Get the path of the installed chromedriver.
-
.installed_chrome_version ⇒ String
Get the currently installed version of chrome.
-
.installed_chromedriver_version ⇒ String
Get the currently installed version of chromedriver.
-
.mac_platform ⇒ String
Detect macOS platform.
- .print_version_status ⇒ Object
-
.remote_file_exists?(url) ⇒ Boolean
Check if a remote file exists, without downloading it, by checking header (OPTIONS).
Class Method Details
.auto_update_chromedriver(force: false) ⇒ Object
Update the installed version of chromedriver automatically fitting to the currently installed version of chrome
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/chromedriver_update.rb', line 21 def self.auto_update_chromedriver(force: false) if installed_chrome_version.split(".").first != installed_chromedriver_version.split(".").first || force original_chromedriver_version = installed_chromedriver_version original_chromedriver_path = chromedriver_path puts "Downloading chromedriver ..." chromedriver_zip = HTTParty.get(chromedriver_link_for_version(installed_chrome_version)) if chromedriver_zip.code == 404 # fallback to latest lower version puts "Could not find same chromedriver version for chrome version '#{installed_chrome_version}'. Fallback to closest version '#{installed_chrome_version}'" puts "-> #{chromedriver_closest_link_for_version(installed_chrome_version)}" chromedriver_zip = HTTParty.get(chromedriver_closest_link_for_version(installed_chrome_version)) else puts "Found same chromedriver version '#{installed_chrome_version}' for chrome version '#{installed_chrome_version}'" puts "-> #{chromedriver_link_for_version(installed_chrome_version)}" end Dir.mktmpdir do |destination_dir| Zip::File.open_buffer(chromedriver_zip.body) do |zip_files| zip_files.each do |entry| if (entry.name.end_with?("/chromedriver") || entry.name.end_with?("/chromedriver.exe")) download_path = File.join(destination_dir, File.basename(entry.name)) entry.extract(download_path) FileUtils.chmod("+x", download_path) unless File.writable?(original_chromedriver_path) puts "Permission denied to overwrite current chromedriver. Please run the script as admin or change the file permissions of the current chromedriver." exit 1 end FileUtils.mv(download_path, chromedriver_path, force: true) unless OS.windows? begin FileUtils.chmod("+x", original_chromedriver_path) rescue begin `sudo chmod +x "#{original_chromedriver_path}"` rescue end end end end end end end puts "Updated Chromedriver from '#{original_chromedriver_version}' to '#{installed_chromedriver_version}'! Chrome is '#{installed_chrome_version}'." else puts "Chromedriver is already up to date at major version! Chromedriver: '#{installed_chromedriver_version}' Chrome: '#{installed_chrome_version}'" end end |
.chromedriver_closest_link_for_version(version) ⇒ String
Get the download URL of the closest chromedriver version fitting the given chrome version
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/chromedriver_update.rb', line 142 def self.chromedriver_closest_link_for_version(version) platform = if OS.windows? "win64" elsif OS.mac? mac_platform else "linux64" end list = JSON.parse(HTTParty.get(CHROME_DOWNLOADS_LIST_URL).body) latest_match = list['versions'].filter { |el| el['version'].start_with?(version.split(".")[0...1].join(".")) && el['version'].split(".")[2].to_i < version.split(".")[2].to_i && el['downloads']['chromedriver'] }.last latest_match['downloads']['chromedriver'].filter { |el| el['platform'] == platform }.first['url'] end |
.chromedriver_closest_version_for_version(version) ⇒ Object
155 156 157 158 159 |
# File 'lib/chromedriver_update.rb', line 155 def self.chromedriver_closest_version_for_version(version) list = JSON.parse(HTTParty.get(CHROME_DOWNLOADS_LIST_URL).body) latest_match = list['versions'].filter { |el| el['version'].start_with?(version.split(".")[0...1].join(".")) && el['version'].split(".")[2].to_i < version.split(".")[2].to_i && el['downloads']['chromedriver'] }.last latest_match['version'] end |
.chromedriver_link_for_version(version) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/chromedriver_update.rb', line 103 def self.chromedriver_link_for_version(version) if OS.windows? "https://storage.googleapis.com/chrome-for-testing-public/#{version}/win64/chromedriver-win64.zip" elsif OS.mac? "https://storage.googleapis.com/chrome-for-testing-public/#{version}/#{mac_platform}/chromedriver-mac-arm64.zip" else "https://storage.googleapis.com/chrome-for-testing-public/#{version}/linux64/chromedriver-linux64.zip" end end |
.chromedriver_path ⇒ String
Get the path of the installed chromedriver
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/chromedriver_update.rb', line 178 def self.chromedriver_path version = if OS.windows? `where chromedriver`.strip else `which chromedriver`.strip end if version == "" raise ChromedriverNotFoundError.new "Could not detect installed chromedriver!" else version end end |
.installed_chrome_version ⇒ String
Get the currently installed version of chrome
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chromedriver_update.rb', line 72 def self.installed_chrome_version begin if OS.windows? version = `reg query "HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon" /v version` version.scan(/version[^0-9]*([0-9\.]+)/).flatten.first elsif OS.mac? version = `"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --version` version.scan(/([0-9\.]+)/).flatten.first else version = "" begin version = `google-chrome --version` rescue Errno::ENOENT => e version = `google-chrome-stable --version` end version.scan(/([0-9\.]+)/).flatten.first end rescue => e raise ChromeNotFoundError.new "Could not detect a installed chrome version!" end end |
.installed_chromedriver_version ⇒ String
Get the currently installed version of chromedriver
99 100 101 |
# File 'lib/chromedriver_update.rb', line 99 def self.installed_chromedriver_version `#{chromedriver_path} --version`.scan(/([0-9\.]+)/).flatten.first end |
.mac_platform ⇒ String
Detect macOS platform
165 166 167 168 169 170 171 |
# File 'lib/chromedriver_update.rb', line 165 def self.mac_platform if RUBY_PLATFORM.include?("x86") || RUBY_PLATFORM.include?("x64") 'mac-x64' else 'mac-arm64' end end |
.print_version_status ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chromedriver_update.rb', line 114 def self.print_version_status puts "Installed versions" puts "- chrome: #{installed_chrome_version}" puts "- chromedriver: #{installed_chromedriver_version} @ #{chromedriver_path}" puts puts "Available versions of chromedriver" exact = if remote_file_exists? chromedriver_link_for_version(installed_chrome_version) installed_chrome_version else "not found" end closest = if remote_file_exists? chromedriver_closest_link_for_version(installed_chrome_version) chromedriver_closest_version_for_version(installed_chrome_version) else "not found" end puts "- exact match: #{exact}" puts " -> #{chromedriver_link_for_version(installed_chrome_version)}" if exact != "not found" puts "- closest lower match: #{closest}" puts " -> #{chromedriver_closest_link_for_version(installed_chrome_version)}" if closest != "not found" end |
.remote_file_exists?(url) ⇒ Boolean
Check if a remote file exists, without downloading it, by checking header (OPTIONS)
194 195 196 197 |
# File 'lib/chromedriver_update.rb', line 194 def self.remote_file_exists?(url) response = HTTParty.head(url) response.success? end |