Refactor: Use binstall_v1::MetaData in mod binstall

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-27 19:44:16 +10:00
parent 05c0d5fcae
commit 2f27a5fd93
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
6 changed files with 51 additions and 82 deletions

View file

@ -1,8 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use compact_str::CompactString; use crate::{metafiles::binstall_v1::MetaData, DesiredTargets, PkgOverride};
use crate::{metafiles, DesiredTargets, PkgOverride};
mod resolve; mod resolve;
pub use resolve::*; pub use resolve::*;
@ -18,11 +16,3 @@ pub struct Options {
pub cli_overrides: PkgOverride, pub cli_overrides: PkgOverride,
pub desired_targets: DesiredTargets, 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,
}

View file

@ -1,12 +1,13 @@
use std::{path::PathBuf, process, sync::Arc}; use std::{path::PathBuf, process, sync::Arc};
use cargo_toml::Package; use cargo_toml::Package;
use compact_str::CompactString;
use log::{debug, error, info}; use log::{debug, error, info};
use miette::{miette, IntoDiagnostic, Result, WrapErr}; use miette::{miette, IntoDiagnostic, Result, WrapErr};
use tokio::{process::Command, task::block_in_place}; use tokio::{process::Command, task::block_in_place};
use super::{MetaData, Options, Resolution}; use super::{MetaData, Options, Resolution};
use crate::{bins, fetchers::Fetcher, *}; use crate::{bins, fetchers::Fetcher, metafiles::binstall_v1::Source, *};
pub async fn install( pub async fn install(
resolution: Resolution, resolution: Resolution,
@ -22,13 +23,21 @@ pub async fn install(
bin_path, bin_path,
bin_files, bin_files,
} => { } => {
let cvs = metafiles::CrateVersionSource { let current_version = package.version.parse().into_diagnostic()?;
name, let target = fetcher.target().into();
version: package.version.parse().into_diagnostic()?,
source: metafiles::Source::cratesio_registry(),
};
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 } => { Resolution::InstallFromSource { package } => {
let desired_targets = opts.desired_targets.get().await; let desired_targets = opts.desired_targets.get().await;
@ -54,11 +63,9 @@ pub async fn install(
async fn install_from_package( async fn install_from_package(
fetcher: Arc<dyn Fetcher>, fetcher: Arc<dyn Fetcher>,
opts: Arc<Options>, opts: Arc<Options>,
cvs: metafiles::CrateVersionSource,
version: String,
bin_path: PathBuf, bin_path: PathBuf,
bin_files: Vec<bins::BinFile>, bin_files: Vec<bins::BinFile>,
) -> Result<Option<MetaData>> { ) -> Result<Option<Vec<CompactString>>> {
// Download package // Download package
if opts.dry_run { if opts.dry_run {
info!("Dry run, not downloading package"); info!("Dry run, not downloading package");
@ -108,12 +115,9 @@ async fn install_from_package(
} }
} }
Ok(Some(MetaData { Ok(Some(
bins: bin_files.into_iter().map(|bin| bin.base_name).collect(), bin_files.into_iter().map(|bin| bin.base_name).collect(),
cvs, ))
version_req: version,
target: fetcher.target().to_string(),
}))
}) })
} }

View file

@ -344,11 +344,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
block_in_place(|| { block_in_place(|| {
if !custom_install_path { if !custom_install_path {
debug!("Writing .crates.toml"); debug!("Writing .crates.toml");
metafiles::v1::CratesToml::append( metafiles::v1::CratesToml::append(metadata_vec.iter())?;
metadata_vec
.iter()
.map(|metadata| (&metadata.cvs, metadata.bins.clone())),
)?;
} }
if opts.no_cleanup { if opts.no_cleanup {

View file

@ -23,30 +23,6 @@ pub struct MetaData {
pub target: CompactString, pub target: CompactString,
pub bins: Vec<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)] #[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum SourceType { 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)] #[derive(Debug, Diagnostic, Error)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]

View file

@ -15,6 +15,16 @@ pub struct CrateVersionSource {
pub source: Source, 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)] #[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum Source { pub enum Source {
Git(Url), 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 { impl FromStr for CrateVersionSource {
type Err = CvsParseError; type Err = CvsParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {

View file

@ -11,7 +11,7 @@ use miette::Diagnostic;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
use super::CrateVersionSource; use super::{binstall_v1::MetaData, CrateVersionSource};
use crate::{cargo_home, create_if_not_exist, FileLock}; use crate::{cargo_home, create_if_not_exist, FileLock};
#[derive(Clone, Debug, Default, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
@ -71,13 +71,13 @@ impl CratesToml {
iter: Iter, iter: Iter,
) -> Result<(), CratesTomlParseError> ) -> Result<(), CratesTomlParseError>
where 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 file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
let mut c1 = Self::load_from_reader(&mut *file)?; let mut c1 = Self::load_from_reader(&mut *file)?;
for (cvs, bins) in iter { for metadata in iter {
c1.insert(cvs, bins); c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone());
} }
file.rewind()?; file.rewind()?;
@ -88,7 +88,7 @@ impl CratesToml {
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError> pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
where where
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<CompactString>)>, Iter: IntoIterator<Item = &'a MetaData>,
{ {
Self::append_to_path(Self::default_path()?, iter) Self::append_to_path(Self::default_path()?, iter)
} }