diff options
author | oxalica <[email protected]> | 2019-12-13 10:16:34 +0000 |
---|---|---|
committer | oxalica <[email protected]> | 2019-12-13 10:16:34 +0000 |
commit | af4eb266457eb784010da28d80535f9fd38d4d1e (patch) | |
tree | 8fb6f839a14985fa5dae695ff074495b87b8dd7a /crates/ra_project_model/src | |
parent | 5eb5e80de99338daceb82c933e366f95f7e1719c (diff) |
Support setting cargo features
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 31 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 14 |
2 files changed, 38 insertions, 7 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 351997dcd..4a0437da3 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -6,6 +6,7 @@ use cargo_metadata::{CargoOpt, MetadataCommand}; | |||
6 | use ra_arena::{impl_arena_id, Arena, RawId}; | 6 | use ra_arena::{impl_arena_id, Arena, RawId}; |
7 | use ra_db::Edition; | 7 | use ra_db::Edition; |
8 | use rustc_hash::FxHashMap; | 8 | use rustc_hash::FxHashMap; |
9 | use serde::Deserialize; | ||
9 | 10 | ||
10 | use crate::Result; | 11 | use crate::Result; |
11 | 12 | ||
@@ -23,6 +24,20 @@ pub struct CargoWorkspace { | |||
23 | pub(crate) workspace_root: PathBuf, | 24 | pub(crate) workspace_root: PathBuf, |
24 | } | 25 | } |
25 | 26 | ||
27 | #[derive(Deserialize, Clone, Debug, PartialEq, Eq, Default)] | ||
28 | #[serde(rename_all = "camelCase", default)] | ||
29 | pub struct CargoFeatures { | ||
30 | /// Do not activate the `default` feature. | ||
31 | pub no_default_features: bool, | ||
32 | |||
33 | /// Activate all available features | ||
34 | pub all_features: bool, | ||
35 | |||
36 | /// List of features to activate. | ||
37 | /// This will be ignored if `cargo_all_features` is true. | ||
38 | pub features: Vec<String>, | ||
39 | } | ||
40 | |||
26 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 41 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
27 | pub struct Package(RawId); | 42 | pub struct Package(RawId); |
28 | impl_arena_id!(Package); | 43 | impl_arena_id!(Package); |
@@ -132,9 +147,21 @@ impl Target { | |||
132 | } | 147 | } |
133 | 148 | ||
134 | impl CargoWorkspace { | 149 | impl CargoWorkspace { |
135 | pub fn from_cargo_metadata(cargo_toml: &Path) -> Result<CargoWorkspace> { | 150 | pub fn from_cargo_metadata( |
151 | cargo_toml: &Path, | ||
152 | cargo_features: &CargoFeatures, | ||
153 | ) -> Result<CargoWorkspace> { | ||
136 | let mut meta = MetadataCommand::new(); | 154 | let mut meta = MetadataCommand::new(); |
137 | meta.manifest_path(cargo_toml).features(CargoOpt::AllFeatures); | 155 | meta.manifest_path(cargo_toml); |
156 | if cargo_features.all_features { | ||
157 | meta.features(CargoOpt::AllFeatures); | ||
158 | } else if cargo_features.no_default_features { | ||
159 | // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures` | ||
160 | // https://github.com/oli-obk/cargo_metadata/issues/79 | ||
161 | meta.features(CargoOpt::NoDefaultFeatures); | ||
162 | } else { | ||
163 | meta.features(CargoOpt::SomeFeatures(cargo_features.features.clone())); | ||
164 | } | ||
138 | if let Some(parent) = cargo_toml.parent() { | 165 | if let Some(parent) = cargo_toml.parent() { |
139 | meta.current_dir(parent); | 166 | meta.current_dir(parent); |
140 | } | 167 | } |
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 55ff4d6ef..d71b7031a 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -18,7 +18,7 @@ use rustc_hash::FxHashMap; | |||
18 | use serde_json::from_reader; | 18 | use serde_json::from_reader; |
19 | 19 | ||
20 | pub use crate::{ | 20 | pub use crate::{ |
21 | cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, | 21 | cargo_workspace::{CargoFeatures, CargoWorkspace, Package, Target, TargetKind}, |
22 | json_project::JsonProject, | 22 | json_project::JsonProject, |
23 | sysroot::Sysroot, | 23 | sysroot::Sysroot, |
24 | }; | 24 | }; |
@@ -60,11 +60,15 @@ impl PackageRoot { | |||
60 | } | 60 | } |
61 | 61 | ||
62 | impl ProjectWorkspace { | 62 | impl ProjectWorkspace { |
63 | pub fn discover(path: &Path) -> Result<ProjectWorkspace> { | 63 | pub fn discover(path: &Path, cargo_features: &CargoFeatures) -> Result<ProjectWorkspace> { |
64 | ProjectWorkspace::discover_with_sysroot(path, true) | 64 | ProjectWorkspace::discover_with_sysroot(path, true, cargo_features) |
65 | } | 65 | } |
66 | 66 | ||
67 | pub fn discover_with_sysroot(path: &Path, with_sysroot: bool) -> Result<ProjectWorkspace> { | 67 | pub fn discover_with_sysroot( |
68 | path: &Path, | ||
69 | with_sysroot: bool, | ||
70 | cargo_features: &CargoFeatures, | ||
71 | ) -> Result<ProjectWorkspace> { | ||
68 | match find_rust_project_json(path) { | 72 | match find_rust_project_json(path) { |
69 | Some(json_path) => { | 73 | Some(json_path) => { |
70 | let file = File::open(json_path)?; | 74 | let file = File::open(json_path)?; |
@@ -73,7 +77,7 @@ impl ProjectWorkspace { | |||
73 | } | 77 | } |
74 | None => { | 78 | None => { |
75 | let cargo_toml = find_cargo_toml(path)?; | 79 | let cargo_toml = find_cargo_toml(path)?; |
76 | let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml)?; | 80 | let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)?; |
77 | let sysroot = | 81 | let sysroot = |
78 | if with_sysroot { Sysroot::discover(&cargo_toml)? } else { Sysroot::default() }; | 82 | if with_sysroot { Sysroot::discover(&cargo_toml)? } else { Sysroot::default() }; |
79 | Ok(ProjectWorkspace::Cargo { cargo, sysroot }) | 83 | Ok(ProjectWorkspace::Cargo { cargo, sysroot }) |