Skip to content

Commit 510fac6

Browse files
committed
enable definition of features at command line
Add the `--no-default-features` and `--features` arguments. Fix #31
1 parent d6acfe4 commit 510fac6

File tree

7 files changed

+85
-4
lines changed

7 files changed

+85
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### next
2-
`check-all` target now checks all. Fix #27
2+
* `check-all` target now checks all - Fix #27
3+
* `--no-default-features` and `--features` - Fix #31
34

45
<a name="v1.1.2"></a>
56
### v1.1.2 - 2021/01/05

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bacon"
3-
version = "1.1.2"
3+
version = "1.1.3-dev"
44
authors = ["dystroy <[email protected]>"]
55
repository = "https://github.com/Canop/bacon"
66
description = "background rust compiler"

src/args.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ pub struct Args {
4949
#[argh(option, short = 'j')]
5050
pub job: Option<String>,
5151

52+
/// ignore features of both the package and the bacon job
53+
#[argh(switch)]
54+
pub no_default_features: bool,
55+
56+
/// comma separated list of features to ask cargo to compile with
57+
/// (if the job defines some, they're merged)
58+
#[argh(option)]
59+
pub features: Option<String>,
60+
5261
/// path to watch (must be a rust directory or inside)
5362
#[argh(option, short = 'p')]
5463
pub path: Option<String>,

src/job.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ pub struct Job {
1414
#[serde(default)]
1515
pub need_stdout: bool,
1616
}
17+
18+
//impl Job {
19+
// pub fn modified_command(&self, settings: &Settings) -> (String, Vec<String>) {
20+
// let tokens = command.iter();
21+
// let exec = tokens.next().unwrap(); // SAFETY: warranty in Job
22+
//
23+
// }
24+
//}

src/mission.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use {
44
cargo_metadata::MetadataCommand,
55
notify::{RecommendedWatcher, RecursiveMode, Watcher},
66
std::{
7+
collections::HashSet,
78
env,
89
fs,
910
path::PathBuf,
@@ -152,10 +153,51 @@ impl Mission {
152153
let mut command = Command::new(
153154
tokens.next().unwrap(), // implies a check in the job
154155
);
156+
let mut no_default_features_done = false;
157+
let mut features_done = false;
158+
let mut last_is_features = false;
155159
for arg in tokens {
160+
let mut arg = arg.to_string();
161+
if last_is_features {
162+
// arg is expected there to be the list of features
163+
features_done = true;
164+
match (&self.settings.features, self.settings.no_default_features) {
165+
(Some(features), false) => {
166+
// we take the features of both the job and the args
167+
arg = merge_features(&arg, &features);
168+
}
169+
(Some(features), true) => {
170+
// arg add features and remove the job ones
171+
arg = features.clone();
172+
}
173+
(None, true) => {
174+
// arg just remove the job features
175+
arg = "".to_string()
176+
}
177+
(None, false) => {
178+
// nothing to change
179+
}
180+
}
181+
last_is_features = false;
182+
} else if arg == "--no-default-features" {
183+
no_default_features_done = true;
184+
last_is_features = false;
185+
} else if arg == "--features" {
186+
last_is_features = true;
187+
}
156188
command.arg(arg);
157189
}
190+
if self.settings.no_default_features && !no_default_features_done {
191+
command.arg("--no-default-features");
192+
}
193+
if !features_done {
194+
if let Some(features) = &self.settings.features {
195+
command.arg("--features");
196+
command.arg(features);
197+
}
198+
}
158199
command.current_dir(&self.cargo_execution_directory);
200+
debug!("command: {:#?}", &command);
159201
command
160202
}
161203

@@ -164,3 +206,14 @@ impl Mission {
164206
self.job.need_stdout
165207
}
166208
}
209+
210+
fn merge_features(a: &str, b: &str) -> String {
211+
let mut features = HashSet::new();
212+
for feature in a.split(',') {
213+
features.insert(feature);
214+
}
215+
for feature in b.split(',') {
216+
features.insert(feature);
217+
}
218+
features.iter().map(|&s|s).collect::<Vec<&str>>().join(",")
219+
}

src/settings.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::*;
22

3-
#[derive(Debug, Clone, Copy)]
3+
#[derive(Debug, Clone)]
44
pub struct Settings {
55
pub summary: bool,
66
pub wrap: bool,
77
pub reverse: bool,
88
pub vim_keys: bool,
9+
pub no_default_features: bool,
10+
pub features: Option<String>, // comma separated list
911
}
1012

1113
impl Settings {
@@ -42,6 +44,12 @@ impl Settings {
4244
if args.reverse {
4345
self.reverse = true;
4446
}
47+
if args.no_default_features {
48+
self.no_default_features = true;
49+
}
50+
if args.features.is_some() {
51+
self.features = args.features.clone();
52+
}
4553
}
4654
}
4755

@@ -52,6 +60,8 @@ impl Default for Settings {
5260
wrap: false,
5361
reverse: false,
5462
vim_keys: false,
63+
no_default_features: false,
64+
features: None,
5565
}
5666
}
5767
}

0 commit comments

Comments
 (0)