]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/serve.rs
Rollup merge of #86479 - exphp-forks:float-debug-exponential, r=yaahc
[rust.git] / src / tools / clippy / clippy_dev / src / serve.rs
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 /// # Panics
8 ///
9 /// Panics if the python commands could not be spawned
10 pub fn run(port: u16, lint: Option<&str>) -> ! {
11     let mut url = Some(match lint {
12         None => format!("http://localhost:{}", port),
13         Some(lint) => format!("http://localhost:{}/#{}", port, lint),
14     });
15
16     loop {
17         if mtime("util/gh-pages/lints.json") < mtime("clippy_lints/src") {
18             Command::new("cargo")
19                 .arg("collect-metadata")
20                 .spawn()
21                 .unwrap()
22                 .wait()
23                 .unwrap();
24         }
25         if let Some(url) = url.take() {
26             thread::spawn(move || {
27                 Command::new("python3")
28                     .arg("-m")
29                     .arg("http.server")
30                     .arg(port.to_string())
31                     .current_dir("util/gh-pages")
32                     .spawn()
33                     .unwrap();
34                 // Give some time for python to start
35                 thread::sleep(Duration::from_millis(500));
36                 // Launch browser after first export.py has completed and http.server is up
37                 let _result = opener::open(url);
38             });
39         }
40         thread::sleep(Duration::from_millis(1000));
41     }
42 }
43
44 fn mtime(path: impl AsRef<Path>) -> SystemTime {
45     let path = path.as_ref();
46     if path.is_dir() {
47         path.read_dir()
48             .into_iter()
49             .flatten()
50             .flatten()
51             .map(|entry| mtime(&entry.path()))
52             .max()
53             .unwrap_or(SystemTime::UNIX_EPOCH)
54     } else {
55         path.metadata()
56             .and_then(|metadata| metadata.modified())
57             .unwrap_or(SystemTime::UNIX_EPOCH)
58     }
59 }
60
61 #[allow(clippy::missing_errors_doc)]
62 pub fn validate_port(arg: &OsStr) -> Result<(), OsString> {
63     match arg.to_string_lossy().parse::<u16>() {
64         Ok(_port) => Ok(()),
65         Err(err) => Err(OsString::from(err.to_string())),
66     }
67 }