mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Refactor: Use binstall_v1::MetaData
in mod binstall
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
05c0d5fcae
commit
2f27a5fd93
6 changed files with 51 additions and 82 deletions
|
@ -1,8 +1,6 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use compact_str::CompactString;
|
||||
|
||||
use crate::{metafiles, DesiredTargets, PkgOverride};
|
||||
use crate::{metafiles::binstall_v1::MetaData, DesiredTargets, PkgOverride};
|
||||
|
||||
mod resolve;
|
||||
pub use resolve::*;
|
||||
|
@ -18,11 +16,3 @@ pub struct Options {
|
|||
pub cli_overrides: PkgOverride,
|
||||
pub desired_targets: DesiredTargets,
|
||||
}
|
||||
|
||||
/// MetaData required to update MetaFiles.
|
||||
pub struct MetaData {
|
||||
pub bins: Vec<CompactString>,
|
||||
pub cvs: metafiles::CrateVersionSource,
|
||||
pub version_req: String,
|
||||
pub target: String,
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::{path::PathBuf, process, sync::Arc};
|
||||
|
||||
use cargo_toml::Package;
|
||||
use compact_str::CompactString;
|
||||
use log::{debug, error, info};
|
||||
use miette::{miette, IntoDiagnostic, Result, WrapErr};
|
||||
use tokio::{process::Command, task::block_in_place};
|
||||
|
||||
use super::{MetaData, Options, Resolution};
|
||||
use crate::{bins, fetchers::Fetcher, *};
|
||||
use crate::{bins, fetchers::Fetcher, metafiles::binstall_v1::Source, *};
|
||||
|
||||
pub async fn install(
|
||||
resolution: Resolution,
|
||||
|
@ -22,13 +23,21 @@ pub async fn install(
|
|||
bin_path,
|
||||
bin_files,
|
||||
} => {
|
||||
let cvs = metafiles::CrateVersionSource {
|
||||
name,
|
||||
version: package.version.parse().into_diagnostic()?,
|
||||
source: metafiles::Source::cratesio_registry(),
|
||||
};
|
||||
let current_version = package.version.parse().into_diagnostic()?;
|
||||
let target = fetcher.target().into();
|
||||
|
||||
install_from_package(fetcher, opts, cvs, version, bin_path, bin_files).await
|
||||
install_from_package(fetcher, opts, bin_path, bin_files)
|
||||
.await
|
||||
.map(|option| {
|
||||
option.map(|bins| MetaData {
|
||||
name: name.into(),
|
||||
version_req: version.into(),
|
||||
current_version,
|
||||
source: Source::cratesio_registry(),
|
||||
target,
|
||||
bins,
|
||||
})
|
||||
})
|
||||
}
|
||||
Resolution::InstallFromSource { package } => {
|
||||
let desired_targets = opts.desired_targets.get().await;
|
||||
|
@ -54,11 +63,9 @@ pub async fn install(
|
|||
async fn install_from_package(
|
||||
fetcher: Arc<dyn Fetcher>,
|
||||
opts: Arc<Options>,
|
||||
cvs: metafiles::CrateVersionSource,
|
||||
version: String,
|
||||
bin_path: PathBuf,
|
||||
bin_files: Vec<bins::BinFile>,
|
||||
) -> Result<Option<MetaData>> {
|
||||
) -> Result<Option<Vec<CompactString>>> {
|
||||
// Download package
|
||||
if opts.dry_run {
|
||||
info!("Dry run, not downloading package");
|
||||
|
@ -108,12 +115,9 @@ async fn install_from_package(
|
|||
}
|
||||
}
|
||||
|
||||
Ok(Some(MetaData {
|
||||
bins: bin_files.into_iter().map(|bin| bin.base_name).collect(),
|
||||
cvs,
|
||||
version_req: version,
|
||||
target: fetcher.target().to_string(),
|
||||
}))
|
||||
Ok(Some(
|
||||
bin_files.into_iter().map(|bin| bin.base_name).collect(),
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -344,11 +344,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
|||
block_in_place(|| {
|
||||
if !custom_install_path {
|
||||
debug!("Writing .crates.toml");
|
||||
metafiles::v1::CratesToml::append(
|
||||
metadata_vec
|
||||
.iter()
|
||||
.map(|metadata| (&metadata.cvs, metadata.bins.clone())),
|
||||
)?;
|
||||
metafiles::v1::CratesToml::append(metadata_vec.iter())?;
|
||||
}
|
||||
|
||||
if opts.no_cleanup {
|
||||
|
|
|
@ -23,30 +23,6 @@ pub struct MetaData {
|
|||
pub target: CompactString,
|
||||
pub bins: Vec<CompactString>,
|
||||
}
|
||||
impl MetaData {
|
||||
pub fn new(metadata: crate::binstall::MetaData) -> Self {
|
||||
let crate::binstall::MetaData {
|
||||
bins,
|
||||
cvs:
|
||||
super::CrateVersionSource {
|
||||
name,
|
||||
version,
|
||||
source,
|
||||
},
|
||||
version_req,
|
||||
target,
|
||||
} = metadata;
|
||||
|
||||
Self {
|
||||
name: name.into(),
|
||||
version_req: version_req.into(),
|
||||
current_version: version,
|
||||
source: source.into(),
|
||||
target: target.into(),
|
||||
bins,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||
pub enum SourceType {
|
||||
|
@ -70,27 +46,6 @@ impl Source {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<super::Source> for Source {
|
||||
fn from(src: super::Source) -> Self {
|
||||
use super::Source::*;
|
||||
|
||||
match src {
|
||||
Git(url) => Source {
|
||||
source_type: SourceType::Git,
|
||||
url,
|
||||
},
|
||||
Path(url) => Source {
|
||||
source_type: SourceType::Path,
|
||||
url,
|
||||
},
|
||||
Registry(url) => Source {
|
||||
source_type: SourceType::Registry,
|
||||
url,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
|
|
|
@ -15,6 +15,16 @@ pub struct CrateVersionSource {
|
|||
pub source: Source,
|
||||
}
|
||||
|
||||
impl From<&super::binstall_v1::MetaData> for CrateVersionSource {
|
||||
fn from(metadata: &super::binstall_v1::MetaData) -> Self {
|
||||
super::CrateVersionSource {
|
||||
name: metadata.name.clone().to_string(),
|
||||
version: metadata.current_version.clone(),
|
||||
source: Source::from(&metadata.source),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub enum Source {
|
||||
Git(Url),
|
||||
|
@ -28,6 +38,20 @@ impl Source {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<&super::binstall_v1::Source> for Source {
|
||||
fn from(source: &super::binstall_v1::Source) -> Self {
|
||||
use super::binstall_v1::SourceType::*;
|
||||
|
||||
let url = source.url.clone();
|
||||
|
||||
match source.source_type {
|
||||
Git => Self::Git(url),
|
||||
Path => Self::Path(url),
|
||||
Registry => Self::Registry(url),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for CrateVersionSource {
|
||||
type Err = CvsParseError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
|
|
|
@ -11,7 +11,7 @@ use miette::Diagnostic;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use super::CrateVersionSource;
|
||||
use super::{binstall_v1::MetaData, CrateVersionSource};
|
||||
use crate::{cargo_home, create_if_not_exist, FileLock};
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
|
@ -71,13 +71,13 @@ impl CratesToml {
|
|||
iter: Iter,
|
||||
) -> Result<(), CratesTomlParseError>
|
||||
where
|
||||
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<CompactString>)>,
|
||||
Iter: IntoIterator<Item = &'a MetaData>,
|
||||
{
|
||||
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
||||
let mut c1 = Self::load_from_reader(&mut *file)?;
|
||||
|
||||
for (cvs, bins) in iter {
|
||||
c1.insert(cvs, bins);
|
||||
for metadata in iter {
|
||||
c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone());
|
||||
}
|
||||
|
||||
file.rewind()?;
|
||||
|
@ -88,7 +88,7 @@ impl CratesToml {
|
|||
|
||||
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
|
||||
where
|
||||
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<CompactString>)>,
|
||||
Iter: IntoIterator<Item = &'a MetaData>,
|
||||
{
|
||||
Self::append_to_path(Self::default_path()?, iter)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue