-
Notifications
You must be signed in to change notification settings - Fork 150
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
Conversation
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.
Thank you for contributing to cargo-edit, @alanpoon!
Comments:
- I've left some inline comments.
- We had a discussion in Support for working with features #193 about the format of output. Ideally, we want to extract the information about available features from the crate's
Cargo.toml
and print a nicely formatted feature list, but that can be done in a separate PR.
README.md
Outdated
@@ -71,7 +71,8 @@ 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. | |||
|
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:
$ cargo build --help 2>&1 | grep -e "--features"
--features FEATURES Space-separated list of features to also build
(and maybe replace also build
with add
)
.travis.yml
Outdated
@@ -44,4 +44,4 @@ env: | |||
- secure: XXYE8x015gOSJ+DD9FeCdEKQTeU6mZ5j47Nh1ncZTkdN6+RnrN/DxAwccvh9u6t8/V6WN868AHIvTc0nA031mqwnh6vcQDr3Ak8ydJCTM8KFgwsuESokSZYkbiiQd/FWTBri/P9H5jejCw3aSFQZUVeCpu2Zxp0N9/Jn3ky7NVknBOVdz21jRC/KivBkpbvOCj8iEtGZuYSQu3Gpq1wbuZaqDdgg6JVvrYy7LbUNGe0rHMjCcyuiutqp6R8zIQoC7p2kmoHkXrTaDKQrpmSi+uLJJE7KqML9tFFnvjvmgroRYDCk0gc/fr/GunikvUkcczBBSsVXWj66EpriQxwdEnI1uhNtKUOGALIAMiLz2k0rbLl1wFLOvFlhEPhDGvt+mw+h2BIxoyy9mwVioYym7V8pksw0UuCuLuyAp1Rc89iT6/N3HfMFPZeTBeudktUb5ijHgJyobVwakxKXgKqELbrhUhmzBf8fwtmPH+Lnganyrz4qcKBnuUl2fSp6MIaGIeSZZwigggV8QSXw9u/1c32RhQs47ZDRJX2lzEzVyAVZ0liyn32hWFI6Ctur4jNV7LWtOcDqdYZZfqyZOL/HehNFtDoJfrYsp/KZvmBipmOkrNsKCKEAdvUoeTDXo4ME31tjGtiPbHIdRuBJJqt38gm1AKyoVJKp2KFGDsI0djA= | |||
branches: | |||
except: | |||
- staging.tmp |
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.
Could you revert this change?
src/bin/add/args.rs
Outdated
} | ||
|
||
|
||
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); |
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.
This formatting seems odd (why 8 whitespaces?).
@@ -14,6 +14,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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Ordian,
Thanks for your suggestion. "cargo list" or "cargo read-manifest" can only read the local cargo.toml manifest file. When you add a dependency to your project, you don't save a copy of the manifest file of the dependencies. Without the manifest file of the dependency, there is no way of returning the list of the available features. I think this enhancement is only useful for experienced users who would check out the available features themselves. I am only motivated in using cargo-edit because Cargo does not support Target specific features. rust-lang/cargo#1197
With cargo-edit being able to specify features, I can use continuous integration to build various target builds with dependencies with various features.
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.
- We download
Cargo.toml
locally for git dependencies in cargo-edit (and download json for crates.io dependencies), so a proper way to extract feature list would be to create a functionget_features_from_manifest
similar toget_name_from_manifest
. But that can be done in a separate PR. - The point of the comment is different: if user of cargo-edit does not specify
--features
when adding a dependency, he probably wants a default feature list, not the empty one. Btw, current CI failure is relevant.
src/dependency.rs
Outdated
@@ -95,7 +105,13 @@ impl Dependency { | |||
if self.optional { | |||
data.get_or_insert("optional", optional); | |||
} | |||
|
|||
if !self.features.is_empty() { | |||
let mut to_array = toml_edit::Array::default(); |
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.
toml_edit::Value
implements FromIterator
, so we can simplify this:
use std::iter::FromIterator;
let features = toml_edit::Value::from_iter(self.features.iter().cloned());
data.get_or_insert("features", features);
but we need to specify feature list only if user specified it.
tests/cargo-add.rs
Outdated
// dependency present afterwards | ||
let toml = get_toml(&manifest); | ||
let val = &toml["dependencies"]["cargo-edit"]["features"]; | ||
let result = if let toml_edit::Item::Value(toml_edit::Value::Array(ref arr_z)) = *val { |
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 haven't tried this, but it should work:
let val = toml["dependencies"]["cargo-edit"]["features"][0].as_str();
assert_eq!(val, Some("jui"));
Thanks for the changes. Do you think we should also add a cc @killercup |
README.md
Outdated
@@ -71,7 +71,8 @@ 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 | |||
|
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.
stray whitespace here
this works as expected, we might want to change the user output though
|
Thanks @qmx, good catch! But we may change the output to the form mentioned in #193 in the future PR. @alanpoon, could you add an output test similar to this one, fix the ordering of |
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.
Could you also please rebase and squash this PR down to a single commit when you're ready? Thanks!
src/bin/add/main.rs
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Could you place this output after line 109 (and change writeln
to write
), so that the output would be
... to dependencies with features: jui
rather than
to features jui dependencies
Friendly ping! This would be a nice feature to have. @alanpoon if you like I could adopt the PR? |
Hi derekdreery, |
I'd also like to see this PR get merged, it's been a while since it was submitted and thus getting stale with conflicts. The changes that were requested were done? Can it be reviewed again and if valid then re-based and merged? |
Master has changed substantially, this PR needs rebasing and then I'll review it. |
Credit to @alanpoon for the original implementation of this feature. This patch is a rebase of the original PR (killercup#194) with minor tidying up. Also contains some unrelated clippy and fmt fixes for rust 1.41.0.
* add --features='nose mouth' Credit to @alanpoon for the original implementation of this feature. This patch is a rebase of the original PR (#194) with minor tidying up. Also contains some unrelated clippy and fmt fixes for rust 1.41.0. * mention cargo-feature; update README * forbid specifying multiple crates with --features * fixup! forbid specifying multiple crates with --features
I believe this was superceded by #395 |
cc #193