Skip to content

Commit

Permalink
use new 'RegistryReq' for public functions
Browse files Browse the repository at this point in the history
(registry.rs) broke `registry_url` down into smaller functions
(registry.rs) implemented RegistryReq struct, meant to abstract the idea of 'getting a registry'
	- allows obtaining a registry without attaching a project directory, or traversing the file system (direct URL)
(fetch.rs) seperate concerns with get_latest_dependency
	- caller shouldn't have to get the registry URL - just know the context of the request
(upgrade/main.rs) add comments to & reorganize DesiredUpgrades::get_upgraded
  • Loading branch information
chrismooredev committed Dec 13, 2020
1 parent ff1e158 commit 03f6d74
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 102 deletions.
15 changes: 6 additions & 9 deletions src/bin/add/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Handle `cargo add` arguments
use cargo_edit::{find, registry_url, Dependency};
use cargo_edit::{get_latest_dependency, CrateName};
use cargo_edit::{find, Dependency};
use cargo_edit::{get_latest_dependency, CrateName, RegistryReq};
use std::path::PathBuf;
use structopt::StructOpt;

Expand Down Expand Up @@ -201,18 +201,15 @@ impl Args {
if let Some(version) = &self.vers {
dependency = dependency.set_version(parse_version_req(version)?);
}
let registry_url = if let Some(registry) = &self.registry {
Some(registry_url(&find(&self.manifest_path)?, Some(registry))?)
} else {
None
};

if self.git.is_none() && self.path.is_none() && self.vers.is_none() {
let dep = get_latest_dependency(
crate_name.name(),
self.allow_prerelease,
&find(&self.manifest_path)?,
&registry_url,
RegistryReq::project(
self.registry.as_ref().map(String::as_ref),
&find(&self.manifest_path)?,
),
)?;
let v = format!(
"{prefix}{version}",
Expand Down
7 changes: 4 additions & 3 deletions src/bin/add/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate error_chain;
use crate::args::{Args, Command};
use cargo_edit::{
find, manifest_from_pkgid, registry_url, update_registry_index, Dependency, Manifest,
RegistryReq,
};
use std::borrow::Cow;
use std::io::Write;
Expand Down Expand Up @@ -124,10 +125,10 @@ fn handle_add(args: &Args) -> Result<()> {
let deps = &args.parse_dependencies()?;

if !args.offline && std::env::var("CARGO_IS_TEST").is_err() {
let url = registry_url(
&find(&manifest_path)?,
let url = registry_url(&RegistryReq::project(
args.registry.as_ref().map(String::as_ref),
)?;
&find(&manifest_path)?,
))?;
update_registry_index(&url)?;
}

Expand Down
85 changes: 50 additions & 35 deletions src/bin/upgrade/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern crate error_chain;
use crate::errors::*;
use cargo_edit::{
find, get_latest_dependency, manifest_from_pkgid, registry_url, update_registry_index,
CrateName, Dependency, LocalManifest,
CrateName, Dependency, LocalManifest, RegistryReq,
};
use failure::Fail;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -393,44 +393,58 @@ impl DesiredUpgrades {
fn get_upgraded(self, allow_prerelease: bool, manifest_path: &Path) -> Result<ActualUpgrades> {
self.0
.into_iter()
.map(
|(
dep,
UpgradeMetadata {
registry,
version,
is_prerelease,
},
)| {
if let Some(v) = version {
Ok((dep, v))
} else {
let registry_url = match registry {
Some(x) => Some(Url::parse(&x).map_err(|_| {
.map(|(dep, meta)| {
match meta.version {
// use previous version
Some(v) => Ok((dep, v)),

// no version exists - get latest
None => {
// if a registry exists on a dependency, parse it as a URL
let registry_url = meta
.registry
.as_ref()
.map(|s| Url::parse(&s))
.transpose()
.map_err(|_| {
ErrorKind::CargoEditLib(::cargo_edit::ErrorKind::InvalidCargoConfig)
})?),
None => None,
})?;

let use_prerelease = allow_prerelease || meta.is_prerelease;

let latest_dep = if let Some(url) = registry_url {
// Configured registry URL
get_latest_dependency(
&dep.name,
use_prerelease,
RegistryReq::custom(url),
)
} else {
// no configured registry - use crates.io
get_latest_dependency(
&dep.name,
use_prerelease,
// specifying registries by name must be qualified with a cargo.toml
// specifying purely by URL may work, but will need to call this function differently for knowing URL vs knowing name
RegistryReq::project(
(&meta.registry).as_ref().map(AsRef::as_ref),
manifest_path.parent().unwrap(),
),
)
};
let allow_prerelease = allow_prerelease || is_prerelease;
get_latest_dependency(
&dep.name,
allow_prerelease,
manifest_path,
&registry_url,
)
.map(|new_dep| {
(
dep,
new_dep

latest_dep
.map(|latest_dep| {
latest_dep
.version()
.expect("Invalid dependency type")
.to_string(),
)
})
.chain_err(|| "Failed to get new version")
.into()
})
.map(|new_version| (dep, new_version))
.chain_err(|| "Failed to get new version")
}
},
)
}
})
.collect::<Result<_>>()
.map(ActualUpgrades)
}
Expand Down Expand Up @@ -459,7 +473,8 @@ fn process(args: Args) -> Result<()> {
let all = workspace || all;

if !args.offline && !to_lockfile && std::env::var("CARGO_IS_TEST").is_err() {
let url = registry_url(&find(&manifest_path)?, None)?;
// get the project's crates.io registry URL
let url = registry_url(&RegistryReq::project(None, &find(&manifest_path)?))?;
update_registry_index(&url)?;
}

Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ error_chain! {
ReadHomeDirFailure {
description("Failed to read home directory")
}
/// Failed to absolutely resolve a relative directory
DirectoryResolutionFailure(dir: PathBuf) {
description("Failed to canonicalize a directory")
display("The path `{}` has failed to resolve to an absolute path", dir.display())
}
/// Invalid JSON in registry index
InvalidSummaryJson {
description("Invalid JSON in registry index")
Expand Down
12 changes: 4 additions & 8 deletions src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::errors::*;
use crate::registry::{registry_path, registry_path_from_url};
use crate::registry::{registry_path, registry_path_from_url, RegistryReq};
use crate::{Dependency, Manifest};
use regex::Regex;
use std::env;
Expand Down Expand Up @@ -30,8 +30,7 @@ struct CrateVersion {
pub fn get_latest_dependency(
crate_name: &str,
flag_allow_prerelease: bool,
manifest_path: &Path,
registry: &Option<Url>,
registry: RegistryReq,
) -> Result<Dependency> {
if env::var("CARGO_IS_TEST").is_ok() {
// We are in a simulated reality. Nothing is real here.
Expand All @@ -53,12 +52,9 @@ pub fn get_latest_dependency(
return Err(ErrorKind::EmptyCrateName.into());
}

let registry_path = match registry {
Some(url) => registry_path_from_url(url)?,
None => registry_path(manifest_path, None)?,
};
let registry_cache = registry_path(&registry)?;

let crate_versions = fuzzy_query_registry_index(crate_name, &registry_path)?;
let crate_versions = fuzzy_query_registry_index(crate_name, &registry_cache)?;

let dep = read_latest_version(&crate_versions, flag_allow_prerelease)?;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ pub use crate::fetch::{
};
pub use crate::manifest::{find, LocalManifest, Manifest};
pub use crate::metadata::manifest_from_pkgid;
pub use crate::registry::registry_url;
pub use crate::registry::{registry_url, RegistryReq};
Loading

0 comments on commit 03f6d74

Please sign in to comment.