-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with features #194
Changes from 18 commits
411ffa9
b1c7e3b
b3b416c
45ffd72
0fc815c
311fc15
24966a8
164de17
f30a63a
56e6907
67349ad
e85b85a
1895d1e
547d93b
68f0a64
50aa8aa
cf2444c
5bb92f3
4ba1f6d
17adeb8
21676e8
c7f287c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,8 +60,8 @@ $ cargo add lib/trial-and-error/ | |
```plain | ||
$ cargo add --help | ||
Usage: | ||
cargo add <crate> [--dev|--build|--optional] [--vers=<ver>|--git=<uri>|--path=<uri>] [options] | ||
cargo add <crates>... [--dev|--build|--optional] [options] | ||
cargo add <crate> [--dev|--build|--optional|--features=<features>] [--vers=<ver>|--git=<uri>|--path=<uri>] [options] | ||
cargo add <crates>... [--dev|--build|--optional|--features=<features>] [options] | ||
cargo add (-h|--help) | ||
cargo add --version | ||
|
||
|
@@ -71,6 +71,7 @@ Specify what crate to add: | |
`cargo add [email protected]`. | ||
--git <uri> Specify a git repository to download the crate from. | ||
--path <uri> Specify the path the crate should be loaded from. | ||
--features <FEATURES> Space-separated list of features to add | ||
|
||
Specify where to add the crate: | ||
-D --dev Add crate as development dependency. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ use errors::*; | |
|
||
static USAGE: &'static str = r#" | ||
Usage: | ||
cargo add <crate> [--dev|--build|--optional] [--vers=<ver>|--git=<uri>|--path=<uri>] [options] | ||
cargo add <crates>... [--dev|--build|--optional] [options] | ||
cargo add <crate> [--dev|--build|--optional|--features=<features>] [--vers=<ver>|--git=<uri>|--path=<uri>] [options] | ||
cargo add <crates>... [--dev|--build|--optional|--features=<features>] [options] | ||
cargo add (-h|--help) | ||
cargo add --version | ||
|
||
|
@@ -55,6 +55,7 @@ Specify where to add the crate: | |
for `dev-dependencies` or `build-dependencies`. | ||
--target <target> Add as dependency to the given target platform. This does not work | ||
for `dev-dependencies` or `build-dependencies`. | ||
--features <FEATURES> Space-separated list of features to add | ||
|
||
Options: | ||
--upgrade=<method> Choose method of semantic version upgrade. Must be one of | ||
|
@@ -77,7 +78,7 @@ crates.io registry suggests. One goal of `cargo add` is to prevent you from usin | |
dependencies (version set to "*"). | ||
"#; | ||
|
||
fn print_msg(dep: &Dependency, section: &[String], optional: bool) -> Result<()> { | ||
fn print_msg(dep: &Dependency, section: &[String], optional: bool, features: Option<String>) -> Result<()> { | ||
let colorchoice = if atty::is(atty::Stream::Stdout) { | ||
ColorChoice::Auto | ||
} else { | ||
|
@@ -97,6 +98,9 @@ fn print_msg(dep: &Dependency, section: &[String], optional: bool) -> Result<()> | |
if optional { | ||
write!(output, " optional")?; | ||
} | ||
if let Some(f) = features { | ||
write!(output, " features {}", f)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you place this output after line 109 (and change
rather than
|
||
} | ||
let section = if section.len() == 1 { | ||
section[0].clone() | ||
} else { | ||
|
@@ -114,7 +118,7 @@ fn handle_add(args: &Args) -> Result<()> { | |
deps.iter() | ||
.map(|dep| { | ||
if !args.flag_quiet { | ||
print_msg(dep, &args.get_section(), args.flag_optional)?; | ||
print_msg(dep, &args.get_section(), args.flag_optional, args.flag_features.clone())?; | ||
} | ||
manifest | ||
.insert_into_table(&args.get_section(), dep) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use toml_edit; | ||
use std::iter::FromIterator; | ||
|
||
#[derive(Debug, Hash, PartialEq, Eq, Clone)] | ||
enum DependencySource { | ||
|
@@ -14,6 +15,7 @@ pub struct Dependency { | |
pub name: String, | ||
optional: bool, | ||
source: DependencySource, | ||
features: Vec<String>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to distinguish an empty feature list from the default feature list (not specifying the feature flag). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Ordian, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
impl Default for Dependency { | ||
|
@@ -22,6 +24,7 @@ impl Default for Dependency { | |
name: "".into(), | ||
optional: false, | ||
source: DependencySource::Version("0.1.0".into()), | ||
features: vec![], | ||
} | ||
} | ||
} | ||
|
@@ -58,6 +61,13 @@ impl Dependency { | |
self.optional = opt; | ||
self | ||
} | ||
/// Set whether the features is array of string | ||
pub fn set_features(mut self, features: Option<String>) -> Dependency { | ||
if let Some(f) = features { | ||
self.features = f.split(' ').map(String::from).collect::<Vec<String>>(); | ||
} | ||
self | ||
} | ||
|
||
/// Get version of dependency | ||
pub fn version(&self) -> Option<&str> { | ||
|
@@ -74,11 +84,12 @@ impl Dependency { | |
/// or the path/git repository as an `InlineTable`. | ||
/// (If the dependency is set as `optional`, an `InlineTable` is returned in any case.) | ||
pub fn to_toml(&self) -> (String, toml_edit::Item) { | ||
let data: toml_edit::Item = match (self.optional, self.source.clone()) { | ||
let data: toml_edit::Item = match (self.optional, self.features.len(), self.source.clone()) | ||
{ | ||
// Extra short when version flag only | ||
(false, DependencySource::Version(v)) => toml_edit::value(v), | ||
(false, 0, DependencySource::Version(v)) => toml_edit::value(v), | ||
// Other cases are represented as an inline table | ||
(optional, source) => { | ||
(optional, _len, source) => { | ||
let mut data = toml_edit::InlineTable::default(); | ||
|
||
match source { | ||
|
@@ -95,7 +106,10 @@ impl Dependency { | |
if self.optional { | ||
data.get_or_insert("optional", optional); | ||
} | ||
|
||
if !self.features.is_empty() { | ||
let features = toml_edit::Value::from_iter(self.features.iter().cloned()); | ||
data.get_or_insert("features", features); | ||
} | ||
data.fmt(); | ||
toml_edit::value(toml_edit::Value::InlineTable(data)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should follow cargo in describing the feature flag:
(and maybe replace
also build
withadd
)