mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 13:38:43 +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,
|
||||
ManifestVisitor::new(manifest_dir_path),
|
||||
)
|
||||
.await?
|
||||
.load_manifest()
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -29,23 +29,11 @@ impl ManifestVisitor {
|
|||
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 {
|
||||
type Target = Manifest<Meta>;
|
||||
|
||||
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError> {
|
||||
for res in entries {
|
||||
let mut entry = res?;
|
||||
|
@ -78,4 +66,18 @@ impl TarEntriesVisitor for ManifestVisitor {
|
|||
|
||||
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 serde::Serialize;
|
||||
use tinytemplate::TinyTemplate;
|
||||
use tokio::task::block_in_place;
|
||||
use url::Url;
|
||||
|
||||
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>>(
|
||||
manifest_path: P,
|
||||
) -> 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)
|
||||
let manifest = Manifest::<Meta>::from_path_with_metadata(manifest_path)?;
|
||||
// Load and parse manifest (this checks file system for binary output names)
|
||||
let manifest = Manifest::<Meta>::from_path_with_metadata(manifest_path)?;
|
||||
|
||||
// Return metadata
|
||||
Ok(manifest)
|
||||
// Return metadata
|
||||
Ok(manifest)
|
||||
})
|
||||
}
|
||||
|
||||
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,
|
||||
fmt: TarBasedFmt,
|
||||
visitor: V,
|
||||
) -> Result<V, BinstallError> {
|
||||
) -> Result<V::Target, BinstallError> {
|
||||
let stream = create_request(url).await?;
|
||||
|
||||
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");
|
||||
|
||||
Ok(visitor)
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
/// Fetch install path from environment
|
||||
|
|
|
@ -99,20 +99,17 @@ where
|
|||
/// Visitor must iterate over all entries.
|
||||
/// Entires can be in arbitary order.
|
||||
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> {
|
||||
(*self).visit(entries)
|
||||
}
|
||||
fn visit<R: Read>(&mut self, entries: Entries<'_, R>) -> Result<(), BinstallError>;
|
||||
fn finish(self) -> Result<Self::Target, BinstallError>;
|
||||
}
|
||||
|
||||
pub async fn extract_tar_based_stream_and_visit<S, V, E>(
|
||||
stream: S,
|
||||
fmt: TarBasedFmt,
|
||||
mut visitor: V,
|
||||
) -> Result<V, BinstallError>
|
||||
) -> Result<V::Target, BinstallError>
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
||||
V: TarEntriesVisitor + Debug + Send + 'static,
|
||||
|
@ -124,6 +121,6 @@ where
|
|||
|
||||
let mut tar = create_tar_decoder(reader, fmt)?;
|
||||
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 simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
|
||||
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::{
|
||||
bins,
|
||||
|
@ -441,26 +445,28 @@ async fn install_from_package(
|
|||
uithread.confirm().await?;
|
||||
|
||||
info!("Installing binaries...");
|
||||
for file in &bin_files {
|
||||
file.install_bin()?;
|
||||
}
|
||||
|
||||
// Generate symlinks
|
||||
if !opts.no_symlinks {
|
||||
block_in_place(|| {
|
||||
for file in &bin_files {
|
||||
file.install_link()?;
|
||||
file.install_bin()?;
|
||||
}
|
||||
}
|
||||
|
||||
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}");
|
||||
});
|
||||
}
|
||||
// Generate symlinks
|
||||
if !opts.no_symlinks {
|
||||
for file in &bin_files {
|
||||
file.install_link()?;
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
|
|
Loading…
Add table
Reference in a new issue