mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-08 03:36:37 +00:00
fix updating of installed crates manifest on custom sparse registry (#2178)
* Add either v1.15.0 to binstalk-registry Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Add `SourceType::Sparse` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Add `Registry::crate_source` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Use `Registry::crate_siouy Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Add support for `Source::Sparse` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Optimize `CratesToml::append_to_file` Bulkify remove Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix typing in cargo_crates_v1.rs and optimize append_to_file Use `Vec::reserve_exact ` in `append_to_file` to avoid unnecessary allocation Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix Registry::url Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix unused variable in `Registry::crate_source` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt in cargo_crates_v1.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt in binstalk-registry/src/lib.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt in cargo_crates_v1.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Refactor: Extract new fn CratesToml::add_crate Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `<Source as Display>::fmt` impl for `Source::Sprase` Add `/` to the end of the url if it doesn't have one Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix crate_version_source.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix lifetime of `CrateToml::add_crate` API Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix lifetime of `CratesToml<'::add_crate` API Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * cargo fmt crate_version_source.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `Registery::crate_source` Match sparse/git crates.io registry to standardrised `CrateSource::cratesio_registry()`. Also optimize it to avoid unnecessary `.to_string()` and `Url::parse` for sparse registry. Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `GitRegistry::url` ret type Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `SparseRegistery::url` return type Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix typing in `Registry::crate_source` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Rm unused import in sparse_registry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Rm unused import in git_registry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * cargo fmt binstalk-registry/src/lib.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Rm unused dep either from binstalk-registry Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --------- Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
parent
e8c9cc3599
commit
25a52038ef
7 changed files with 101 additions and 56 deletions
|
@ -37,7 +37,7 @@ pub struct CratesToml<'a> {
|
|||
v1: Vec<(String, Cow<'a, [CompactString]>)>,
|
||||
}
|
||||
|
||||
impl CratesToml<'_> {
|
||||
impl<'v1> CratesToml<'v1> {
|
||||
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
|
||||
Ok(cargo_home()?.join(".crates.toml"))
|
||||
}
|
||||
|
@ -68,9 +68,14 @@ impl CratesToml<'_> {
|
|||
}
|
||||
|
||||
pub fn remove(&mut self, name: &str) {
|
||||
self.remove_all(&[name]);
|
||||
}
|
||||
|
||||
/// * `sorted_names` - must be sorted
|
||||
pub fn remove_all(&mut self, sorted_names: &[&str]) {
|
||||
self.v1.retain(|(s, _bin)| {
|
||||
s.split_once(' ')
|
||||
.map(|(crate_name, _rest)| crate_name != name)
|
||||
.map(|(crate_name, _rest)| sorted_names.binary_search(&crate_name).is_err())
|
||||
.unwrap_or_default()
|
||||
});
|
||||
}
|
||||
|
@ -106,53 +111,52 @@ impl CratesToml<'_> {
|
|||
self.write_to_file(&mut file)
|
||||
}
|
||||
|
||||
pub fn append_to_file<'a, Iter>(file: &mut File, iter: Iter) -> Result<(), CratesTomlParseError>
|
||||
where
|
||||
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||
{
|
||||
fn inner(
|
||||
file: &mut File,
|
||||
iter: &mut dyn Iterator<Item = &CrateInfo>,
|
||||
) -> Result<(), CratesTomlParseError> {
|
||||
let mut c1 = CratesToml::load_from_reader(&mut *file)?;
|
||||
pub fn add_crate(&mut self, metadata: &'v1 CrateInfo) {
|
||||
let name = &metadata.name;
|
||||
let version = &metadata.current_version;
|
||||
let source = Source::from(&metadata.source);
|
||||
|
||||
for metadata in iter {
|
||||
let name = &metadata.name;
|
||||
let version = &metadata.current_version;
|
||||
let source = Source::from(&metadata.source);
|
||||
self.v1.push((
|
||||
format!("{name} {version} ({source})"),
|
||||
Cow::borrowed(&metadata.bins),
|
||||
));
|
||||
}
|
||||
|
||||
c1.remove(name);
|
||||
c1.v1.push((
|
||||
format!("{name} {version} ({source})"),
|
||||
Cow::borrowed(&metadata.bins),
|
||||
));
|
||||
}
|
||||
pub fn append_to_file(
|
||||
file: &mut File,
|
||||
crates: &[CrateInfo],
|
||||
) -> Result<(), CratesTomlParseError> {
|
||||
let mut c1 = CratesToml::load_from_reader(&mut *file)?;
|
||||
|
||||
file.rewind()?;
|
||||
c1.write_to_file(file)?;
|
||||
let mut crate_names: Vec<_> = crates
|
||||
.iter()
|
||||
.map(|metadata| metadata.name.as_str())
|
||||
.collect();
|
||||
crate_names.sort_unstable();
|
||||
c1.remove_all(&crate_names);
|
||||
|
||||
Ok(())
|
||||
c1.v1.reserve_exact(crates.len());
|
||||
|
||||
for metadata in crates {
|
||||
c1.add_crate(metadata);
|
||||
}
|
||||
|
||||
inner(file, &mut iter.into_iter())
|
||||
file.rewind()?;
|
||||
c1.write_to_file(file)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn append_to_path<'a, Iter>(
|
||||
pub fn append_to_path(
|
||||
path: impl AsRef<Path>,
|
||||
iter: Iter,
|
||||
) -> Result<(), CratesTomlParseError>
|
||||
where
|
||||
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||
{
|
||||
crates: &[CrateInfo],
|
||||
) -> Result<(), CratesTomlParseError> {
|
||||
let mut file = create_if_not_exist(path.as_ref())?;
|
||||
Self::append_to_file(&mut file, iter)
|
||||
Self::append_to_file(&mut file, crates)
|
||||
}
|
||||
|
||||
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
|
||||
where
|
||||
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||
{
|
||||
Self::append_to_path(Self::default_path()?, iter)
|
||||
pub fn append(crates: &[CrateInfo]) -> Result<(), CratesTomlParseError> {
|
||||
Self::append_to_path(Self::default_path()?, crates)
|
||||
}
|
||||
|
||||
/// Return BTreeMap with crate name as key and its corresponding version
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
use std::{borrow::Cow, fmt, str::FromStr};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fmt::{self, Write as _},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use binstalk_types::maybe_owned::MaybeOwned;
|
||||
use compact_str::CompactString;
|
||||
|
@ -30,6 +34,7 @@ impl From<&CrateInfo> for CrateVersionSource {
|
|||
Git => Source::Git(url),
|
||||
Path => Source::Path(url),
|
||||
Registry => Source::Registry(url),
|
||||
Sparse => Source::Sparse(url),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -40,6 +45,7 @@ pub enum Source<'a> {
|
|||
Git(MaybeOwned<'a, Url>),
|
||||
Path(MaybeOwned<'a, Url>),
|
||||
Registry(MaybeOwned<'a, Url>),
|
||||
Sparse(MaybeOwned<'a, Url>),
|
||||
}
|
||||
|
||||
impl<'a> From<&'a CrateSource> for Source<'a> {
|
||||
|
@ -52,6 +58,7 @@ impl<'a> From<&'a CrateSource> for Source<'a> {
|
|||
Git => Self::Git(url),
|
||||
Path => Self::Path(url),
|
||||
Registry => Self::Registry(url),
|
||||
Sparse => Self::Sparse(url),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -125,6 +132,15 @@ impl fmt::Display for Source<'_> {
|
|||
Source::Git(url) => write!(f, "git+{url}"),
|
||||
Source::Path(url) => write!(f, "path+{url}"),
|
||||
Source::Registry(url) => write!(f, "registry+{url}"),
|
||||
Source::Sparse(url) => {
|
||||
let url = url.as_str();
|
||||
write!(f, "sparse+{url}")?;
|
||||
if url.ends_with("/") {
|
||||
Ok(())
|
||||
} else {
|
||||
f.write_char('/')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue