Skip to content

Commit

Permalink
Merge #317
Browse files Browse the repository at this point in the history
317: Query crate versions from local registry index rather than HTTP API r=ordian a=DCjanus

Close #311 

Co-authored-by: DCjanus <[email protected]>
  • Loading branch information
bors[bot] and DCjanus committed Aug 7, 2019
2 parents 70b359b + 1193902 commit af51449
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 141 deletions.
203 changes: 186 additions & 17 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 16 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,24 @@ repository = "killercup/cargo-edit"
repository = "killercup/cargo-edit"

[dependencies]
cargo_metadata = "0.8.0"
env_proxy = "0.3.1"
cargo_metadata = "0.8.1"
env_proxy = "0.3.0"
error-chain = "0.12.1"
failure = "0.1.5"
regex = "1.1.6"
reqwest = "0.9.17"
serde = "1.0.91"
serde_derive = "1.0.91"
serde_json = "1.0.39"
termcolor = "1.0.4"
toml_edit = "0.1.4"
atty = "0.2.11"
structopt = "0.2.15"
regex = "1.2.1"
reqwest = "0.9.19"
serde = "1.0.98"
serde_derive = "1.0.98"
serde_json = "1.0.40"
termcolor = "1.0.5"
toml_edit = "0.1.5"
atty = "0.2.13"
structopt = "0.2.18"
dirs = "2.0.2"
git2 = "0.9.2"
hex = "0.3.2"
url = "2.1.0"
toml = "0.5.1"

[dependencies.semver]
features = ["serde"]
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ image: Visual Studio 2015

environment:
matrix:
- TARGET: x86_64-pc-windows-gnu
- TARGET: i686-pc-windows-gnu
# - TARGET: x86_64-pc-windows-gnu
# - TARGET: i686-pc-windows-gnu
- TARGET: x86_64-pc-windows-msvc
- TARGET: i686-pc-windows-msvc
install:
Expand Down
13 changes: 11 additions & 2 deletions src/bin/add/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Handle `cargo add` arguments

use cargo_edit::Dependency;
use cargo_edit::{find, Dependency};
use cargo_edit::{get_latest_dependency, CrateName};
use semver;
use std::path::PathBuf;
Expand Down Expand Up @@ -96,6 +96,10 @@ pub struct Args {
/// Do not print any output in case of success.
#[structopt(long = "quiet", short = "q")]
pub quiet: bool,

/// Run without accessing the network
#[structopt(long = "offline")]
pub offline: bool,
}

