Skip to content

Commit 934ccc2

Browse files
authoredSep 17, 2022
Support new cmdline option --roots (rust-lang#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 <[email protected]>
1 parent a611b82 commit 934ccc2

File tree

7 files changed

+67
-38
lines changed

7 files changed

+67
-38
lines changed
 

‎crates/bin/src/args.rs

+13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ pub struct Args {
144144
#[clap(help_heading = "Options", long)]
145145
pub install_path: Option<PathBuf>,
146146

147+
/// Install binaries with a custom cargo root.
148+
///
149+
/// By default, we use `$CARGO_INSTALL_ROOT` or `$CARGO_HOME` as the
150+
/// cargo root and global metadata files are updated with the
151+
/// package information.
152+
///
153+
/// Specifying another path here would install the binaries and update
154+
/// the metadata files inside the path you specified.
155+
///
156+
/// NOTE that `--install-path` takes precedence over this option.
157+
#[clap(help_heading = "Options", long)]
158+
pub roots: Option<PathBuf>,
159+
147160
/// Deprecated, here for back-compat only. Secure is now on by default.
148161
#[clap(hide(true), long)]
149162
pub secure: bool,

‎crates/bin/src/entry.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ pub async fn install_crates(mut args: Args, jobserver_client: LazyJobserverClien
4141
// Initialize UI thread
4242
let mut uithread = UIThread::new(!args.no_confirm);
4343

44-
let (install_path, metadata, temp_dir) = block_in_place(|| -> Result<_> {
44+
let (install_path, cargo_roots, metadata, temp_dir) = block_in_place(|| -> Result<_> {
45+
// Compute cargo_roots
46+
let cargo_roots =
47+
install_path::get_cargo_roots_path(args.roots.take()).ok_or_else(|| {
48+
error!("No viable cargo roots path found of specified, try `--roots`");
49+
miette!("No cargo roots path found or specified")
50+
})?;
51+
4552
// Compute install directory
4653
let (install_path, custom_install_path) =
47-
install_path::get_install_path(args.install_path.as_deref());
54+
install_path::get_install_path(args.install_path.as_deref(), Some(&cargo_roots));
4855
let install_path = install_path.ok_or_else(|| {
4956
error!("No viable install path found of specified, try `--install-path`");
5057
miette!("No install path found or specified")
@@ -54,8 +61,12 @@ pub async fn install_crates(mut args: Args, jobserver_client: LazyJobserverClien
5461

5562
// Load metadata
5663
let metadata = if !custom_install_path {
57-
debug!("Reading binstall/crates-v1.json");
58-
Some(Records::load()?)
64+
let metadata_dir = cargo_roots.join("binstall");
65+
fs::create_dir_all(&metadata_dir).map_err(BinstallError::Io)?;
66+
let manifest_path = metadata_dir.join("crates-v1.json");
67+
68+
debug!("Reading {}", manifest_path.display());
69+
Some(Records::load_from_path(&manifest_path)?)
5970
} else {
6071
None
6172
};
@@ -71,7 +82,7 @@ pub async fn install_crates(mut args: Args, jobserver_client: LazyJobserverClien
7182
.map_err(BinstallError::from)
7283
.wrap_err("Creating a temporary directory failed.")?;
7384

74-
Ok((install_path, metadata, temp_dir))
85+
Ok((install_path, cargo_roots, metadata, temp_dir))
7586
})?;
7687

7788
// Remove installed crates
@@ -206,12 +217,11 @@ pub async fn install_crates(mut args: Args, jobserver_client: LazyJobserverClien
206217

207218
block_in_place(|| {
208219
if let Some(mut records) = metadata {
209-
// If using standardised install path,
210-
// then create_dir_all(&install_path) would also
211-
// create .cargo.
220+
// The cargo manifest path is already created when loading
221+
// metadata.
212222

213223
debug!("Writing .crates.toml");
214-
CratesToml::append(metadata_vec.iter())?;
224+
CratesToml::append_to_path(cargo_roots.join(".crates.toml"), metadata_vec.iter())?;
215225

216226
debug!("Writing binstall/crates-v1.json");
217227
for metadata in metadata_vec {

‎crates/bin/src/install_path.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
use std::{
2+
env::var_os,
23
path::{Path, PathBuf},
34
sync::Arc,
45
};
56

6-
use binstalk::helpers::statics::cargo_home;
7+
use binstalk::home::cargo_home;
78
use log::debug;
89

10+
pub fn get_cargo_roots_path(cargo_roots: Option<PathBuf>) -> Option<PathBuf> {
11+
if let Some(p) = cargo_roots {
12+
return Some(p);
13+
}
14+
15+
// Environmental variables
16+
if let Some(p) = var_os("CARGO_INSTALL_ROOT") {
17+
let p = PathBuf::from(p);
18+
debug!("using CARGO_INSTALL_ROOT ({})", p.display());
19+
return Some(p);
20+
}
21+
22+
if let Ok(p) = cargo_home() {
23+
debug!("using ({}) as cargo home", p.display());
24+
Some(p)
25+
} else {
26+
None
27+
}
28+
}
29+
930
/// Fetch install path from environment
1031
/// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
1132
///
1233
/// Return (install_path, is_custom_install_path)
13-
pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Arc<Path>>, bool) {
34+
pub fn get_install_path<P: AsRef<Path>>(
35+
install_path: Option<P>,
36+
cargo_roots: Option<P>,
37+
) -> (Option<Arc<Path>>, bool) {
1438
// Command line override first first
1539
if let Some(p) = install_path {
1640
return (Some(Arc::from(p.as_ref())), true);
1741
}
1842

19-
// Environmental variables
20-
if let Ok(p) = std::env::var("CARGO_INSTALL_ROOT") {
21-
debug!("using CARGO_INSTALL_ROOT ({p})");
22-
let b = PathBuf::from(p);
23-
return (Some(Arc::from(b.join("bin"))), true);
24-
}
25-
26-
if let Ok(p) = cargo_home() {
27-
debug!("using ({}) as cargo home", p.display());
28-
return (Some(p.join("bin").into()), false);
43+
// Then cargo_roots
44+
if let Some(p) = cargo_roots {
45+
return (Some(Arc::from(p.as_ref().join("bin"))), false);
2946
}
3047

3148
// Local executable dir if no cargo is found

‎crates/binstalk/src/helpers/statics.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
use std::{
2-
io::Error,
3-
ops::Deref,
4-
path::{Path, PathBuf},
5-
};
6-
7-
use once_cell::sync::{Lazy, OnceCell};
1+
use once_cell::sync::Lazy;
82
use url::Url;
93

10-
pub fn cargo_home() -> Result<&'static Path, Error> {
11-
static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
12-
13-
CARGO_HOME
14-
.get_or_try_init(home::cargo_home)
15-
.map(Deref::deref)
16-
}
17-
184
pub fn cratesio_url() -> &'static Url {
195
static CRATESIO: Lazy<Url, fn() -> Url> =
206
Lazy::new(|| Url::parse("https://github.com/rust-lang/crates.io-index").unwrap());

‎crates/binstalk/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub mod manifests;
88
pub mod ops;
99

1010
pub use detect_targets::{get_desired_targets, DesiredTargets};
11+
pub use home;

‎crates/binstalk/src/manifests/binstall_crates_v1.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use std::{
1515
};
1616

1717
use fs_lock::FileLock;
18+
use home::cargo_home;
1819
use miette::Diagnostic;
1920
use serde::Serialize;
2021
use thiserror::Error;
2122

22-
use crate::{fs::create_if_not_exist, helpers::statics::cargo_home};
23+
use crate::fs::create_if_not_exist;
2324

2425
use super::crate_info::CrateInfo;
2526

‎crates/binstalk/src/manifests/cargo_crates_v1.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ use std::{
1717

1818
use compact_str::CompactString;
1919
use fs_lock::FileLock;
20+
use home::cargo_home;
2021
use miette::Diagnostic;
2122
use serde::{Deserialize, Serialize};
2223
use thiserror::Error;
2324

24-
use crate::{fs::create_if_not_exist, helpers::statics::cargo_home};
25+
use crate::fs::create_if_not_exist;
2526

2627
use super::crate_info::CrateInfo;
2728

0 commit comments

Comments
 (0)
Please sign in to comment.