2020-12-18 12:21:13 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
// represents an archive we download from crates.io
|
2020-12-18 13:14:15 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-18 12:21:13 +00:00
|
|
|
struct KrateSource {
|
|
|
|
version: String,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
// represents the extracted sourcecode of a crate
|
2020-12-18 13:14:15 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-18 12:21:13 +00:00
|
|
|
struct Krate {
|
|
|
|
version: String,
|
|
|
|
name: String,
|
2020-12-18 13:14:15 +00:00
|
|
|
path: PathBuf,
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KrateSource {
|
2020-12-18 15:17:53 +00:00
|
|
|
fn new(name: &str, version: &str) -> Self {
|
2020-12-18 12:21:13 +00:00
|
|
|
KrateSource {
|
|
|
|
version: version.into(),
|
|
|
|
name: name.into(),
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 13:14:15 +00:00
|
|
|
fn download_and_extract(&self) -> Krate {
|
|
|
|
let extract_dir = PathBuf::from("target/crater/crates");
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// download
|
2020-12-18 13:14:15 +00:00
|
|
|
let krate_download_dir = PathBuf::from("target/crater/downloads");
|
|
|
|
|
|
|
|
let url = format!(
|
|
|
|
"https://crates.io/api/v1/crates/{}/{}/download",
|
|
|
|
self.name, self.version
|
|
|
|
);
|
2020-12-18 15:17:53 +00:00
|
|
|
println!("Downloading {}, {} / {}", self.name, self.version, url);
|
|
|
|
std::fs::create_dir("target/crater/").unwrap();
|
2020-12-18 13:14:15 +00:00
|
|
|
|
2020-12-18 15:17:53 +00:00
|
|
|
std::fs::create_dir(&krate_download_dir).unwrap();
|
|
|
|
std::fs::create_dir(&extract_dir).unwrap();
|
|
|
|
|
|
|
|
let krate_name = format!("{}-{}.crate.tar.gz", &self.name, &self.version);
|
2020-12-18 13:14:15 +00:00
|
|
|
let mut krate_dest = std::fs::File::create(krate_download_dir.join(krate_name)).unwrap();
|
|
|
|
let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
|
|
|
|
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
|
|
|
|
let krate = krate_dest;
|
2020-12-18 15:17:53 +00:00
|
|
|
dbg!(&krate);
|
|
|
|
let tar = flate2::read::GzDecoder::new(&krate);
|
2020-12-18 13:14:15 +00:00
|
|
|
let mut archiv = tar::Archive::new(tar);
|
2020-12-18 15:17:53 +00:00
|
|
|
let extracted_path = extract_dir.join(format!("{}-{}", self.name, self.version));
|
|
|
|
// println!("ar: p: {:?}", &krate, extracted_path);
|
2020-12-18 13:14:15 +00:00
|
|
|
archiv.unpack(&extracted_path).expect("Failed to extract!");
|
2020-12-18 15:17:53 +00:00
|
|
|
// extract
|
2020-12-18 12:21:13 +00:00
|
|
|
|
|
|
|
Krate {
|
2020-12-18 13:14:15 +00:00
|
|
|
version: self.version.clone(),
|
|
|
|
name: self.name.clone(),
|
|
|
|
path: extracted_path,
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Krate {
|
|
|
|
fn run_clippy_lints(&self) -> String {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_clippy() {
|
|
|
|
Command::new("cargo")
|
|
|
|
.arg("build")
|
|
|
|
.output()
|
|
|
|
.expect("Failed to build clippy!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// the main fn
|
2020-12-18 13:28:59 +00:00
|
|
|
pub fn run() {
|
2020-12-18 12:21:13 +00:00
|
|
|
let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
|
2020-12-18 15:17:53 +00:00
|
|
|
let clippy_driver_path: PathBuf = PathBuf::from("target/debug/clippy-driver");
|
2020-12-18 12:21:13 +00:00
|
|
|
|
|
|
|
// crates we want to check:
|
|
|
|
let krates: Vec<KrateSource> = vec![KrateSource::new("cargo", "0.49.0"), KrateSource::new("regex", "1.4.2")];
|
|
|
|
|
2020-12-18 15:17:53 +00:00
|
|
|
println!("Compiling clippy...");
|
2020-12-18 12:21:13 +00:00
|
|
|
build_clippy();
|
2020-12-18 15:17:53 +00:00
|
|
|
println!("Done compiling");
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// assert that clippy is found
|
|
|
|
assert!(
|
|
|
|
cargo_clippy_path.is_file(),
|
2020-12-18 15:17:53 +00:00
|
|
|
"target/debug/cargo-clippy binary not found! {}", cargo_clippy_path.display()
|
2020-12-18 12:21:13 +00:00
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
clippy_driver_path.is_file(),
|
2020-12-18 15:17:53 +00:00
|
|
|
"target/debug/clippy-driver binary not found! {}", clippy_driver_path.display()
|
2020-12-18 12:21:13 +00:00
|
|
|
);
|
|
|
|
|
2020-12-18 13:14:15 +00:00
|
|
|
// download and extract the crates, then run clippy on them and collect clippys warnings
|
2020-12-18 13:28:59 +00:00
|
|
|
let _clippy_lint_results: Vec<String> = krates
|
2020-12-18 13:14:15 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
|
|
|
.map(|krate| krate.run_clippy_lints())
|
|
|
|
.collect::<Vec<String>>();
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|