Support new cmdline option --roots (#389)

* Add new field `Args::roots`
* Use `env::var_os` to fetch `CARGO_INSTALL_ROOTS`
   Previously, it uses `env::var`, which might reject valid path just
   because it is not utf-8 string.
* Update manifest if `CARGO_INSTALL_ROOT` is specified
* Add new fn `install_path::get_cargo_roots_path`
* Fix updating manifest: Use `cargo_roots` instead of default path
* Rm `helpers::statics::cargo_home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-09-17 18:34:36 +10:00 committed by GitHub
parent a611b824fd
commit 934ccc257b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 38 deletions

View file

@ -1,31 +1,48 @@
use std::{
env::var_os,
path::{Path, PathBuf},
sync::Arc,
};
use binstalk::helpers::statics::cargo_home;
use binstalk::home::cargo_home;
use log::debug;
pub fn get_cargo_roots_path(cargo_roots: Option<PathBuf>) -> Option<PathBuf> {
if let Some(p) = cargo_roots {
return Some(p);
}
// Environmental variables
if let Some(p) = var_os("CARGO_INSTALL_ROOT") {
let p = PathBuf::from(p);
debug!("using CARGO_INSTALL_ROOT ({})", p.display());
return Some(p);
}
if let Ok(p) = cargo_home() {
debug!("using ({}) as cargo home", p.display());
Some(p)
} else {
None
}
}
/// Fetch install path from environment
/// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
///
/// Return (install_path, is_custom_install_path)
pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Arc<Path>>, bool) {
pub fn get_install_path<P: AsRef<Path>>(
install_path: Option<P>,
cargo_roots: Option<P>,
) -> (Option<Arc<Path>>, bool) {
// Command line override first first
if let Some(p) = install_path {
return (Some(Arc::from(p.as_ref())), true);
}
// Environmental variables
if let Ok(p) = std::env::var("CARGO_INSTALL_ROOT") {
debug!("using CARGO_INSTALL_ROOT ({p})");
let b = PathBuf::from(p);
return (Some(Arc::from(b.join("bin"))), true);
}
if let Ok(p) = cargo_home() {
debug!("using ({}) as cargo home", p.display());
return (Some(p.join("bin").into()), false);
// Then cargo_roots
if let Some(p) = cargo_roots {
return (Some(Arc::from(p.as_ref().join("bin"))), false);
}
// Local executable dir if no cargo is found