Skip to content

Commit

Permalink
Merge pull request #58 from mikebirdgeneau/main
Browse files Browse the repository at this point in the history
Consider removal of dependency on `sys_info` crate
  • Loading branch information
phansch authored Aug 21, 2024
2 parents cb21642 + bbefab8 commit 8bc6253
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ dirs-sys = "0.3"
eyre = "0.6"
flate2 = "1"
regex = "1"
sys-info = "0.9"
tar = "0.4"
zip = "0.5"
tracing = "0.1"
tracing-subscriber = "0.3"
url = "2.2"
serde_json = "1.0.120"

[dependencies.winapi]
features = ["winuser"]
version = "0.3"

[dependencies.reqwest]
features = ["blocking"]
features = ["blocking", "json"]
version = "0.11"

[dev-dependencies]
Expand Down
5 changes: 5 additions & 0 deletions examples/latest_chromedriver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use webdriver_install::Driver;

fn main() {
Driver::Chrome.install().unwrap();
}
44 changes: 25 additions & 19 deletions src/chromedriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
/// See https://chromedriver.chromium.org/downloads/version-selection
use eyre::{eyre, Result};
use regex::Regex;
use tracing::debug;
use tracing::{debug};
use url::Url;
use serde_json::Value;

use std::process::{Command, Stdio};

Expand All @@ -19,24 +20,29 @@ use std::path::PathBuf;
pub struct Chromedriver;

impl DriverFetcher for Chromedriver {
const BASE_URL: &'static str = "https://chromedriver.storage.googleapis.com";
const BASE_URL: &'static str = "https://storage.googleapis.com/chrome-for-testing-public";

/// Returns the latest version of the driver
fn latest_version(&self) -> Result<String> {
let latest_release_url = format!(
"{}/LATEST_RELEASE_{}",
Self::BASE_URL,
Version::find()?.build_version()
);
debug!("latest_release_url: {}", latest_release_url);
let resp = reqwest::blocking::get(&latest_release_url)?;
Ok(resp.text()?)
}
const VERSION_URL: &'static str = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json";
let version_response = reqwest::blocking::get(VERSION_URL)?;
let data: Value = version_response.json()?;

// Extract the last element from the `versions` array and get the `version` field
if let Some(last_version) = data["versions"].as_array().and_then(|v| v.last()) {
if let Some(version) = last_version["version"].as_str() {
debug!("Latest version: {}", version);
return Ok(version.to_string());
}
}

Err(eyre!("Could not find the latest version"))
}

/// Returns the download url for the driver executable
fn direct_download_url(&self, version: &str) -> Result<Url> {
Ok(Url::parse(&format!(
"{}/{version}/chromedriver_{platform}",
"{}/{version}/{platform}/chromedriver-{platform}.zip",
Self::BASE_URL,
version = version,
platform = Self::platform()?
Expand All @@ -57,10 +63,10 @@ impl Chromedriver {
/// If future chromedriver releases have multiple pointer widths per platform,
/// we have to change this to work like `Geckodriver::platform`.
fn platform() -> Result<String> {
match sys_info::os_type()?.as_str() {
"Linux" => Ok(String::from("linux64.zip")),
"Darwin" => Ok(String::from("mac64.zip")),
"Windows" => Ok(String::from("win32.zip")),
match std::env::consts::OS {
"linux" => Ok(String::from("linux64")),
"macos" => Ok(String::from("mac64")),
"windows" => Ok(String::from("win32")),
other => Err(eyre!(
"webdriver-install doesn't support '{}' currently",
other
Expand Down Expand Up @@ -298,23 +304,23 @@ fn version_from_output_panic_not_4_parts_test() {
fn direct_download_url_test() {
#[cfg(target_os = "linux")]
assert_eq!(
"https://chromedriver.storage.googleapis.com/v1/chromedriver_linux64.zip",
"https://storage.googleapis.com/chrome-for-testing-public/v1/linux64/chromedriver-linux64.zip",
Chromedriver::new()
.direct_download_url("v1")
.unwrap()
.to_string()
);
#[cfg(target_os = "macos")]
assert_eq!(
"https://chromedriver.storage.googleapis.com/v1/chromedriver_mac64.zip",
"https://storage.googleapis.com/chrome-for-testing-public/v1/mac64/chromedriver-mac64.zip",
Chromedriver::new()
.direct_download_url("v1")
.unwrap()
.to_string()
);
#[cfg(target_os = "windows")]
assert_eq!(
"https://chromedriver.storage.googleapis.com/v1/chromedriver_win32.zip",
"https://storage.googleapis.com/chrome-for-testing-public/v1/win32/chromedriver-win32.zip",
Chromedriver::new()
.direct_download_url("v1")
.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions src/geckodriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl Geckodriver {
}

fn platform() -> Result<String> {
match sys_info::os_type()?.as_str() {
"Linux" => Ok(format!("linux{}.tar.gz", Self::pointer_width())),
"Darwin" => Ok(String::from("macos.tar.gz")),
"Windows" => Ok(format!("win{}.zip", Self::pointer_width())),
match std::env::consts::OS {
"linux" => Ok(format!("linux{}.tar.gz", Self::pointer_width())),
"macos" => Ok(String::from("macos.tar.gz")),
"windows" => Ok(format!("win{}.zip", Self::pointer_width())),
other => Err(eyre!(
"webdriver-install doesn't support '{}' currently",
other
Expand Down
10 changes: 6 additions & 4 deletions src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ fn decompress(archive_filename: &str, bytes: &[u8], target_dir: PathBuf) -> Resu
let mut filename: Option<String> = None;
for i in 0..zip.len() {
let mut file = zip.by_index(i)?;
if DRIVER_EXECUTABLES.contains(&file.name()) {
filename = Some(file.name().to_string());
file.read_to_end(&mut zip_bytes)?;
break;
if let Some(file_name) = std::path::Path::new(file.name()).file_name().and_then(|n| n.to_str()){
if DRIVER_EXECUTABLES.contains(&file_name) {
filename = Some(file_name.to_string());
file.read_to_end(&mut zip_bytes)?;
break;
}
}
}
if let Some(name) = filename {
Expand Down

0 comments on commit 8bc6253

Please sign in to comment.