]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/serve.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / clippy_dev / src / serve.rs
1 use std::ffi::OsStr;
2 use std::num::ParseIntError;
3 use std::path::Path;
4 use std::process::Command;
5 use std::thread;
6 use std::time::{Duration, SystemTime};
7
8 /// # Panics
9 ///
10 /// Panics if the python commands could not be spawned
11 pub fn run(port: u16, lint: Option<&String>) -> ! {
12     let mut url = Some(match lint {
13         None => format!("http://localhost:{port}"),
14         Some(lint) => format!("http://localhost:{port}/#{lint}"),
15     });
16
17     loop {
18         if mtime("util/gh-pages/lints.json") < mtime("clippy_lints/src") {
19             Command::new("cargo")
20                 .arg("collect-metadata")
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()
52             .map(|entry| mtime(entry.path()))
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)]
63 pub fn validate_port(arg: &OsStr) -> Result<(), ParseIntError> {
64     arg.to_string_lossy().parse::<u16>().map(|_| ())
65 }