diff options
Diffstat (limited to 'tools/external_crates/crate_health/src/pseudo_crate.rs')
-rw-r--r-- | tools/external_crates/crate_health/src/pseudo_crate.rs | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/tools/external_crates/crate_health/src/pseudo_crate.rs b/tools/external_crates/crate_health/src/pseudo_crate.rs index 55472c0be..bcde2f38e 100644 --- a/tools/external_crates/crate_health/src/pseudo_crate.rs +++ b/tools/external_crates/crate_health/src/pseudo_crate.rs @@ -14,7 +14,6 @@ use std::{ fs::{create_dir, write}, - path::{Path, PathBuf}, process::Command, str::from_utf8, }; @@ -23,7 +22,7 @@ use anyhow::{anyhow, Context, Result}; use serde::Serialize; use tinytemplate::TinyTemplate; -use crate::{ensure_exists_and_empty, NamedAndVersioned}; +use crate::{ensure_exists_and_empty, NamedAndVersioned, RepoPath}; static CARGO_TOML_TEMPLATE: &'static str = include_str!("templates/Cargo.toml.template"); @@ -39,25 +38,21 @@ struct CargoToml { } pub struct PseudoCrate { - // Absolute path to pseudo-crate. - path: PathBuf, + path: RepoPath, } impl PseudoCrate { - pub fn new<P: Into<PathBuf>>(path: P) -> PseudoCrate { - PseudoCrate { path: path.into() } + pub fn new(path: RepoPath) -> PseudoCrate { + PseudoCrate { path } } pub fn init<'a>( &self, crates: impl Iterator<Item = &'a (impl NamedAndVersioned + 'a)>, ) -> Result<()> { - if self.path.exists() { - return Err(anyhow!( - "Can't init pseudo-crate because {} already exists", - self.path.display() - )); + if self.path.abs().exists() { + return Err(anyhow!("Can't init pseudo-crate because {} already exists", self.path)); } - ensure_exists_and_empty(&self.path)?; + ensure_exists_and_empty(&self.path.abs())?; let mut deps = Vec::new(); for krate in crates { @@ -80,23 +75,24 @@ impl PseudoCrate { let mut tt = TinyTemplate::new(); tt.add_template("cargo_toml", CARGO_TOML_TEMPLATE)?; - let cargo_toml = self.path.join("Cargo.toml"); + let cargo_toml = self.path.join(&"Cargo.toml").abs(); write(&cargo_toml, tt.render("cargo_toml", &CargoToml { deps })?)?; - create_dir(self.path.join("src")).context("Failed to create src dir")?; - write(self.path.join("src/lib.rs"), "// Nothing").context("Failed to create src/lib.rs")?; + create_dir(self.path.join(&"src").abs()).context("Failed to create src dir")?; + write(self.path.join(&"src/lib.rs").abs(), "// Nothing") + .context("Failed to create src/lib.rs")?; self.vendor() // TODO: Run "cargo deny" } - pub fn get_path(&self) -> &Path { - self.path.as_path() + pub fn get_path(&self) -> &RepoPath { + &self.path } pub fn add(&self, krate: &impl NamedAndVersioned) -> Result<()> { let status = Command::new("cargo") .args(["add", format!("{}@={}", krate.name(), krate.version()).as_str()]) - .current_dir(&self.path) + .current_dir(self.path.abs()) .spawn() .context("Failed to spawn 'cargo add'")? .wait() @@ -107,7 +103,8 @@ impl PseudoCrate { Ok(()) } pub fn vendor(&self) -> Result<()> { - let output = Command::new("cargo").args(["vendor"]).current_dir(&self.path).output()?; + let output = + Command::new("cargo").args(["vendor"]).current_dir(self.path.abs()).output()?; if !output.status.success() { return Err(anyhow!( "cargo vendor failed with exit code {}\nstdout:\n{}\nstderr:\n{}", |