Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduerr committed Jan 24, 2020
1 parent b791fcf commit 8786dec
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,48 +200,34 @@ fn install_one(
)?
};

let mut td_opt = None;
let mut needs_cleanup = false;
let overidden_target_dir = if source_id.is_git() || source_id.is_path() {
None
} else if let Some(dir) = config.target_dir()? {
Some(dir)
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
let p = td.path().to_owned();
td_opt = Some(td);
Some(Filesystem::new(p))
let (mut ws, git_package) = if source_id.is_git() {
// Don't use ws.current() in order to keep the package source as a git source so that
// install tracking uses the correct sourc.
(Workspace::new(pkg.manifest_path(), config)?, Some(&pkg))
} else if source_id.is_path() {
(Workspace::new(pkg.manifest_path(), config)?, None)
} else {
needs_cleanup = true;
Some(Filesystem::new(config.cwd().join("target-install")))
(Workspace::ephemeral(pkg, config, None, false)?, None)
};
ws.set_ignore_lock(config.lock_update_allowed());
ws.set_require_optional_deps(false);

let mut git_package = None;
let mut ws = match overidden_target_dir {
Some(dir) => Workspace::ephemeral(pkg, config, Some(dir), false)?,
None => {
let mut ws = Workspace::new(pkg.manifest_path(), config)?;
ws.set_require_optional_deps(false);

// Use tempdir to build git depedencies to prevent bloat in cargo cache
if source_id.is_git() {
if config.target_dir()?.is_none() {
match TempFileBuilder::new().prefix("cargo-install").tempdir() {
Ok(td) => {
let p = td.path().to_owned();
td_opt = Some(td);
ws.set_target_dir(Filesystem::new(p));
}
// If tempfile creation fails, write to cargo cache but clean up afterwards
Err(_) => needs_cleanup = true,
}
}
git_package = Some(&pkg);
}
let mut td_opt = None;
let mut needs_cleanup = false;
if !source_id.is_path() {
let target_dir = if let Some(dir) = config.target_dir()? {
dir
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
let p = td.path().to_owned();
td_opt = Some(td);
Filesystem::new(p)
} else {
needs_cleanup = true;
Filesystem::new(config.cwd().join("target-install"))
};
ws.set_target_dir(target_dir);
}

ws
}
};
ws.set_ignore_lock(config.lock_update_allowed());
let pkg = git_package.map_or_else(|| ws.current(), |pkg| Ok(pkg))?;

if from_cwd {
Expand Down

0 comments on commit 8786dec

Please sign in to comment.