fn parse_version_req(s: &str) -> Result<&str> {
Expand Down Expand Up @@ -159,7 +163,11 @@ impl Args {
}

if self.git.is_none() && self.path.is_none() && self.vers.is_none() {
let dep = get_latest_dependency(crate_name.name(), self.allow_prerelease)?;
let dep = get_latest_dependency(
crate_name.name(),
self.allow_prerelease,
&find(&self.manifest_path)?,
)?;
let v = format!(
"{prefix}{version}",
prefix = self.get_upgrade_prefix(),
Expand Down Expand Up @@ -222,6 +230,7 @@ impl Default for Args {
allow_prerelease: false,
no_default_features: false,
quiet: false,
offline: true,
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/bin/add/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
extern crate error_chain;

use crate::args::{Args, Command};
use cargo_edit::{Dependency, Manifest};
use cargo_edit::{find, update_registry_index, Dependency, Manifest};
use std::io::Write;
use std::process;
use structopt::StructOpt;
Expand Down Expand Up @@ -81,6 +81,10 @@ fn handle_add(args: &Args) -> Result<()> {
let mut manifest = Manifest::open(manifest_path)?;
let deps = &args.parse_dependencies()?;

if !args.offline {
update_registry_index(&find(manifest_path)?)?;
}

deps.iter()
.map(|dep| {
if !args.quiet {
Expand Down
25 changes: 18 additions & 7 deletions src/bin/upgrade/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
extern crate error_chain;

use crate::errors::*;
use cargo_edit::{find, get_latest_dependency, CrateName, Dependency, LocalManifest};
use cargo_edit::{
find, get_latest_dependency, update_registry_index, CrateName, Dependency, LocalManifest,
};
use failure::Fail;
use std::collections::HashMap;
use std::io::Write;
Expand Down Expand Up @@ -76,6 +78,10 @@ struct Args {
/// Print changes to be made without making them.
#[structopt(long = "dry-run")]
dry_run: bool,

/// Run without accessing the network
#[structopt(long = "offline")]
pub offline: bool,
}

/// A collection of manifests.
Expand All @@ -89,9 +95,9 @@ impl Manifests {
if let Some(path) = manifest_path {
cmd.manifest_path(path);
}
let result = cmd
.exec()
.map_err(|e| Error::from(e.compat()).chain_err(|| "Failed to get workspace metadata"))?;
let result = cmd.exec().map_err(|e| {
Error::from(e.compat()).chain_err(|| "Failed to get workspace metadata")
})?;
result
.packages
.into_iter()
Expand Down Expand Up @@ -215,14 +221,14 @@ struct ActualUpgrades(HashMap<String, String>);
impl DesiredUpgrades {
/// Transform the dependencies into their upgraded forms. If a version is specified, all
/// dependencies will get that version.
fn get_upgraded(self, allow_prerelease: bool) -> Result<ActualUpgrades> {
fn get_upgraded(self, allow_prerelease: bool, manifest_path: &Path) -> Result<ActualUpgrades> {
self.0
.into_iter()
.map(|(name, version)| {
if let Some(v) = version {
Ok((name, v))
} else {
get_latest_dependency(&name, allow_prerelease)
get_latest_dependency(&name, allow_prerelease, manifest_path)
.map(|new_dep| {
(
name,
Expand Down Expand Up @@ -252,6 +258,10 @@ fn process(args: Args) -> Result<()> {
..
} = args;

if !args.offline {
update_registry_index(&find(&manifest_path)?)?;
}

let manifests = if all {
Manifests::get_all(&manifest_path)
} else {
Expand All @@ -260,7 +270,8 @@ fn process(args: Args) -> Result<()> {

let existing_dependencies = manifests.get_dependencies(dependency)?;

let upgraded_dependencies = existing_dependencies.get_upgraded(allow_prerelease)?;
let upgraded_dependencies =
existing_dependencies.get_upgraded(allow_prerelease, &find(&manifest_path)?)?;

manifests.upgrade(&upgraded_dependencies, dry_run)
}
Expand Down
34 changes: 26 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
error_chain! {
foreign_links {
Io(::std::io::Error) #[doc = "An error from the std::io module"];
Git(::git2::Error)#[doc = "An error from the git2 crate"];
}

errors {
/// Failed to fetch crate from crates.io
FetchVersionFailure {
description("Failed to fetch crate version from crates.io")
/// Failed to read home directory
ReadHomeDirFailure {
description("Failed to read home directory")
}
/// Invalid JSON in registry index
InvalidSummaryJson {
description("Invalid JSON in registry index")
}
/// Invalid JSON from crates.io response
InvalidCratesIoJson {
description("Invalid JSON (the crate may not exist)")
/// Given crate name is empty
EmptyCrateName{
description("Found empty crate name")
}
/// No crate by that name exists
NoCrate(name: String) {
description("The crate could not be found on crates.io.")
display("The crate `{}` could not be found on crates.io.", name)
description("The crate could not be found in registry index.")
display("The crate `{}` could not be found in registry index.", name)
}
/// No versions available
NoVersionsAvailable {
Expand Down Expand Up @@ -47,5 +56,14 @@ error_chain! {
description("non existent dependency")
display("The dependency `{}` could not be found in `{}`.", name, table)
}
/// Config of cargo is invalid
InvalidCargoConfig {
description("Invalid cargo config")
}
/// Unable to find the source specified by 'replace-with'
NoSuchSourceFound(name: String) {
description("Unable to find the source specified by 'replace-with'")
display("The source '{}' could not be found", name)
}
}
}
Loading

0 comments on commit af51449

Please sign in to comment.