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