Skip to content

Commit

Permalink
Introduce Source::download_now
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Feb 6, 2019
1 parent 23fa400 commit 643d258
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
13 changes: 12 additions & 1 deletion src/cargo/core/source/mod.rs
Expand Up @@ -2,7 +2,8 @@ use std::collections::hash_map::HashMap;
use std::fmt;

use crate::core::{Dependency, Package, PackageId, Summary};
use crate::util::CargoResult;
use crate::core::package::PackageSet;
use crate::util::{CargoResult, Config};

mod source_id;

Expand Down Expand Up @@ -51,6 +52,16 @@ pub trait Source {
/// version specified.
fn download(&mut self, package: PackageId) -> CargoResult<MaybePackage>;

fn download_now(self: Box<Self>, package: PackageId, config: &Config) -> CargoResult<Package>
where
Self: std::marker::Sized,
{
let mut sources = SourceMap::new();
sources.insert(self);
let pkg_set = PackageSet::new(&[package], sources, config)?;
Ok(pkg_set.get_one(package)?.clone())
}

fn finish_download(&mut self, package: PackageId, contents: Vec<u8>) -> CargoResult<Package>;

/// Generates a unique string which represents the fingerprint of the
Expand Down
20 changes: 6 additions & 14 deletions src/cargo/ops/common_for_install_and_uninstall.rs
Expand Up @@ -7,8 +7,6 @@ use std::path::{Path, PathBuf};
use semver::VersionReq;
use serde::{Deserialize, Serialize};

use crate::core::package::PackageSet;
use crate::core::source::SourceMap;
use crate::core::PackageId;
use crate::core::{Dependency, Package, Source, SourceId};
use crate::sources::PathSource;
Expand Down Expand Up @@ -139,8 +137,11 @@ where
};
let dep = Dependency::parse_no_deprecated(name, vers_spec, source.source_id())?;
let deps = source.query_vec(&dep)?;
let pkgid = match deps.iter().map(|p| p.package_id()).max() {
Some(pkgid) => pkgid,
match deps.iter().map(|p| p.package_id()).max() {
Some(pkgid) => {
let pkg = Box::new(&mut source).download_now(pkgid, config)?;
Ok((pkg, Box::new(source)))
},
None => {
let vers_info = vers
.map(|v| format!(" with version `{}`", v))
Expand All @@ -152,16 +153,7 @@ where
vers_info
)
}
};

let pkg = {
let mut map = SourceMap::new();
map.insert(Box::new(&mut source));
PackageSet::new(&[pkgid], map, config)?
.get_one(pkgid)?
.clone()
};
Ok((pkg, Box::new(source)))
}
}
None => {
let candidates = list_all(&mut source)?;
Expand Down

0 comments on commit 643d258

Please sign in to comment.