- let extract_dir = PathBuf::from("target/lintcheck/crates");
- let krate_download_dir = PathBuf::from("target/lintcheck/downloads");
-
- // url to download the crate from crates.io
- let url = format!(
- "https://crates.io/api/v1/crates/{}/{}/download",
- self.name, self.version
- );
- println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
- let _ = std::fs::create_dir("target/lintcheck/");
- let _ = std::fs::create_dir(&krate_download_dir);
- let _ = std::fs::create_dir(&extract_dir);
-
- let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
- // don't download/extract if we already have done so
- if !krate_file_path.is_file() {
- // create a file path to download and write the crate data into
- let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
- let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
- // copy the crate into the file
- std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
-
- // unzip the tarball
- let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
- // extract the tar archive
- let mut archive = tar::Archive::new(ungz_tar);
- archive.unpack(&extract_dir).expect("Failed to extract!");
- }
- // crate is extracted, return a new Krate object which contains the path to the extracted
- // sources that clippy can check
- Crate {
- version: self.version.clone(),
- name: self.name.clone(),
- path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
+ match self {
+ CrateSource::CratesIo { name, version, options } => {
+ let extract_dir = PathBuf::from("target/lintcheck/crates");
+ let krate_download_dir = PathBuf::from("target/lintcheck/downloads");
+
+ // url to download the crate from crates.io
+ let url = format!("https://crates.io/api/v1/crates/{}/{}/download", name, version);
+ println!("Downloading and extracting {} {} from {}", name, version, url);
+ let _ = std::fs::create_dir("target/lintcheck/");
+ let _ = std::fs::create_dir(&krate_download_dir);
+ let _ = std::fs::create_dir(&extract_dir);
+
+ let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", name, version));
+ // don't download/extract if we already have done so
+ if !krate_file_path.is_file() {
+ // create a file path to download and write the crate data into
+ let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
+ let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
+ // copy the crate into the file
+ std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
+
+ // unzip the tarball
+ let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
+ // extract the tar archive
+ let mut archive = tar::Archive::new(ungz_tar);
+ archive.unpack(&extract_dir).expect("Failed to extract!");
+ }
+ // crate is extracted, return a new Krate object which contains the path to the extracted
+ // sources that clippy can check
+ Crate {
+ version: version.clone(),
+ name: name.clone(),
+ path: extract_dir.join(format!("{}-{}/", name, version)),
+ options: options.clone(),
+ }
+ },
+ CrateSource::Git {
+ name,
+ url,
+ commit,
+ options,
+ } => {
+ let repo_path = {
+ let mut repo_path = PathBuf::from("target/lintcheck/crates");
+ // add a -git suffix in case we have the same crate from crates.io and a git repo
+ repo_path.push(format!("{}-git", name));
+ repo_path
+ };
+ // clone the repo if we have not done so
+ if !repo_path.is_dir() {
+ println!("Cloning {} and checking out {}", url, commit);
+ if !Command::new("git")
+ .arg("clone")
+ .arg(url)
+ .arg(&repo_path)
+ .status()
+ .expect("Failed to clone git repo!")
+ .success()
+ {
+ eprintln!("Failed to clone {} into {}", url, repo_path.display())
+ }
+ }
+ // check out the commit/branch/whatever
+ if !Command::new("git")
+ .arg("checkout")
+ .arg(commit)
+ .current_dir(&repo_path)
+ .status()
+ .expect("Failed to check out commit")
+ .success()
+ {
+ eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
+ }
+
+ Crate {
+ version: commit.clone(),
+ name: name.clone(),
+ path: repo_path,
+ options: options.clone(),
+ }
+ },
+ CrateSource::Path { name, path, options } => {
+ use fs_extra::dir;
+
+ // simply copy the entire directory into our target dir
+ let copy_dest = PathBuf::from("target/lintcheck/crates/");
+
+ // the source path of the crate we copied, ${copy_dest}/crate_name
+ let crate_root = copy_dest.join(name); // .../crates/local_crate
+
+ if !crate_root.exists() {
+ println!("Copying {} to {}", path.display(), copy_dest.display());
+
+ dir::copy(path, ©_dest, &dir::CopyOptions::new()).expect(&format!(
+ "Failed to copy from {}, to {}",
+ path.display(),
+ crate_root.display()
+ ));
+ } else {
+ println!(
+ "Not copying {} to {}, destination already exists",
+ path.display(),
+ crate_root.display()
+ );
+ }
+
+ Crate {
+ version: String::from("local"),
+ name: name.clone(),
+ path: crate_root,
+ options: options.clone(),
+ }
+ },