webdriver_install/lib.rs
1//! Fast and simple webdriver installation
2//!
3//! ## Usage
4//!
5//! By default, driver executables are installed into `$HOME/.webdrivers`.
6//!
7//! ```no_run
8//! # fn main() -> eyre::Result<()> {
9//! use webdriver_install::Driver;
10//!
11//! // Install geckodriver into $HOME/.webdrivers
12//! Driver::Gecko.install()?;
13//!
14//! // Install chromedriver into $HOME/.webdrivers
15//! Driver::Chrome.install()?;
16//! # Ok(())
17//! # }
18//! ```
19//!
20//! You can specify a different location with [`Driver::install_into`]:
21//!
22//! ```no_run
23//! # fn main() -> eyre::Result<()> {
24//! use webdriver_install::Driver;
25//! use std::path::PathBuf;
26//!
27//! // Install geckodriver into /tmp/webdrivers
28//! Driver::Gecko.install_into(PathBuf::from("/tmp/webdrivers"))?;
29//!
30//! // Install chromedriver into /tmp/webdrivers
31//! Driver::Chrome.install_into(PathBuf::from("/tmp/webdrivers"))?;
32//! # Ok(())
33//! # }
34//! ```
35
36mod chromedriver;
37mod geckodriver;
38pub mod installer;
39
40use eyre::Result;
41pub use installer::Driver;
42use url::Url;
43
44#[doc(hidden)]
45pub trait DriverFetcher {
46 const BASE_URL: &'static str;
47
48 fn latest_version(&self) -> Result<String>;
49
50 fn direct_download_url(&self, version: &str) -> Result<Url>;
51}
52
53#[must_use]
54#[cfg(target_os = "windows")]
55fn run_powershell_cmd(cmd: &str) -> std::process::Output {
56 use std::process::{Command, Stdio};
57 use tracing::debug;
58
59 let mut ps = Command::new("powershell");
60 ps.stdin(Stdio::piped());
61 ps.stdout(Stdio::piped());
62 ps.stderr(Stdio::piped());
63
64 let process = ps.args(&["-Command", cmd]).spawn().unwrap();
65
66 let output = process
67 .wait_with_output()
68 .expect("failed to wait on child process");
69 debug!(
70 "stdout: {:?}",
71 String::from_utf8(output.clone().stdout).unwrap()
72 );
73 debug!(
74 "stderr: {:?}",
75 String::from_utf8(output.clone().stderr).unwrap()
76 );
77 output
78}