|
| 1 | +use std::ffi::{OsStr, OsString}; |
| 2 | +use std::path::Path; |
| 3 | +use std::process::Command; |
| 4 | +use std::thread; |
| 5 | +use std::time::{Duration, SystemTime}; |
| 6 | + |
| 7 | +pub fn run(port: u16, lint: Option<&str>) -> ! { |
| 8 | + let mut url = Some(match lint { |
| 9 | + None => format!("http://localhost:{}", port), |
| 10 | + Some(lint) => format!("http://localhost:{}/#{}", port, lint), |
| 11 | + }); |
| 12 | + |
| 13 | + loop { |
| 14 | + if mtime("util/gh-pages/lints.json") < mtime("clippy_lints/src") { |
| 15 | + Command::new("python3") |
| 16 | + .arg("util/export.py") |
| 17 | + .spawn() |
| 18 | + .unwrap() |
| 19 | + .wait() |
| 20 | + .unwrap(); |
| 21 | + } |
| 22 | + if let Some(url) = url.take() { |
| 23 | + thread::spawn(move || { |
| 24 | + Command::new("python3") |
| 25 | + .arg("-m") |
| 26 | + .arg("http.server") |
| 27 | + .arg(port.to_string()) |
| 28 | + .current_dir("util/gh-pages") |
| 29 | + .spawn() |
| 30 | + .unwrap(); |
| 31 | + // Give some time for python to start |
| 32 | + thread::sleep(Duration::from_millis(500)); |
| 33 | + // Launch browser after first export.py has completed and http.server is up |
| 34 | + let _ = opener::open(url); |
| 35 | + }); |
| 36 | + } |
| 37 | + thread::sleep(Duration::from_millis(1000)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn mtime(path: impl AsRef<Path>) -> SystemTime { |
| 42 | + let path = path.as_ref(); |
| 43 | + if path.is_dir() { |
| 44 | + path.read_dir() |
| 45 | + .into_iter() |
| 46 | + .flatten() |
| 47 | + .flatten() |
| 48 | + .map(|entry| mtime(&entry.path())) |
| 49 | + .max() |
| 50 | + .unwrap_or(SystemTime::UNIX_EPOCH) |
| 51 | + } else { |
| 52 | + path.metadata() |
| 53 | + .and_then(|metadata| metadata.modified()) |
| 54 | + .unwrap_or(SystemTime::UNIX_EPOCH) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[allow(clippy::missing_errors_doc)] |
| 59 | +pub fn validate_port(arg: &OsStr) -> Result<(), OsString> { |
| 60 | + match arg.to_string_lossy().parse::<u16>() { |
| 61 | + Ok(_port) => Ok(()), |
| 62 | + Err(err) => Err(OsString::from(err.to_string())), |
| 63 | + } |
| 64 | +} |
0 commit comments