Skip to content

Commit

Permalink
Add cargo-upgrade as a new command
Browse files Browse the repository at this point in the history
This adds `cargo upgrade` as a more fully featured replacement for
`cargo add --update-only`.

Usage:

```plain
Upgrade all dependencies in a manifest file to the latest version.

Usage:
    cargo upgrade [--dependency <dep>...] [--manifest-path <path>]
    cargo upgrade (-h | --help)
    cargo upgrade (-V | --version)

Options:
    -d --dependency <dep>       Specific dependency to upgrade. If this
option is used, only the
                                specified dependencies will be upgraded.
    --manifest-path <path>      Path to the manifest to upgrade.
    -h --help                   Show this help page.
    -V --version                Show version.

Dev, build, and all target dependencies will also be upgraded. Only
dependencies from crates.io are
supported. Git/path dependencies will be ignored.
```

Resolves killercup#74.
  • Loading branch information
bjgill committed Jul 19, 2017
1 parent d02ec5a commit 08f07d1
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 180 deletions.
62 changes: 26 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ path = "src/bin/add/main.rs"
[[bin]]
name = "cargo-rm"
path = "src/bin/rm/main.rs"
[[bin]]
name = "cargo-upgrade"
path = "src/bin/upgrade/main.rs"

[dependencies]
docopt = "0.8"
Expand Down
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# cargo edit

This tool extends [Cargo](http://doc.crates.io/) to allow you to add and remove dependencies by modifying your `Cargo.toml` file from the command line
This tool extends [Cargo](http://doc.crates.io/) to allow you to add, remove, and upgrade dependencies by modifying your `Cargo.toml` file from the command line.

Currently available subcommands:

- [`cargo add`](#cargo-add)
- [`cargo rm`](#cargo-rm)
- [`cargo upgrade`](#cargo-upgrade)

[![Build Status](https://travis-ci.org/killercup/cargo-edit.svg?branch=master)](https://travis-ci.org/killercup/cargo-edit)
[![Build status](https://ci.appveyor.com/api/projects/status/m23rnkaxhipb23i9/branch/master?svg=true)](https://ci.appveyor.com/project/killercup/cargo-edit/branch/master)
Expand Down Expand Up @@ -91,8 +92,6 @@ Options:
--upgrade=<method> Choose method of semantic version upgrade. Must be one of
"none" (exact version), "patch" (`~` modifier), "minor"
(`^` modifier, default), or "all" (`>=`).
--update-only If the dependency already exists, it will have its version updated,
preserving all other fields. The dependency will not be added if absent.
--manifest-path=<path> Path to the manifest to add a dependency to.
--allow-prerelease Include prerelease versions when fetching from crates.io (e.g.
'0.6.0-alpha'). Defaults to false.
Expand Down Expand Up @@ -140,6 +139,40 @@ Options:
Remove a dependency from a Cargo.toml manifest file.
```

### `cargo upgrade`

Upgrade dependencies in your `Cargo.toml` to their latest versions.

#### Examples

```sh
# Upgrade all dependencies
$ cargo upgrade
# Upgrade libc and serde
$ cargo upgrade -d libc --dependency serde
```

#### Usage

```plain
Upgrade all dependencies in a manifest file to the latest version.
Usage:
cargo upgrade [--dependency <dep>...] [--manifest-path <path>]
cargo upgrade (-h | --help)
cargo upgrade (-V | --version)
Options:
-d --dependency <dep> Specific dependency to upgrade. If this option is used, only the
specified dependencies will be upgraded.
--manifest-path <path> Path to the manifest to upgrade.
-h --help Show this help page.
-V --version Show version.
Dev, build, and all target dependencies will also be upgraded. Only dependencies from crates.io are
supported. Git/path dependencies will be ignored.
```

## License

Apache-2.0/MIT
46 changes: 22 additions & 24 deletions src/bin/add/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Handle `cargo add` arguments

use cargo_edit::Dependency;
use fetch::{get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path,
get_latest_dependency};
use cargo_edit::{get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path,
get_latest_dependency};
use semver;
use std::error::Error;
use std::path::PathBuf;
Expand Down Expand Up @@ -34,8 +34,6 @@ pub struct Args {
pub flag_version: bool,
/// `---upgrade`
pub flag_upgrade: Option<String>,
/// `--update-only`
pub flag_update_only: bool,
/// '--fetch-prereleases'
pub flag_allow_prerelease: bool,
}
Expand Down Expand Up @@ -86,28 +84,29 @@ impl Args {


let dependency = if !crate_name_is_url_or_path(&self.arg_crate) {
let dependency = Dependency::new(&self.arg_crate);
let dependency = Dependency::new(&self.arg_crate);

if let Some(ref version) = self.flag_vers {
semver::VersionReq::parse(version)?;
dependency.set_version(version)
} else if let Some(ref repo) = self.flag_git {
dependency.set_git(repo)
} else if let Some(ref path) = self.flag_path {
dependency.set_path(path.to_str().unwrap())
} else {
let dep = get_latest_dependency(&self.arg_crate, self.flag_allow_prerelease)?;
let v = format!("{prefix}{version}",
prefix = self.get_upgrade_prefix().unwrap_or(""),
// if version is unavailable
// `get_latest_dependency` must have returned `Err(FetchVersionError::GetVersion)`
version = dep.version().unwrap_or_else(|| unreachable!()));
dep.set_version(&v)
}
if let Some(ref version) = self.flag_vers {
semver::VersionReq::parse(version)?;
dependency.set_version(version)
} else if let Some(ref repo) = self.flag_git {
dependency.set_git(repo)
} else if let Some(ref path) = self.flag_path {
dependency.set_path(path.to_str().unwrap())
} else {
parse_crate_name_from_uri(&self.arg_crate)?
let dep = get_latest_dependency(&self.arg_crate, self.flag_allow_prerelease)?;
let v = format!(
"{prefix}{version}",
prefix = self.get_upgrade_prefix().unwrap_or(""),
// If version is unavailable `get_latest_dependency` must have
// returned `Err(FetchVersionError::GetVersion)`
version = dep.version().unwrap_or_else(|| unreachable!())
);
dep.set_version(&v)
}
.set_optional(self.flag_optional);
} else {
parse_crate_name_from_uri(&self.arg_crate)?
}.set_optional(self.flag_optional);

Ok(vec![dependency])
}
Expand Down Expand Up @@ -146,7 +145,6 @@ impl Default for Args {
flag_manifest_path: None,
flag_version: false,
flag_upgrade: None,
flag_update_only: false,
flag_allow_prerelease: false,
}
}
Expand Down

0 comments on commit 08f07d1

Please sign in to comment.