]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/crater.rs
cargo dev crater: fixes and debug prints
[rust.git] / clippy_dev / src / crater.rs
1 use std::path::PathBuf;
2 use std::process::Command;
3
4 // represents an archive we download from crates.io
5 #[derive(Debug)]
6 struct KrateSource {
7     version: String,
8     name: String,
9 }
10
11 // represents the extracted sourcecode of a crate
12 #[derive(Debug)]
13 struct Krate {
14     version: String,
15     name: String,
16     path: PathBuf,
17 }
18
19 impl KrateSource {
20     fn new(name: &str, version: &str) -> Self {
21         KrateSource {
22             version: version.into(),
23             name: name.into(),
24         }
25     }
26     fn download_and_extract(&self) -> Krate {
27         let extract_dir = PathBuf::from("target/crater/crates");
28
29         // download
30         let krate_download_dir = PathBuf::from("target/crater/downloads");
31
32         let url = format!(
33             "https://crates.io/api/v1/crates/{}/{}/download",
34             self.name, self.version
35         );
36         println!("Downloading {}, {} / {}", self.name, self.version, url);
37         std::fs::create_dir("target/crater/").unwrap();
38
39         std::fs::create_dir(&krate_download_dir).unwrap();
40         std::fs::create_dir(&extract_dir).unwrap();
41
42         let krate_name = format!("{}-{}.crate.tar.gz", &self.name, &self.version);
43         let mut krate_dest = std::fs::File::create(krate_download_dir.join(krate_name)).unwrap();
44         let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
45         std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
46         let krate = krate_dest;
47         dbg!(&krate);
48         let tar = flate2::read::GzDecoder::new(&krate);
49         let mut archiv = tar::Archive::new(tar);
50         let extracted_path = extract_dir.join(format!("{}-{}", self.name, self.version));
51       // println!("ar:  p: {:?}", &krate, extracted_path);
52         archiv.unpack(&extracted_path).expect("Failed to extract!");
53         // extract
54
55         Krate {
56             version: self.version.clone(),
57             name: self.name.clone(),
58             path: extracted_path,
59         }
60     }
61 }
62
63 impl Krate {
64     fn run_clippy_lints(&self) -> String {
65         todo!();
66     }
67 }
68
69 fn build_clippy() {
70     Command::new("cargo")
71         .arg("build")
72         .output()
73         .expect("Failed to build clippy!");
74 }
75
76 // the main fn
77 pub fn run() {
78     let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
79     let clippy_driver_path: PathBuf = PathBuf::from("target/debug/clippy-driver");
80
81     // crates we want to check:
82     let krates: Vec<KrateSource> = vec![KrateSource::new("cargo", "0.49.0"), KrateSource::new("regex", "1.4.2")];
83
84     println!("Compiling clippy...");
85     build_clippy();
86     println!("Done compiling");
87
88     // assert that clippy is found
89     assert!(
90         cargo_clippy_path.is_file(),
91         "target/debug/cargo-clippy binary not found! {}", cargo_clippy_path.display()
92     );
93     assert!(
94         clippy_driver_path.is_file(),
95         "target/debug/clippy-driver binary not found! {}", clippy_driver_path.display()
96     );
97
98     // download and extract the crates, then run clippy on them and collect clippys warnings
99     let _clippy_lint_results: Vec<String> = krates
100         .into_iter()
101         .map(|krate| krate.download_and_extract())
102         .map(|krate| krate.run_clippy_lints())
103         .collect::<Vec<String>>();
104 }