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);
|
2020-12-18 15:53:18 +00:00
|
|
|
let _ = std::fs::create_dir("target/crater/");
|
2020-12-18 13:14:15 +00:00
|
|
|
|
2020-12-18 15:53:18 +00:00
|
|
|
let _ = std::fs::create_dir(&krate_download_dir);
|
|
|
|
let _ = std::fs::create_dir(&extract_dir);
|
2020-12-18 15:17:53 +00:00
|
|
|
|
|
|
|
let krate_name = format!("{}-{}.crate.tar.gz", &self.name, &self.version);
|
2020-12-18 15:53:18 +00:00
|
|
|
let krate_file_path = krate_download_dir.join(krate_name);
|
|
|
|
let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
|
2020-12-18 13:14:15 +00:00
|
|
|
let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
|
|
|
|
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
|
2020-12-18 15:53:18 +00:00
|
|
|
// unzip the tarball
|
|
|
|
let dl = std::fs::File::open(krate_file_path).unwrap();
|
|
|
|
|
|
|
|
let ungz_tar = flate2::read::GzDecoder::new(dl);
|
|
|
|
// extract the tar archive
|
|
|
|
let mut archiv = tar::Archive::new(ungz_tar);
|
2020-12-18 16:25:07 +00:00
|
|
|
let extract_path = extract_dir.clone();
|
2020-12-18 15:53:18 +00:00
|
|
|
archiv.unpack(&extract_path).expect("Failed to extract!");
|
|
|
|
// extracted
|
2020-12-18 16:25:07 +00:00
|
|
|
dbg!(&extract_path);
|
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(),
|
2020-12-18 17:01:45 +00:00
|
|
|
path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Krate {
|
2020-12-18 17:34:09 +00:00
|
|
|
fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<String> {
|
2020-12-18 16:25:07 +00:00
|
|
|
let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
|
|
|
|
let project_root = &self.path;
|
|
|
|
dbg!(&cargo_clippy_path);
|
|
|
|
dbg!(&project_root);
|
|
|
|
|
2020-12-18 17:01:45 +00:00
|
|
|
let output = std::process::Command::new(cargo_clippy_path)
|
2020-12-18 17:34:09 +00:00
|
|
|
.args(&["--", "--message-format=short", "--", "--cap-lints=warn"])
|
2020-12-18 17:01:45 +00:00
|
|
|
.current_dir(project_root)
|
2020-12-18 17:34:09 +00:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let mut output = String::from_utf8_lossy(&output.stderr);
|
|
|
|
let output: Vec<&str> = output.lines().collect();
|
|
|
|
let mut output: Vec<String> = output
|
|
|
|
.into_iter()
|
|
|
|
.filter(|line| line.contains(": warning: "))
|
|
|
|
.map(|l| l.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
output.sort();
|
2020-12-18 16:25:07 +00:00
|
|
|
output
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-12-18 17:01:45 +00:00
|
|
|
let krates: Vec<KrateSource> = vec![KrateSource::new("regex", "1.4.2"), KrateSource::new("cargo", "0.49.0")];
|
2020-12-18 12:21:13 +00:00
|
|
|
|
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:53:18 +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:53:18 +00:00
|
|
|
"target/debug/clippy-driver binary not found! {}",
|
|
|
|
clippy_driver_path.display()
|
2020-12-18 17:01:45 +00:00
|
|
|
);
|
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 17:34:09 +00:00
|
|
|
let clippy_lint_results: Vec<Vec<String>> = krates
|
2020-12-18 13:14:15 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
2020-12-18 16:25:07 +00:00
|
|
|
.map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
|
2020-12-18 17:34:09 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let results: Vec<String> = clippy_lint_results.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
results.iter().for_each(|l| println!("{}", l));
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|