mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 21:48:42 +00:00
Merge pull request #194 from NobodyXu/fix/blocking
This commit is contained in:
commit
401fa8772c
5 changed files with 56 additions and 49 deletions
|
@ -71,6 +71,5 @@ pub async fn fetch_crate_cratesio(
|
||||||
TarBasedFmt::Tgz,
|
TarBasedFmt::Tgz,
|
||||||
ManifestVisitor::new(manifest_dir_path),
|
ManifestVisitor::new(manifest_dir_path),
|
||||||
)
|
)
|
||||||
.await?
|
.await
|
||||||
.load_manifest()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,23 +29,11 @@ impl ManifestVisitor {
|
||||||
vfs: Vfs::new(),
|
vfs: Vfs::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load binstall metadata using the extracted information stored in memory.
|
|
||||||
pub(super) fn load_manifest(&self) -> Result<Manifest<Meta>, BinstallError> {
|
|
||||||
debug!("Loading manifest directly from extracted file");
|
|
||||||
|
|
||||||
// Load and parse manifest
|
|
||||||
let mut manifest = Manifest::<Meta>::from_slice_with_metadata(&self.cargo_toml_content)?;
|
|
||||||
|
|
||||||
// Checks vfs for binary output names
|
|
||||||
manifest.complete_from_abstract_filesystem(&self.vfs)?;
|
|
||||||
|
|
||||||
// Return metadata
|
|
||||||
Ok(manifest)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TarEntriesVisitor for ManifestVisitor {
|
impl TarEntriesVisitor for ManifestVisitor {
|
||||||
|
type Target = Manifest<Meta>;
|
||||||
|
|
||||||
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError> {
|
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError> {
|
||||||
for res in entries {
|
for res in entries {
|
||||||
let mut entry = res?;
|
let mut entry = res?;
|
||||||
|
@ -78,4 +66,18 @@ impl TarEntriesVisitor for ManifestVisitor {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load binstall metadata using the extracted information stored in memory.
|
||||||
|
fn finish(self) -> Result<Self::Target, BinstallError> {
|
||||||
|
debug!("Loading manifest directly from extracted file");
|
||||||
|
|
||||||
|
// Load and parse manifest
|
||||||
|
let mut manifest = Manifest::from_slice_with_metadata(&self.cargo_toml_content)?;
|
||||||
|
|
||||||
|
// Checks vfs for binary output names
|
||||||
|
manifest.complete_from_abstract_filesystem(&self.vfs)?;
|
||||||
|
|
||||||
|
// Return metadata
|
||||||
|
Ok(manifest)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ use once_cell::sync::OnceCell;
|
||||||
use reqwest::{Client, ClientBuilder, Method, Response};
|
use reqwest::{Client, ClientBuilder, Method, Response};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tinytemplate::TinyTemplate;
|
use tinytemplate::TinyTemplate;
|
||||||
|
use tokio::task::block_in_place;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{BinstallError, Meta, PkgFmt, PkgFmtDecomposed, TarBasedFmt};
|
use crate::{BinstallError, Meta, PkgFmt, PkgFmtDecomposed, TarBasedFmt};
|
||||||
|
@ -38,13 +39,15 @@ pub static REQWESTGLOBALCONFIG: OnceCell<(bool, Option<TLSVersion>)> = OnceCell:
|
||||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||||
manifest_path: P,
|
manifest_path: P,
|
||||||
) -> Result<Manifest<Meta>, BinstallError> {
|
) -> Result<Manifest<Meta>, BinstallError> {
|
||||||
debug!("Reading manifest: {}", manifest_path.as_ref().display());
|
block_in_place(|| {
|
||||||
|
debug!("Reading manifest: {}", manifest_path.as_ref().display());
|
||||||
|
|
||||||
// Load and parse manifest (this checks file system for binary output names)
|
// Load and parse manifest (this checks file system for binary output names)
|
||||||
let manifest = Manifest::<Meta>::from_path_with_metadata(manifest_path)?;
|
let manifest = Manifest::<Meta>::from_path_with_metadata(manifest_path)?;
|
||||||
|
|
||||||
// Return metadata
|
// Return metadata
|
||||||
Ok(manifest)
|
Ok(manifest)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_reqwest_client_builder() -> ClientBuilder {
|
pub fn new_reqwest_client_builder() -> ClientBuilder {
|
||||||
|
@ -127,16 +130,16 @@ pub async fn download_tar_based_and_visit<V: TarEntriesVisitor + Debug + Send +
|
||||||
url: Url,
|
url: Url,
|
||||||
fmt: TarBasedFmt,
|
fmt: TarBasedFmt,
|
||||||
visitor: V,
|
visitor: V,
|
||||||
) -> Result<V, BinstallError> {
|
) -> Result<V::Target, BinstallError> {
|
||||||
let stream = create_request(url).await?;
|
let stream = create_request(url).await?;
|
||||||
|
|
||||||
debug!("Downloading and extracting then in-memory processing");
|
debug!("Downloading and extracting then in-memory processing");
|
||||||
|
|
||||||
let visitor = extract_tar_based_stream_and_visit(stream, fmt, visitor).await?;
|
let ret = extract_tar_based_stream_and_visit(stream, fmt, visitor).await?;
|
||||||
|
|
||||||
debug!("Download, extraction and in-memory procession OK");
|
debug!("Download, extraction and in-memory procession OK");
|
||||||
|
|
||||||
Ok(visitor)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch install path from environment
|
/// Fetch install path from environment
|
||||||
|
|
|
@ -99,20 +99,17 @@ where
|
||||||
/// Visitor must iterate over all entries.
|
/// Visitor must iterate over all entries.
|
||||||
/// Entires can be in arbitary order.
|
/// Entires can be in arbitary order.
|
||||||
pub trait TarEntriesVisitor {
|
pub trait TarEntriesVisitor {
|
||||||
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError>;
|
type Target;
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: TarEntriesVisitor> TarEntriesVisitor for &mut V {
|
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError>;
|
||||||
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError> {
|
fn finish(self) -> Result<Self::Target, BinstallError>;
|
||||||
(*self).visit(entries)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn extract_tar_based_stream_and_visit<S, V, E>(
|
pub async fn extract_tar_based_stream_and_visit<S, V, E>(
|
||||||
stream: S,
|
stream: S,
|
||||||
fmt: TarBasedFmt,
|
fmt: TarBasedFmt,
|
||||||
mut visitor: V,
|
mut visitor: V,
|
||||||
) -> Result<V, BinstallError>
|
) -> Result<V::Target, BinstallError>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
||||||
V: TarEntriesVisitor + Debug + Send + 'static,
|
V: TarEntriesVisitor + Debug + Send + 'static,
|
||||||
|
@ -124,6 +121,6 @@ where
|
||||||
|
|
||||||
let mut tar = create_tar_decoder(reader, fmt)?;
|
let mut tar = create_tar_decoder(reader, fmt)?;
|
||||||
visitor.visit(tar.entries()?)?;
|
visitor.visit(tar.entries()?)?;
|
||||||
Ok(visitor)
|
visitor.finish()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
40
src/main.rs
40
src/main.rs
|
@ -12,7 +12,11 @@ use log::{debug, error, info, warn, LevelFilter};
|
||||||
use miette::{miette, IntoDiagnostic, Result, WrapErr};
|
use miette::{miette, IntoDiagnostic, Result, WrapErr};
|
||||||
use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
|
use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
use tokio::{process::Command, runtime::Runtime, task::JoinError};
|
use tokio::{
|
||||||
|
process::Command,
|
||||||
|
runtime::Runtime,
|
||||||
|
task::{block_in_place, JoinError},
|
||||||
|
};
|
||||||
|
|
||||||
use cargo_binstall::{
|
use cargo_binstall::{
|
||||||
bins,
|
bins,
|
||||||
|
@ -441,26 +445,28 @@ async fn install_from_package(
|
||||||
uithread.confirm().await?;
|
uithread.confirm().await?;
|
||||||
|
|
||||||
info!("Installing binaries...");
|
info!("Installing binaries...");
|
||||||
for file in &bin_files {
|
block_in_place(|| {
|
||||||
file.install_bin()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate symlinks
|
|
||||||
if !opts.no_symlinks {
|
|
||||||
for file in &bin_files {
|
for file in &bin_files {
|
||||||
file.install_link()?;
|
file.install_bin()?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if opts.no_cleanup {
|
// Generate symlinks
|
||||||
let _ = temp_dir.into_path();
|
if !opts.no_symlinks {
|
||||||
} else {
|
for file in &bin_files {
|
||||||
temp_dir.close().unwrap_or_else(|err| {
|
file.install_link()?;
|
||||||
warn!("Failed to clean up some resources: {err}");
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
if opts.no_cleanup {
|
||||||
|
let _ = temp_dir.into_path();
|
||||||
|
} else {
|
||||||
|
temp_dir.close().unwrap_or_else(|err| {
|
||||||
|
warn!("Failed to clean up some resources: {err}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn install_from_source(
|
async fn install_from_source(
|
||||||
|
|
Loading…
Add table
Reference in a new issue