Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit 7e3ec3c

Browse files
author
Alex Pinkus
committed
Filter to the host platform when running the default command
1 parent 30723ce commit 7e3ec3c

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use watchexec::{error::Result, run::watch};
88
mod args;
99
mod options;
1010
mod root;
11+
mod rustc;
1112
mod watch;
1213

1314
fn main() -> Result<()> {

src/options.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ use watchexec::{
1414
Shell,
1515
};
1616

17-
pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) {
17+
/// Sets all the requested commands on the passed-in `builder`.
18+
///
19+
/// If no commands are provided, this defaults to `cargo check`, and returns `true` to indicate
20+
/// that it used the default.
21+
pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) -> bool {
1822
let mut commands: Vec<String> = Vec::new();
1923

2024
// --features are injected just after applicable cargo subcommands
@@ -114,17 +118,23 @@ pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) {
114118
}
115119

116120
// Default to `cargo check`
117-
if commands.is_empty() {
121+
let default_command = if commands.is_empty() {
118122
let mut cmd: String = "cargo check".into();
119123
if let Some(features) = features.as_ref() {
120124
cmd.push_str(" --features ");
121125
cmd.push_str(features);
122126
}
123127
commands.push(cmd);
124-
}
128+
129+
true
130+
} else {
131+
false
132+
};
125133

126134
debug!("Commands: {:?}", commands);
127135
builder.cmd(commands);
136+
137+
default_command
128138
}
129139

130140
pub fn set_ignores(builder: &mut ConfigBuilder, matches: &ArgMatches) {
@@ -192,8 +202,13 @@ pub fn set_debounce(builder: &mut ConfigBuilder, matches: &ArgMatches) {
192202
}
193203
}
194204

195-
fn find_local_deps() -> Result<Vec<PathBuf>, String> {
205+
fn find_local_deps(filter_platform: Option<String>) -> Result<Vec<PathBuf>, String> {
206+
let options: Vec<String> = filter_platform
207+
.map(|platform| format!("--filter-platform={}", platform))
208+
.into_iter()
209+
.collect();
196210
let metadata = MetadataCommand::new()
211+
.other_options(options)
197212
.exec()
198213
.map_err(|e| format!("Failed to execute `cargo metadata`: {}", e))?;
199214

@@ -250,7 +265,7 @@ fn find_local_deps() -> Result<Vec<PathBuf>, String> {
250265
Ok(local_deps.into_iter().collect::<Vec<PathBuf>>())
251266
}
252267

253-
pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches) {
268+
pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches, only_default_command: bool) {
254269
let mut watches = Vec::new();
255270
if matches.is_present("watch") {
256271
for watch in values_t!(matches, "watch", String).unwrap_or_else(|e| e.exit()) {
@@ -260,7 +275,12 @@ pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches) {
260275

261276
if watches.is_empty() {
262277
if !matches.is_present("skip-local-deps") {
263-
match find_local_deps() {
278+
let filter_platform = if only_default_command {
279+
Some(crate::rustc::host_triple())
280+
} else {
281+
None
282+
};
283+
match find_local_deps(filter_platform) {
264284
Ok(dirs) => {
265285
if dirs.is_empty() {
266286
debug!("Found no local deps");
@@ -322,8 +342,8 @@ pub fn get_options(matches: &ArgMatches) -> Config {
322342

323343
set_ignores(&mut builder, matches);
324344
set_debounce(&mut builder, matches);
325-
set_watches(&mut builder, matches);
326-
set_commands(&mut builder, matches);
345+
let only_default_command = set_commands(&mut builder, matches);
346+
set_watches(&mut builder, matches, only_default_command);
327347

328348
let mut args = builder.build().unwrap();
329349
args.once = matches.is_present("once");

src/rustc.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use clap::{Error, ErrorKind};
2+
use std::{io::BufRead, process::Command};
3+
4+
/// Queries `rustc` to identify the host platform's target triple.
5+
pub fn host_triple() -> String {
6+
Command::new("rustc")
7+
.arg("-vV")
8+
.output()
9+
.map_err(|err| err.to_string())
10+
.and_then(|out| {
11+
// Look for a line starting with `host: `, just like `cargo` does, to identify the
12+
// host platform -- see:
13+
// https://github.com/rust-lang/cargo/blob/631b8774e512a69402a55367b4eb9c195220e404/src/cargo/util/rustc.rs#L68
14+
out.stdout
15+
.lines()
16+
.map_while(Result::ok)
17+
.find_map(|line| line.strip_prefix("host: ").map(ToString::to_string))
18+
.ok_or_else(|| "`rustc -vV` didn't have a line for `host`.".to_string())
19+
})
20+
.unwrap_or_else(|err| Error::with_description(&err, ErrorKind::Io).exit())
21+
}

0 commit comments

Comments
 (0)